欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义View实现带数字的进度条实例代码

程序员文章站 2024-02-27 23:34:21
第一步、效果展示 图1、蓝色的进度条 图2、红色的进度条 图3、多条颜色不同的进度条 图4、多条颜色不同的进度条 第二步...

第一步、效果展示

图1、蓝色的进度条

Android自定义View实现带数字的进度条实例代码

图2、红色的进度条

Android自定义View实现带数字的进度条实例代码

图3、多条颜色不同的进度条

Android自定义View实现带数字的进度条实例代码

图4、多条颜色不同的进度条

Android自定义View实现带数字的进度条实例代码

Android自定义View实现带数字的进度条实例代码

Android自定义View实现带数字的进度条实例代码

第二步、自定义progressbar实现带数字的进度条

0、项目结构

Android自定义View实现带数字的进度条实例代码

如上图所示:library项目为自定义的带数字的进度条numberprogressbar的具体实现,demo项目为示例项目以工程依赖的方式引用library项目,然后使用自定义的带数字的进度条numberprogressbar来做展示

Android自定义View实现带数字的进度条实例代码 

如上图所示:自定义的带数字的进度条的library项目的结构图

Android自定义View实现带数字的进度条实例代码 

如上图所示:demo项目的结构图

1、绘制步骤分析

如上面几幅图形所示。这个进度条的可以分为以下三部分:

Android自定义View实现带数字的进度条实例代码

reacherd area :表示当前进度值之前文本的进度条(长方形)

text area :表示当前进度值文本

unreacherd area :当前进度值文本之后的进度条(长方形)

按照上面的分析,我们要实现带数字的进度条,只需要按照以下三个步骤绘制即可实现:

1、绘制reacherd area(当前进度值之前文本的进度条)

2、绘制text area(当前进度值文本)

3、绘制unreacherd area(当前进度值文本之后的进度条) 即可。

2、自定义属性

由于我们发现以上三个部分的颜色、字体大小、进度条的最大值、表示进度条的长方形的高度等属性都可以改变,从而展现出不同的界面效果。

因此我们将这些属性都做自定义属性。这样我们就能够做到像android官方提供的那些组件一样用xml来定义它的属性了。

1、定义自己的属性配置文件:attr.xml

在res/values文件下定义一个attrs.xml文件,res/values/attrs.xml定义代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="numberprogressbar">
<!--进度条的当前进度值-->
<attr name="progress_current" format="integer"/>
<!--进度条的最大进度值-->
<attr name="progress_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>

2、定义主题配置文件:styles.xml

