简书链接:android自带ProgressBar圆形进度条修改颜色的技巧方法无bug探索 文章字数:616,阅读全文大约需要2分钟 通常来说,解决方法的代码是非常短的,但是解决这个事情如果没想到的话也许一天都搞不定,还不如自己写一个progressbar或者第三方的,但是我就是有这个强迫症,非得搞定这个问题 为了证明我解决这个问题不容易,我决定长篇大论,通常简单的代码连个回复的人都没有,其实有些精华代码那是作者花了大量时间才找到的东西,好歹点个赞呗。。
在网上找了一大堆,有设置progressDrawalbe方法的 如下面的代码
尝试1 修改progressdrawable 1 2 ClipDrawable clipDrawable = new ClipDrawable(new ColorDrawable(Color.YELLOW), Gravity.LEFT, ClipDrawable.HORIZONTAL); // progressBar.setProgressDrawable(colorDrawable);
设置不顶用,我也自己尝试设置ColorDrawalbe也是设置不顶用,然后尝试xml 设置progressdrable直接设置颜色,
尝试2 修改progressdrawableTinit着色 发现也不行然后测试progressdrawabletint 各种修改,竟然都没有效果 各种修改度没有效果。包括第二颜色。
尝试3 再次尝试代码设置drawabletint 1 2 3 4 5 6 7 8 9 10 11 12 progressBar.setProgressDrawableTiled(colorDrawable); /* int[] colors = new int[] { pressed, focused, normal, focused, unable, normal }; int[][] states = new int[6][]; states[0] = new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }; states[1] = new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }; states[2] = new int[] { android.R.attr.state_enabled }; states[3] = new int[] { android.R.attr.state_focused }; states[4] = new int[] { android.R.attr.state_window_focused }; states[5] = new int[] {}; ColorStateList colorList = new ColorStateList(states, colors); return colorList;*/ // ColorStateList ColorStateList=new ColorStateList();
尝试4 setIndeterminateDrawable 1 2 3 4 ColorDrawable colorDrawable = new ColorDrawable(SuperAppContext.getInstance().getResources().getColor(R.color.colorThemeColor)); progressBar.setIndeterminateDrawable(drawable);
xml代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 <?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:repeatCount="-1" android:duration="200" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0" android:toDegrees="359"> <shape android:shape="ring" android:innerRadiusRatio="3" android:thicknessRatio="8" android:useLevel="false"> <size android:width="55dp" android:height="55dp" /> <gradient android:type="sweep" android:useLevel="false" android:startColor="@color/colorThemeColor" android:centerY="0.50" android:endColor="@color/transparent" /> </shape> </rotate>
这次可以了,但是在fragment里面出现了一点问题,刚开始是不转的,但是切换后台回来就可以转。 翻看了源码感觉只能重新弄一个setProgressdrawable
并且调用的不是自己已经生成的drawable才能解决问题 这个我就没尝试了,我不想用xml设置drawable,我也尝试过感觉还可以类似玩seekbar那样来解决问题。问题是我这是动态new的没法设置主题
尝试5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 int N = background.getNumberOfFrames(); AnimationDrawable newBg = new AnimationDrawable(); newBg.setOneShot(background.isOneShot()); for (int i = 0; i < N; i++) { final Bitmap tileBitmap = ((BitmapDrawable) background.getFrame(i)).getBitmap(); final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); shape.getPaint().setShader(bitmapShader); final Drawable result = new ClipDrawable(shape, Gravity.LEFT, ClipDrawable.HORIZONTAL); result.setLevel(10000); newBg.addFrame(result, background.getDuration(i)); } newBg.setLevel(10000);*
这些方法都是基于xml AnimationDrawable动画, 这就蛋疼了。
最后的办法直接设置IndeterminateDrawable着色无bug 1 2 3 4 int color = SuperAppContext.getInstance().getResources().getColor(R.color.colorThemeColor); ColorStateList colorStateList = ColorStateList.valueOf(color); progressBar.setIndeterminateTintList(colorStateList); progressBar.setIndeterminateTintMode(PorterDuff.Mode.SRC_ATOP);
如果要使用xml修改颜色
1 2 3 4 5 6 7 8 <ProgressBar android:id="@+id/progressbar" android:indeterminateTint="@color/theme_color_red" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" />
其他不怎么好用的方法参考http://blog.csdn.net/tszxlzc/article/details/38420005 http://blog.csdn.net/chenlove1/article/details/41758977 http://blog.csdn.net/baiyuliang2013/article/details/50767034 https://stackoverflow.com/questions/10951978/change-progressbar-color-through-code-only-in-android https://stackoverflow.com/questions/30488570/indeterminate-progressbar-with-animation-and-rounded-corners http://blog.csdn.net/xiangxue336/article/details/9301337