通常來說,解決方法的代碼是非常短的,但是解決這個事情如果沒想到的話也許一天都搞不定,還不如自己寫一個progressbar或者第三方的煮寡,但是我就是有這個強迫癥虹蓄,非得搞定這個問題 為了證明我解決這個問題不容易,我決定長篇大論幸撕,通常簡單的代碼連個回復的人都沒有薇组,其實有些精華代碼那是作者花了大量時間才找到的東西,好歹點個贊唄杈帐。体箕。
在網(wǎng)上找了一大堆,有設(shè)置progressDrawalbe方法的 如下面的代碼
嘗試1 修改progressdrawable
ClipDrawable clipDrawable = new ClipDrawable(new ColorDrawable(Color.YELLOW), Gravity.LEFT, ClipDrawable.HORIZONTAL);
// progressBar.setProgressDrawable(colorDrawable);
設(shè)置不頂用,我也自己嘗試設(shè)置ColorDrawalbe也是設(shè)置不頂用专钉,然后嘗試xml
設(shè)置progressdrable直接設(shè)置顏色挑童,
嘗試2 修改progressdrawableTinit著色
發(fā)現(xiàn)也不行然后測試progressdrawabletint 各種修改,竟然都沒有效果 各種修改度沒有效果跃须。包括第二顏色站叼。
嘗試3 再次嘗試代碼設(shè)置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里面出現(xiàn)了一點問題菇民,剛開始是不轉(zhuǎn)的尽楔,但是切換后臺回來就可以轉(zhuǎn)。
翻看了源碼感覺只能重新弄一個setProgressdrawable
并且調(diào)用的不是自己已經(jīng)生成的drawable才能解決問題
這個我就沒嘗試了第练,我不想用xml設(shè)置drawable,我也嘗試過感覺還可以類似玩seekbar那樣來解決問題阔馋。問題是我這是動態(tài)new的沒法設(shè)置主題
嘗試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動畫, 這就蛋疼了娇掏。
最后的辦法直接設(shè)置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" />
其他不怎么好用的方法參考
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