在res/values文件下定义一个styles.xml文件,里面定义一些基本的主题选项,以备用户可以选择使用。res/values/styles.xml定义代码如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="numberprogressbar_default">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
<item name="progress_max">100</item>
<item name="progress_current">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>
<style name="numberprogressbar_passing_green">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
<item name="progress_max">100</item>
<item name="progress_current">0</item>
<item name="progress_unreached_color">#cccccc</item>
<item name="progress_reached_color">#70a800</item>
<item name="progress_text_size">10sp</item>
<item name="progress_text_color">#70a800</item>
<item name="progress_reached_bar_height">1.5dp</item>
<item name="progress_unreached_bar_height">0.75dp</item>
</style>
<style name="numberprogressbar_beauty_red">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
<item name="progress_max">100</item>
<item name="progress_current">0</item>
<item name="progress_unreached_color">#cccccc</item>
<item name="progress_reached_color">#ff3d7f</item>
<item name="progress_text_size">10sp</item>
<item name="progress_text_color">#ff3d7f</item>
<item name="progress_reached_bar_height">1.5dp</item>
<item name="progress_unreached_bar_height">0.75dp</item>
</style>
<style name="numberprogressbar_warning_red">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
<item name="progress_max">100</item>
<item name="progress_current">0</item>
<item name="progress_unreached_color">#cccccc</item>
<item name="progress_reached_color">#e74c3c</item>
<item name="progress_text_size">10sp</item>
<item name="progress_text_color">#e74c3c</item>
<item name="progress_reached_bar_height">1.5dp</item>
<item name="progress_unreached_bar_height">0.75dp</item>
</style>
<style name="numberprogressbar_relax_blue">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
<item name="progress_max">100</item>
<item name="progress_current">0</item>
<item name="progress_unreached_color">#cccccc</item>
<item name="progress_reached_color">#6dbcdb</item>
<item name="progress_text_size">10sp</item>
<item name="progress_text_color">#6dbcdb</item>
<item name="progress_reached_bar_height">1.5dp</item>
<item name="progress_unreached_bar_height">0.75dp</item>
</style>
<style name="numberprogressbar_grace_yellow">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
<item name="progress_max">100</item>
<item name="progress_current">0</item>
<item name="progress_unreached_color">#cccccc</item>
<item name="progress_reached_color">#ffc73b</item>
<item name="progress_text_size">10sp</item>
<item name="progress_text_color">#ffc73b</item>
<item name="progress_reached_bar_height">1.5dp</item>
<item name="progress_unreached_bar_height">0.75dp</item>
</style>
<style name="numberprogressbar_funny_orange">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
<item name="progress_max">100</item>
<item name="progress_current">0</item>
<item name="progress_unreached_color">#cccccc</item>
<item name="progress_reached_color">#ff530d</item>
<item name="progress_text_size">10sp</item>
<item name="progress_text_color">#ff530d</item>
<item name="progress_reached_bar_height">1.5dp</item>
<item name="progress_unreached_bar_height">0.75dp</item>
</style>
<style name="numberprogressbar_twinkle_night">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">match_parent</item>
<item name="progress_max">100</item>
<item name="progress_current">0</item>
<item name="progress_unreached_color">#cccccc</item>
<item name="progress_reached_color">#ecf0f1</item>
<item name="progress_text_size">10sp</item>
<item name="progress_text_color">#ecf0f1</item>
<item name="progress_reached_bar_height">1.5dp</item>
<item name="progress_unreached_bar_height">0.75dp</item>
</style>
</resources>

3、自定义view实现带数字的进度条

