代码设置progressbar的progressdrawable方法
项目里面有个地方的progressbar总是报图片oom,很奇葩.
Caused by: java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:601) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:434) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840) at android.content.res.Resources.createFromResourceStream(Resources.java:2513) at android.content.res.Resources.loadDrawable(Resources.java:2165) at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:387) at android.content.res.Resources.loadDrawable(Resources.java:2072) at android.content.res.Resources.getDrawable(Resources.java:710) at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:282) at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:939) at android.graphics.drawable.Drawable.createFromXml(Drawable.java:879) at android.content.res.Resources.createFromXml(Resources.java:2525) at android.content.res.Resources.loadDrawable(Resources.java:2145) at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:387) at android.content.res.Resources.loadDrawable(Resources.java:2072) at android.content.res.TypedArray.getDrawable(TypedArray.java:602) at android.widget.ProgressBar.<init>(ProgressBar.java:294) at android.widget.ProgressBar.<init>(ProgressBar.java:246) at android.widget.ProgressBar.<init>(ProgressBar.java:242)
最初的progressdrawable的xml文件如下
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background" android:drawable="@drawable/progress_default_background" /> <item android:id="@android:id/progress" > <clip> <nine-patch android:src="@drawable/progress_default"/> </clip> </item> </layer-list>
先是把.9图改为颜色
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background" android:drawable="@drawable/progress_bg" /> <item android:id="@android:id/progress" android:drawable="@drawable/progress_hl" /> </layer-list> <!-- 背景--> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#E0E0E0"></solid> <corners android:radius="1dip"></corners> </shape> <!-- 进度--> <clip xmlns:android="http://schemas.android.com/apk/res/android"> <shape> <solid android:color="#70B23F"></solid> <corners android:radius="1dip"></corners> </shape> </clip>
然并卵,还是报oom.
于是想在代码里用progressbar.setprogressdrawable()来设置,可是如果是从xml中获得的progressdrawable,发现进度是不会动的.
在网上看了好多人回答,大部分还是xml设置,貌似没什么人用代码设置progressdrawable.
自己试了好久终于设置成功了.代码如下
GradientDrawable p=new GradientDrawable(); p.setCornerRadius(dipToPixels(mContext, 1)); p.setColor(Color.rgb(0x70, 0xb2, 0x3f)); ClipDrawable progress = new ClipDrawable(p, Gravity.LEFT, ClipDrawable.HORIZONTAL); GradientDrawable background = new GradientDrawable(); background.setColor(Color.rgb(0xe0, 0xe0, 0xe0)); background.setCornerRadius(dipToPixels(mContext,1)); LayerDrawable pd=new LayerDrawable(new Drawable[]{background,progress});
progressbar.setProgressDrawable(pd)
效果看起来没问题了,希望测试不要又测出什么bug