2016-08-23 42 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