我花了很多时间研究但没有希望。我想要做的是以编程方式更改progress_bar_clip.xml的位图drawable(android:id =“progress)。
progress_bar_clip.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/background">
<clip
android:clipOrientation="vertical"
android:drawable="@drawable/body_empty"
android:gravity="bottom" />
</item>
<item android:id="@android:id/progress">
<clip
android:clipOrientation="vertical"
android:gravity="bottom" >
<bitmap android:src="@drawable/body_empty"
android:id="@+id/to_be_changed" >
</bitmap>
</clip>
</item>
</layer-list>
<ProgressBar
android:id="@+id/progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="180dp"
android:layout_height="240dp"
android:layout_marginTop="80dp"
android:indeterminateOnly="false"
android:max="100"
android:progress="-500"
android:progressDrawable="@drawable/progress_bar_clip"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
>
</ProgressBar>
码
Resources res = getResources();
LayerDrawable layerDrawable = (LayerDrawable)res.getDrawable(R.drawable.progress_bar_clip);
Drawable newDrawable = res.getDrawable(R.drawable.i_want_to_set_this);
layerDrawable.setDrawableByLayerId(R.id.to_be_changed, newDrawable);
动画
private void wireResultAnimation(View view){
final ProgressBar bar = (ProgressBar) view.findViewById(R.id.progressbar);
final Handler handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
if (bar.getProgress() != 100) {
bar.incrementProgressBy(1);
handler.postDelayed(runnable, 20);
}
}
};
handler.post(runnable);
}
动画效果很好但是,drawable没有改变。