package com.daimajia.numberprogressbar;
import android.content.context;
import android.content.res.typedarray;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.paint;
import android.graphics.rectf;
import android.os.bundle;
import android.os.parcelable;
import android.util.attributeset;
import android.view.view;
import static com.daimajia.numberprogressbar.numberprogressbar.progresstextvisibility.invisible;
import static com.daimajia.numberprogressbar.numberprogressbar.progresstextvisibility.visible;
/**
* created by daimajia on 14-4-30.
*/
public class numberprogressbar extends view {
/**
* 进度值最大值
*/
private int mmaxprogress = 100;
/**
* current progress, can not exceed the max progress.
* 当前进度值,不能超过进度值最大值
*/
private int mcurrentprogress = 0;
/**
* the progress area bar color.
* 当前进度值文本之前的进度条颜色
*/
private int mreachedbarcolor;
/**
* the bar unreached area color.
* 当前进度值文本之后的进度条颜色
*/
private int munreachedbarcolor;
/**
* the progress text color.
* 当前进度值文本的颜色
*/
private int mtextcolor;
/**
* the progress text size.
* 当前进度值文本的字体大小
*/
private float mtextsize;
/**
* the height of the reached area.
* 当前进度值文本之前的进度条的高度
*/
private float mreachedbarheight;
/**
* the height of the unreached area.
* 当前进度值文本之后的进度条的高度
*/
private float munreachedbarheight;
/**
* the suffix of the number.
* 当前进度值的百分比后缀
*/
private string msuffix = "%";
/**
* the prefix.
* 当前进度值的百分比前缀
*/
private string mprefix = "";
//当前进度值文本的默认颜色
private final int default_text_color = color.rgb(66, 145, 241);
//当前进度值文本的字体大小
private final float default_text_size;
//当前进度值之前的默认进度条颜色
private final int default_reached_color = color.rgb(66, 145, 241);
//当前进度值之后的默认进度条颜色
private final int default_unreached_color = color.rgb(204, 204, 204);
//当前进度值之前文本的默认间距
private final float default_progress_text_offset;
//当前进度值文本之前的进度条的默认高度
private final float default_reached_bar_height;
//当前进度值文本之后的进度条的默认高度
private final float default_unreached_bar_height;
/**
* for save and restore instance of progressbar.
*/
private static final string instance_state = "saved_instance";
private static final string instance_text_color = "text_color";
private static final string instance_text_size = "text_size";
private static final string instance_reached_bar_height = "reached_bar_height";
private static final string instance_reached_bar_color = "reached_bar_color";
private static final string instance_unreached_bar_height = "unreached_bar_height";
private static final string instance_unreached_bar_color = "unreached_bar_color";
private static final string instance_max = "max";
private static final string instance_progress = "progress";
private static final string instance_suffix = "suffix";
private static final string instance_prefix = "prefix";
private static final string instance_text_visibility = "text_visibility";
//默认显示当前进度值文本 0为显示,1为不显示
private static final int progress_text_visible = 0;
/**
* the width of the text that to be drawn.
* 要绘制的当前进度值的文本的宽度
*/
private float mdrawtextwidth;
/**
* the drawn text start.
* 要绘制的当前进度值的文本的起始位置
*/
private float mdrawtextstart;
/**
* the drawn text end.
* 要绘制的当前进度值的文本的结束位置
*/
private float mdrawtextend;
/**
* the text that to be drawn in ondraw().
* 要绘制的当前进度值的文本
*/
private string mcurrentdrawtext;
/**
* the paint of the reached area.
* 绘制当前进度值文本之前的进度条的画笔
*/
private paint mreachedbarpaint;
/**
* the paint of the unreached area.
* 绘制当前进度值文本之后的进度条的画笔
*/
private paint munreachedbarpaint;
/**
* the paint of the progress text.
* 绘制当前进度值文本的的画笔
*/
private paint mtextpaint;
/**
* unreached bar area to draw rect.
* 当前进度值文本之后的进度条(长方形)
*/
private rectf munreachedrectf = new rectf(0, 0, 0, 0);
/**
* reached bar area rect.
* 当前进度值之前文本的进度条(长方形)
*/
private rectf mreachedrectf = new rectf(0, 0, 0, 0);
/**
* the progress text offset.
* 当前进度值之前文本的间距
*/
private float moffset;
/**
* determine if need to draw unreached area.
* 是否绘制当前进度值之后的进度条
*/
private boolean mdrawunreachedbar = true;
/**
* 是否绘制当前进度值之前的进度条
*/
private boolean mdrawreachedbar = true;
/**
* 是否绘制当前进度值文本
*/
private boolean mifdrawtext = true;
/**
* listener
*/
private onprogressbarlistener mlistener;
public enum progresstextvisibility {
visible, invisible
}
public numberprogressbar(context context) {
this(context, null);
}
public numberprogressbar(context context, attributeset attrs) {
this(context, attrs, r.attr.numberprogressbarstyle);
}
public numberprogressbar(context context, attributeset attrs, int defstyleattr) {
super(context, attrs, defstyleattr);
default_reached_bar_height = dp2px(1.5f);
default_unreached_bar_height = dp2px(1.0f);
default_text_size = sp2px(10);
default_progress_text_offset = dp2px(3.0f);
//获取自定义属性
final typedarray attributes = context.gettheme().obtainstyledattributes(attrs, r.styleable.numberprogressbar,
defstyleattr, 0);
mreachedbarcolor = attributes.getcolor(r.styleable.numberprogressbar_progress_reached_color, default_reached_color);
munreachedbarcolor = attributes.getcolor(r.styleable.numberprogressbar_progress_unreached_color, default_unreached_color);
mtextcolor = attributes.getcolor(r.styleable.numberprogressbar_progress_text_color, default_text_color);
mtextsize = attributes.getdimension(r.styleable.numberprogressbar_progress_text_size, default_text_size);
mreachedbarheight = attributes.getdimension(r.styleable.numberprogressbar_progress_reached_bar_height, default_reached_bar_height);
munreachedbarheight = attributes.getdimension(r.styleable.numberprogressbar_progress_unreached_bar_height, default_unreached_bar_height);
moffset = attributes.getdimension(r.styleable.numberprogressbar_progress_text_offset, default_progress_text_offset);
int textvisible = attributes.getint(r.styleable.numberprogressbar_progress_text_visibility, progress_text_visible);
if (textvisible != progress_text_visible) {
mifdrawtext = false;
}
setprogress(attributes.getint(r.styleable.numberprogressbar_progress_current, 0));
setmax(attributes.getint(r.styleable.numberprogressbar_progress_max, 100));
//回收 typedarray,用于后续调用时可复用之。回收到typedarraypool池中,以备后用
attributes.recycle();
initializepainters();
}
@override
protected int getsuggestedminimumwidth() {
return (int) mtextsize;
}
@override
protected int getsuggestedminimumheight() {
return math.max((int) mtextsize, math.max((int) mreachedbarheight, (int) munreachedbarheight));
}
@override
protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
/**
* measurespec参数的值为int型,分为高32位和低16为,
* 高32位保存的是specmode,低16位表示specsize,
*
* specmode分三种:
1、measurespec.unspecified,父视图不对子视图施加任何限制,子视图可以得到任意想要的大小;
2、measurespec.exactly,父视图希望子视图的大小是specsize中指定的大小;
3、measurespec.at_most,子视图的大小最多是specsize中的大小。
*/
setmeasureddimension(measure(widthmeasurespec, true), measure(heightmeasurespec, false));
}
private int measure(int measurespec, boolean iswidth) {
int result;
int mode = measurespec.getmode(measurespec);
int size = measurespec.getsize(measurespec);
int padding = iswidth ? getpaddingleft() + getpaddingright() : getpaddingtop() + getpaddingbottom();
/**
父决定子的确切大小,子被限定在给定的边界里,忽略本身想要的大小。
(当设置width或height为match_parent时,模式为exactly,因为子view会占据剩余容器的空间,所以它大小是确定的)
*/
if (mode == measurespec.exactly) {
result = size;
} else {
result = iswidth ? getsuggestedminimumwidth() : getsuggestedminimumheight();
result += padding;
/**
*子最大可以达到的指定大小
* (当设置为wrap_content时,模式为at_most, 表示子view的大小最多是多少,这样子view会根据这个上限来设置自己的尺寸)
*/
if (mode == measurespec.at_most) {
if (iswidth) {
result = math.max(result, size);
} else {
result = math.min(result, size);
}
}
}
return result;
}
@override
protected void ondraw(canvas canvas) {
//如果要绘制当前进度值文本
if (mifdrawtext) {
calculatedrawrectf();
}else {
calculatedrawrectfwithoutprogresstext();
}
//如果要绘制当前进度值之前的进度条
if (mdrawreachedbar) {
canvas.drawrect(mreachedrectf, mreachedbarpaint);
}
//如果要绘制当前进度值之后的进度条
if (mdrawunreachedbar) {
canvas.drawrect(munreachedrectf, munreachedbarpaint);
}
//绘制当前进度值文本
if (mifdrawtext)
canvas.drawtext(mcurrentdrawtext, mdrawtextstart, mdrawtextend, mtextpaint);
}
/**
* 初始化画笔
*/
private void initializepainters() {
mreachedbarpaint = new paint(paint.anti_alias_flag);
mreachedbarpaint.setcolor(mreachedbarcolor);
munreachedbarpaint = new paint(paint.anti_alias_flag);
munreachedbarpaint.setcolor(munreachedbarcolor);
mtextpaint = new paint(paint.anti_alias_flag);
mtextpaint.setcolor(mtextcolor);
mtextpaint.settextsize(mtextsize);
}
/**
* 计算不要绘制当前进度值文本时 图形的各个属性
*/
private void calculatedrawrectfwithoutprogresstext() {
//当前进度值不画
//当前进度值之前的进度条(长方形)的属性
mreachedrectf.left = getpaddingleft();
mreachedrectf.top = getheight() / 2.0f - mreachedbarheight / 2.0f;
mreachedrectf.right =
(getwidth() - getpaddingleft() - getpaddingright()) / (getmax() * 1.0f) * getprogress()
+ getpaddingleft();
mreachedrectf.bottom = getheight() / 2.0f + mreachedbarheight / 2.0f;
//当前进度值之后的进度条(长方形)的属性
munreachedrectf.left = mreachedrectf.right;
munreachedrectf.right = getwidth() - getpaddingright();
munreachedrectf.top = getheight() / 2.0f + -munreachedbarheight / 2.0f;
munreachedrectf.bottom = getheight() / 2.0f + munreachedbarheight / 2.0f;
}
/**
* 计算要绘制当前进度值文本时 图形的各个属性
*/
private void calculatedrawrectf() {
//要绘制的当前进度值的文本
mcurrentdrawtext = string.format("%d", getprogress() * 100 / getmax());
mcurrentdrawtext = mprefix + mcurrentdrawtext + msuffix;
//要绘制的当前进度值的文本的宽度
mdrawtextwidth = mtextpaint.measuretext(mcurrentdrawtext);
//如果当前进度值为0,则不绘制当前进度值之前的进度条
if (getprogress() == 0) {
mdrawreachedbar = false;
mdrawtextstart = getpaddingleft();
}
//否则绘制当前进度值文本之前的进度条
else {
mdrawreachedbar = true;
//当前进度值文本之前的进度条(长方形)的属性
mreachedrectf.left = getpaddingleft();
mreachedrectf.top = getheight() / 2.0f - mreachedbarheight / 2.0f;
mreachedrectf.right= (getwidth() - getpaddingleft() - getpaddingright()) / (getmax() * 1.0f) * getprogress()
- moffset + getpaddingleft();
mreachedrectf.bottom = getheight() / 2.0f + mreachedbarheight / 2.0f;
//当前进度值的文本的起始位置
mdrawtextstart = (mreachedrectf.right + moffset);
}
//当前进度值的文本的结束位置
mdrawtextend = (int) ((getheight() / 2.0f) - ((mtextpaint.descent() + mtextpaint.ascent()) / 2.0f));
//如果画不下当前进度值的文本了,就重新计算下当前进度值的文本的起始位置和当前进度值之前的进度条(长方形)的右边
if ((mdrawtextstart + mdrawtextwidth) >= getwidth() - getpaddingright()) {
mdrawtextstart = getwidth() - getpaddingright() - mdrawtextwidth;
mreachedrectf.right = mdrawtextstart - moffset;
}
//当前进度值文本之后的进度条的起始位置
float unreachedbarstart = mdrawtextstart + mdrawtextwidth + moffset;
//如果画不下进度值文本之后的进度条了,就不画进度值之后的进度条
if (unreachedbarstart >= getwidth() - getpaddingright()) {
mdrawunreachedbar = false;
} else {
mdrawunreachedbar = true;
//当前进度值文本之后的进度条(长方形)的属性
munreachedrectf.left = unreachedbarstart;
munreachedrectf.right = getwidth() - getpaddingright();
munreachedrectf.top = getheight() / 2.0f + -munreachedbarheight / 2.0f;
munreachedrectf.bottom = getheight() / 2.0f + munreachedbarheight / 2.0f;
}
}
/**
* get progress text color.
* 获取当前进度值文本的颜色
* @return progress text color.
*/
public int gettextcolor() {
return mtextcolor;
}
/**
* get progress text size.
* 获取当前进度值文本的字体大小
* @return progress text size.
*/
public float getprogresstextsize() {
return mtextsize;
}
/**
* 获取当前进度值文本之后的进度条颜色
*/
public int getunreachedbarcolor() {
return munreachedbarcolor;
}
/**
* 获取当前进度值文本之前的进度条颜色
*/
public int getreachedbarcolor() {
return mreachedbarcolor;
}
/**
* 获取进度条的当前进度值
*/
public int getprogress() {
return mcurrentprogress;
}
/**
* 获取进度条的最大值
*/
public int getmax() {
return mmaxprogress;
}
/**
* 获取当前进度值文本之前的进度条的高度
*/
public float getreachedbarheight() {
return mreachedbarheight;
}
/**
* 获取当前进度值文本之后的进度条的高度
*/
public float getunreachedbarheight() {
return munreachedbarheight;
}
/**
* 设置当前进度值文本的字体大小
* @param textsize 当前进度值文本的字体大小
*/
public void setprogresstextsize(float textsize) {
this.mtextsize = textsize;
mtextpaint.settextsize(mtextsize);
invalidate();
}
/**
* 设置当前进度值文本的颜色
* @param textcolor 当前进度值文本的颜色
*/
public void setprogresstextcolor(int textcolor) {
this.mtextcolor = textcolor;
mtextpaint.setcolor(mtextcolor);
invalidate();
}
/**
* 设置当前进度值文本之后的进度条颜色
* @param barcolor 当前进度值文本之后的进度条颜色
*/
public void setunreachedbarcolor(int barcolor) {
this.munreachedbarcolor = barcolor;
munreachedbarpaint.setcolor(munreachedbarcolor);
invalidate();
}
/**
* 设置当前进度值文本之前的进度条颜色
* @param progresscolor 当前进度值文本之前的进度条颜色
*/
public void setreachedbarcolor(int progresscolor) {
this.mreachedbarcolor = progresscolor;
mreachedbarpaint.setcolor(mreachedbarcolor);
invalidate();
}
/**
* 设置当前进度值文本之前的进度条的高度
* @param height 当前进度值文本之前的进度条的高度
*/
public void setreachedbarheight(float height) {
mreachedbarheight = height;
}
/**
* 设置当前进度值文本之后的进度条的高度
* @param height 当前进度值文本之后的进度条的高度
*/
public void setunreachedbarheight(float height) {
munreachedbarheight = height;
}
/**
* 设置进度值的最大值
* @param maxprogress 进度值的最大值
*/
public void setmax(int maxprogress) {
if (maxprogress > 0) {
this.mmaxprogress = maxprogress;
invalidate();
}
}
/**
* 设置当前进度值文本的后缀
* @param suffix 当前进度值文本的后缀
*/
public void setsuffix(string suffix) {
if (suffix == null) {
msuffix = "";
} else {
msuffix = suffix;
}
}
/**
* 获取当前进度值文本的后缀
*/
public string getsuffix() {
return msuffix;
}
/**
* 设置当前进度值文本的前缀
* @param prefix 当前进度值文本的前缀
*/
public void setprefix(string prefix) {
if (prefix == null)
mprefix = "";
else {
mprefix = prefix;
}
}
/**
* 获取当前进度值文本的前缀
*/
public string getprefix() {
return mprefix;
}
/**
* 设置进度条的当前进度值增加
* @param by 增加多少
*/
public void incrementprogressby(int by) {
if (by > 0) {
setprogress(getprogress() + by);
}
if (mlistener != null) {
//回调onprogresschange()方法来处理进度值变化后的事件
mlistener.onprogresschange(getprogress(), getmax());
}
}
/**
* 设置当前进度值
*
* @param progress 当前进度值
*/
public void setprogress(int progress) {
if (progress <= getmax() && progress >= 0) {
this.mcurrentprogress = progress;
invalidate();
}
}
@override
protected parcelable onsaveinstancestate() {
final bundle bundle = new bundle();
bundle.putparcelable(instance_state, super.onsaveinstancestate());
bundle.putint(instance_text_color, gettextcolor());
bundle.putfloat(instance_text_size, getprogresstextsize());
bundle.putfloat(instance_reached_bar_height, getreachedbarheight());
bundle.putfloat(instance_unreached_bar_height, getunreachedbarheight());
bundle.putint(instance_reached_bar_color, getreachedbarcolor());
bundle.putint(instance_unreached_bar_color, getunreachedbarcolor());
bundle.putint(instance_max, getmax());
bundle.putint(instance_progress, getprogress());
bundle.putstring(instance_suffix, getsuffix());
bundle.putstring(instance_prefix, getprefix());
bundle.putboolean(instance_text_visibility, getprogresstextvisibility());
return bundle;
}
@override
protected void onrestoreinstancestate(parcelable state) {
if (state instanceof bundle) {
final bundle bundle = (bundle) state;
mtextcolor = bundle.getint(instance_text_color);
mtextsize = bundle.getfloat(instance_text_size);
mreachedbarheight = bundle.getfloat(instance_reached_bar_height);
munreachedbarheight = bundle.getfloat(instance_unreached_bar_height);
mreachedbarcolor = bundle.getint(instance_reached_bar_color);
munreachedbarcolor = bundle.getint(instance_unreached_bar_color);
initializepainters();
setmax(bundle.getint(instance_max));
setprogress(bundle.getint(instance_progress));
setprefix(bundle.getstring(instance_prefix));
setsuffix(bundle.getstring(instance_suffix));
setprogresstextvisibility(bundle.getboolean(instance_text_visibility) ? visible : invisible);
super.onrestoreinstancestate(bundle.getparcelable(instance_state));
return;
}
super.onrestoreinstancestate(state);
}
/**
* dp转px
*/
public float dp2px(float dp) {
final float scale = getresources().getdisplaymetrics().density;
return dp * scale + 0.5f;
}
/**
* sp转px
*/
public float sp2px(float sp) {
final float scale = getresources().getdisplaymetrics().scaleddensity;
return sp * scale;
}
/**
* 设置是否绘制当前进度值文本
*/
public void setprogresstextvisibility(progresstextvisibility visibility) {
mifdrawtext = visibility == visible;
invalidate();
}
/**
* 获取是否绘制当前进度值文本
*/
public boolean getprogresstextvisibility() {
return mifdrawtext;
}
/**
* 设置进度值变化时的监听器
*/
public void setonprogressbarlistener(onprogressbarlistener listener) {
mlistener = listener;
}
}

