android自带ProgressBar圆形进度条修改颜色的技巧方法无bug探索

通常来说,解决方法的代码是非常短的,但是解决这个事情如果没想到的话也许一天都搞不定,还不如自己写一个progressbar或者第三方的,但是我就是有这个强迫症,非得搞定这个问题 为了证明我解决这个问题不容易,我决定长篇大论,通常简单的代码连个回复的人都没有,其实有些精华代码那是作者花了大量时间才找到的东西,好歹点个赞呗。。

在网上找了一大堆,有设置progressDrawalbe方法的 如下面的代码

尝试1 修改progressdrawable

ClipDrawable clipDrawable = new ClipDrawable(new ColorDrawable(Color.YELLOW), Gravity.LEFT, ClipDrawable.HORIZONTAL);
//                progressBar.setProgressDrawable(colorDrawable);

设置不顶用,我也自己尝试设置ColorDrawalbe也是设置不顶用,然后尝试xml
设置progressdrable直接设置颜色,

尝试2 修改progressdrawableTinit着色

发现也不行然后测试progressdrawabletint 各种修改,竟然都没有效果 各种修改度没有效果。包括第二颜色。

尝试3 再次尝试代码设置drawabletint

    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

   ColorDrawable colorDrawable = new ColorDrawable(SuperAppContext.getInstance().getResources().getColor(R.color.colorThemeColor));
   progressBar.setIndeterminateDrawable(drawable);
       

xml代码

<?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

   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

 int color = SuperAppContext.getInstance().getResources().getColor(R.color.colorThemeColor);
                    ColorStateList colorStateList = ColorStateList.valueOf(color);
                    progressBar.setIndeterminateTintList(colorStateList);
                    progressBar.setIndeterminateTintMode(PorterDuff.Mode.SRC_ATOP);

如果要使用xml修改颜色

  <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" />

image.png

其他不怎么好用的方法参考
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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,532评论 25 708
  • 在此特此声明:一下所有链接均来自互联网,在此记录下我的查阅学习历程,感谢各位原创作者的无私奉献 ! 技术一点一点积...
    远航的移动开发历程阅读 11,239评论 12 197
  • 本系列出于AWeiLoveAndroid的分享,在此感谢,再结合自身经验查漏补缺,完善答案。以成系统。 Andro...
    济公大将阅读 1,853评论 2 10
  • 2017.11.8号 星期 三 天气 晴 因孩子们考完试老师要批卷家长可以报名看班,今天我也报名了...
    吕政民阅读 144评论 0 0
  • 肆意着,喧嚣吧,压抑着,爆发吧,近日的压抑憋屈无奈苦闷,发散吧。肆意地发散吧,痛快哭一场就好了,即使今晚狂风暴雨,...
    微_风起阅读 196评论 0 0