效果图:
利用TimerTask动态改变滚动条前景色
主要代码如下:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/main_battery_bg"
android:orientation="horizontal" >
<ProgressBar
android:id="@+id/pb_battery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:indeterminate="false"
android:indeterminateOnly="false"
android:progress="0"
android:progressDrawable="@drawable/main_progress_vertical" />
</LinearLayout>
</RelativeLayout>
main_progress_vertical.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/progress">
<clip
android:clipOrientation="vertical"
android:gravity="bottom" >
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="180"
android:centerColor="#ffffcb00"
android:centerY="0.75"
android:endColor="#ffffcb00"
android:startColor="#ffffcb00" />
</shape>
</clip>
</item>
</layer-list>
main_progress_vertical_warn.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@android:id/progress">
<clip
android:clipOrientation="vertical"
android:gravity="bottom" >
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="180"
android:centerColor="#ff0000"
android:centerY="0.75"
android:endColor="#ff0000"
android:startColor="#ff0000" />
</shape>
</clip>
</item>
</layer-list>
MainActivity
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
/**
* 达到警告线
*/
public static final int BATTERYWARN = 20;
private ProgressBar pBar;
/**
* 需要改变多少
*/
private int progressAdd = 0;
/**
* 改变是上升还是下降
*/
private int actionType = 1;
/**
* 原来的progress值
*/
private int progressOld = 0;
private Timer timer = new Timer();
/**
* 动态改变滚动条的ProgressDrawable
*
* @param resId
* Drawable资源ID
*/
private void setProgressDrawable(int resId) {
Drawable drawable = MainActivity.this.getResources().getDrawable(resId);
drawable.setBounds(pBar.getProgressDrawable().getBounds());
/* 设置drawable之后貌似getProgress是从当前的为progress值为起始,
* 所以注意先取得progress,然后重置progres=0,然后设置drawable,
* 最后再把原来的progress还原,要不然设置了drawable之后ProgressBar的前景色会消失
* 这里不会出现这种问题,因为progress始终是变化的,前景色的变化设置drawable也只执行一次
*/
pBar.setProgressDrawable(drawable);
}
/**
* 电池滚动条特效
*/
private TimerTask task = new TimerTask() {
int add = 0;
public void run() {
try {
while (add++ < progressAdd) {
int progress = pBar.getProgress() + actionType;
progressOld = progress - actionType;
if (progress <= BATTERYWARN) {
// 初始时 或者 原先的值大于下限(只执行一次动态改变ProgressDrawable)
if (progressOld == 0 || progressOld > BATTERYWARN) {
setProgressDrawable(R.drawable.main_progress_vertical_warn);
}
} else {
// 原先的本身比下限小并且有值则还原进度条默认颜色(只执行一次动态改变ProgressDrawable,原来没值时按默认)
if (progressOld > 0 && progressOld <= BATTERYWARN) {
setProgressDrawable(R.drawable.main_progress_vertical);
}
}
pBar.setProgress(progress);
// 最后一次不用休眠
if (add < progressAdd) {
Thread.sleep(30);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
bindData(true);
}
/**
* 初始化view
*/
private void initView() {
pBar = (ProgressBar) findViewById(R.id.pb_battery);
pBar.setMax(100);
}
/**
* 绑定数据
*
* @param isStart
* 初始绑定还是有广播数据变更需要刷新数据,tru初始绑定数据,false有广播数据变更需要刷新数据
*/
private void bindData(boolean isStart) {
// 测试数据,收入
int moneyIn = 30;
// 测试数据,预算
int moneyYs = 300;
// 测试数据,支出
double moneyOut = 110;
// 目前滚动条状态%值
int progress = 100 - (int) ((moneyOut / (moneyIn + moneyYs)) * 100);
/*测试下降
//测试数据,原滚动条状态值
pBar.setProgress(progress);
//测试数据,别处操作了收入、支出、预算的数据使其发生了变化
isStart = false;
*/
// 初始加载
if (isStart) {
progressAdd = progress;
} else {
// 测试数据,测试下降需要把上面的 /*测试下降 注释去掉
progress = 5;
progressOld = pBar.getProgress();
// 上升还是下降
progressAdd = progress - progressOld;
if (progressAdd < 0) {
actionType = -1;
progressAdd *= -1;
}
}
// 过500毫秒执行task
timer.schedule(task, 500);
}
}
主要是动态改变ProgressBar的progressDrawable。