如以上代码所示:

在自定义numberprogressbar控件的构造方法中,去获取了全部设置好了的自定义属性值,如果没有设置则使用默认的自定义属性值。

然后先重写onmeasure(int widthmeasurespec, int heightmeasurespec)方法,来确定自定义numberprogressbar控件的大小。

接着重写ondraw()方法,进行绘制自定义的带数字的进度条。

第三步、将自定义带数字的进度条添加到布局文件中

在res/layout目录下定义一个activity_main.xml文件,res/layout/activity_main.xml定义代码如下所示:

<linearlayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingleft="@dimen/activity_horizontal_margin"
android:paddingright="@dimen/activity_horizontal_margin"
android:paddingtop="@dimen/activity_vertical_margin"
android:paddingbottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.daimajia.numberprogressbar.example.mainactivity=">
<com.daimajia.numberprogressbar.numberprogressbar
android:id="@+id/numberbar1"
android:layout_width="wrap_content"
android:padding="20dp"
custom:progress_current="0"
style="@style/numberprogressbar_default"
android:layout_height="wrap_content" />
<com.daimajia.numberprogressbar.numberprogressbar
android:id="@+id/numberbar2"
android:layout_height="wrap_content"
android:padding="20dp"
custom:progress_current="20"
android:layout_width="match_parent"
style="@style/numberprogressbar_passing_green"
/>
<com.daimajia.numberprogressbar.numberprogressbar
android:id="@+id/numberbar3"
android:layout_margin="20dp"
style="@style/numberprogressbar_relax_blue"
custom:progress_current="30"
android:layout_height="wrap_content" />
<com.daimajia.numberprogressbar.numberprogressbar
android:id="@+id/numberbar4"
android:layout_width="wrap_content"
android:layout_margin="20dp"
style="@style/numberprogressbar_grace_yellow"
custom:progress_current="40"
android:layout_height="wrap_content" />
<com.daimajia.numberprogressbar.numberprogressbar
android:id="@+id/numberbar5"
android:layout_width="wrap_content"
android:layout_margin="20dp"
custom:progress_current="50"
style="@style/numberprogressbar_warning_red"
android:layout_height="wrap_content" />
<com.daimajia.numberprogressbar.numberprogressbar
android:id="@+id/numberbar6"
android:layout_width="wrap_content"
android:layout_margin="20dp"
style="@style/numberprogressbar_funny_orange"
custom:progress_current="60"
android:layout_height="wrap_content" />
<com.daimajia.numberprogressbar.numberprogressbar
android:id="@+id/numberbar7"
android:layout_width="wrap_content"
android:layout_margin="20dp"
style="@style/numberprogressbar_beauty_red"
custom:progress_current="70"
android:layout_height="wrap_content" />
<com.daimajia.numberprogressbar.numberprogressbar
android:id="@+id/numberbar8"
android:layout_width="wrap_content"
android:layout_margin="20dp"
style="@style/numberprogressbar_twinkle_night"
custom:progress_current="80"
android:layout_height="wrap_content" />
<com.daimajia.numberprogressbar.numberprogressbar
android:id="@+id/numberbar9"
android:layout_width="wrap_content"
android:layout_margin="20dp"
custom:progress_current="20"
custom:progress_max="100"
custom:progress_unreached_color="#ff530d"
custom:progress_reached_color="#6dbcdb"
custom:progress_text_size="10sp"
custom:progress_text_color="#ecf0f1"
custom:progress_reached_bar_height="1.5dp"
custom:progress_unreached_bar_height="0.75dp"
android:layout_height="wrap_content" />
</linearlayout>

