2016-08-23 44 views

回答

6

您可以從ClipDrawable開始。這將基於可繪製的級別截取另一個可繪製的—例如ShapeDrawable —。

然後在你的計時器回調:

int level; // from 0 to 10000 = 100% 

    view.getBackground.setLevel(level); 

Drawable#setLevel


編輯:下面是一個例子:

  1. 定義形狀繪製在/res/drawable。叫它bkgd_shape.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <shape 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:shape="rectangle"> 
        <solid android:color="@color/background"/> 
    </shape> 
    
  2. 定義可在/res/drawable中繪製的背景。讓我們把它叫做bkgd_level.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <clip 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:drawable="@drawable/bkgd_shape" 
        android:clipOrientation="horizontal" 
        android:gravity="left|clip_horizontal|fill_vertical"/> 
    

    可能能夠把顏色直接用於繪製源,但我還沒有嘗試過。

  3. 將它設置爲佈局背景:

    android:background="@drawable/bkgd_level" 
    
  4. 呼叫setLevel上繪製:

    int level; // from 0 to 10000 = 100% 
    
        view.getBackground.setLevel(level); 
    
+0

請問您能解釋一下關於使用ClipDrawable的更多信息... – Obtice

+1

使用示例XML更新了答案。 –

+0

非常感謝,這工作得很好... – Obtice

1

你可以把任何類型的視圖的背景和更新它與和設置你想它動態的顏色。

DisplayMetrics dm = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(dm); 

int width = dm.widthPixels; 
int height = dm.heightPixels; 

擴大寬度然後,只是按百分比。

int percentChange = .2; //Update this accordingly, put in its own function possibly 
int backgroundWidth = width * percentChange; 

例如,使用在後臺的ImageView:

ImageView background = (ImageView) findViewById(R.id.expandingBackground); 
background.requestLayout(); 
background.getLayoutParams().width = backgroundWidth; 

希望幫助,祝你好運。

+0

我想改變我的活動佈局的背景顏色,在這種情況下,我認爲我的意見將不可見。我對嗎 ? – Obtice

+1

它們將可見,只是將它放在背後的其他背後。例如,在相對佈局內的組件樹中,將其放置在所有內容的底部(可能位於頂部),將其移至背景。 – Steeno

+0

謝謝兄弟,我會試試... – Obtice