2016-08-04 101 views
0

我想在android(@android:style/Widget.DeviceDefault.ProgressBar)中自定义进度条,使其改变颜色(例如,它会在第一次旋转,然后是蓝色,然后是黄色等时为红色)。在互联网上阅读过一些东西,并试图使用动画列表,但没有奏效。任何想法如何实现这一目标?在进度条上添加动画android

回答

0

在较新版本的Android(API 21+)中,您可以以编程方式更改颜色。

我会用一个countdownTimer,这可能不是最好的方式,但是这一点,我怎么会做到这一点:P:

//In the public class, make a variable which can simply be an int 
int colorCode = 0; 


//then you put this code where your progress bar starts... 

new CountdownTimer(3000,1000){ 
public void onTick(long millisUntilDone){ 

if(colorCode == 0){ 

colorCode = 1; 

}else if(colorCode == 1){ 

colorCode = 2; 

}else { 

colorCode = 0; 

} 

if(colorCode == 0){ 
//To set it red, use: 

//bar is a ProgressBar 
bar.setProgressTintList(ColorStateList.valueOf(Color.RED)); 

}else if(colorCode == 1){ 

//To set it blue, use: 

//bar is a ProgressBar 
bar.setProgressTintList(ColorStateList.valueOf(Color.BLUE)); 

}else { 

To set it green, use: 

//bar is a ProgressBar 
bar.setProgressTintList(ColorStateList.valueOf(Color.GREEN)); 

} 

} 


public void onFinish(){ 

//you don't have to set it to do anything... 

} 

}.start(); 

的API 16+我认为这将更好地工作:

Drawable drawable = progressBar.getProgressDrawable(); 
drawable.setColorFilter(new LightingColorFilter(0xFF000000, customColorInt)); 

在每一个地方,你会用的地方就用这个:

bar.setProgressTintList(ColorStateList.valueOf(Color.GREEN)); 
+0

OK谢谢你,我会尝试做这样的事情......但需要网络连接找到一个更低的api的解决方案(需要支持16+)..我认为,也许有一个更简单的方法来实现直接在xml文件 – Lalilalu

+0

我希望这会有所帮助:D –