第四步、编写activity加载布局文件,显示自定义的带数字的进度条

mainactity的代码如下所示:

package com.daimajia.numberprogressbar.example;
import android.app.activity;
import android.graphics.color;
import android.os.bundle;
import android.widget.toast;
import com.daimajia.numberprogressbar.numberprogressbar;
import com.daimajia.numberprogressbar.onprogressbarlistener;
import java.util.timer;
import java.util.timertask;
public class mainactivity extends activity implements onprogressbarlistener {
private timer timer;
private numberprogressbar bnp;
private numberprogressbar bnp9;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
bnp = (numberprogressbar)findviewbyid(r.id.numberbar1);
bnp.setonprogressbarlistener(this);
bnp9 = (numberprogressbar)findviewbyid(r.id.numberbar9);
bnp9.setprefix("欧阳鹏:");
bnp9.setsuffix("% csdn");
bnp9.setprogresstextsize(20);
bnp9.setprogresstextcolor(color.yellow);
bnp9.setprogresstextvisibility(numberprogressbar.progresstextvisibility.visible);
bnp9.setunreachedbarcolor(color.red);
bnp9.setreachedbarheight(10);
bnp9.setreachedbarheight(5);
timer = new timer();
timer.schedule(new timertask() {
@override
public void run() {
runonuithread(new runnable() {
@override
public void run() {
bnp.incrementprogressby(1);
}
});
}
}, 1000, 100);
}
@override
protected void ondestroy() {
super.ondestroy();
timer.cancel();
}
@override
public void onprogresschange(int current, int max) {
if(current == max) {
toast.maketext(getapplicationcontext(), getstring(r.string.finish), toast.length_short).show();
bnp.setprogress(0);
}
}
}

显示出的效果图为:

Android自定义View实现带数字的进度条实例代码

Android自定义View实现带数字的进度条实例代码

Android自定义View实现带数字的进度条实例代码

看完介绍后,读者可以到以下地址去查看完整的项目代码

daimajia的github上该项目的原始地址

https://github.com/daimajia/numberprogressbar

这里还有另外一个numberprogresbar的例子,如下图所示

Android自定义View实现带数字的进度条实例代码

Android自定义View实现带数字的进度条实例代码

Android自定义View实现带数字的进度条实例代码

以上内容是小编给大家介绍的android自定义view实现带数字的进度条实例代码,希望对大家有所帮助!