开源项目NumberProgressBar
原地址:https://github.com/daimajia/NumberProgressBar
eclipse环境demo下载
http://download.csdn.net/detail/yang786654260/8662541
自定义progressBar控件
直接copy到项目中使用。
自定义属性
创建attrs.xml 用来自定义属性名,源码需要
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="NumberProgressBar">
<attr name="progress" format="integer"/>
<attr name="max" format="integer"/>
<attr name="progress_unreached_color" format="color"/>
<attr name="progress_reached_color" format="color"/>
<attr name="progress_reached_bar_height" format="dimension"/>
<attr name="progress_unreached_bar_height" format="dimension"/>
<attr name="progress_text_size" format="dimension"/>
<attr name="progress_text_color" format="color"/>
<attr name="progress_text_offset" format="dimension"/>
<attr name="progress_text_visibility" format="enum">
<enum name="visible" value="0"/>
<enum name="invisible" value="1"/>
</attr>
</declare-styleable>
<declare-styleable name="Themes">
<attr name="numberProgressBarStyle" format="reference"/>
</declare-styleable>
</resources>
自定义样式
创建style样式
<style name="NumberProgressBar_Default">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
<item name="max">100</item>
<item name="progress">0</item>
<item name="progress_unreached_color">#CCCCCC</item>
<item name="progress_reached_color">#3498DB</item>
<item name="progress_text_size">10sp</item>
<item name="progress_text_color">#3498DB</item>
<item name="progress_reached_bar_height">1.5dp</item>
<item name="progress_unreached_bar_height">0.75dp</item>
</style>
reached和unreached属性请看下图:
在布局中的使用
<com.daimajia.numberprogressbar.NumberProgressBar
android:id="@+id/numberbar1"
style="@style/NumberProgressBar_Default"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
custom:progress="0" />
在代码中的使用
public class MainActivity extends Activity {
private int counter = 0;
private Timer timer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final NumberProgressBar npb1 = (NumberProgressBar) findViewById(R.id.numberbar1);
counter = 0;
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
npb1.incrementProgressBy(1);//进度的增加量
counter ++;
if (counter == 110) {
npb1.setProgress(0);//设置进度
counter = 0;
}
}
});
}
}, 1000, 100);
}
@Override
protected void onDestroy() {
super.onDestroy();
timer.cancel();
}
}