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

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

概述第一步、效果展示图1、蓝色的进度条图2、红色的进度条图3、多条颜色不同的进度条

第一步、效果展示

图1、蓝色的进度条

图2、红色的进度条

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

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

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

0、项目结构


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

 

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

 

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

1、绘制步骤分析

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


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,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);/*** Reached bar area rect.* 当前进度值之前文本的进度条(长方形)*/private RectF mReachedRectF = new RectF(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,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();}@OverrIDeprotected int getSuggestedMinimumWIDth() {return (int) mTextSize;}@OverrIDeprotected int getSuggestedMinimumHeight() {return Math.max((int) mTextSize,Math.max((int) mReachedbarHeight,(int) mUnreachedbarHeight));}@OverrIDeprotected 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;}@OverrIDeprotected 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();}}@OverrIDeprotected 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;}@OverrIDeprotected 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定义代码如下所示:

<linearLayoutxmlns: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.NumberProgressbarandroID:ID="@+ID/numberbar1"androID:layout_wIDth="wrap_content"androID:padding="20dp"custom:progress_current="0"androID:layout_height="wrap_content" /><com.daimajia.numberprogressbar.NumberProgressbarandroID:ID="@+ID/numberbar2"androID:layout_height="wrap_content"androID:padding="20dp"custom:progress_current="20"androID:layout_wIDth="match_parent"/><com.daimajia.numberprogressbar.NumberProgressbarandroID:ID="@+ID/numberbar3"androID:layout_margin="20dp"custom:progress_current="30"androID:layout_height="wrap_content" /><com.daimajia.numberprogressbar.NumberProgressbarandroID:ID="@+ID/numberbar4"androID:layout_wIDth="wrap_content"androID:layout_margin="20dp"custom:progress_current="40"androID:layout_height="wrap_content" /><com.daimajia.numberprogressbar.NumberProgressbarandroID:ID="@+ID/numberbar5"androID:layout_wIDth="wrap_content"androID:layout_margin="20dp"custom:progress_current="50"androID:layout_height="wrap_content" /><com.daimajia.numberprogressbar.NumberProgressbarandroID:ID="@+ID/numberbar6"androID:layout_wIDth="wrap_content"androID:layout_margin="20dp"custom:progress_current="60"androID:layout_height="wrap_content" /><com.daimajia.numberprogressbar.NumberProgressbarandroID:ID="@+ID/numberbar7"androID:layout_wIDth="wrap_content"androID:layout_margin="20dp"custom:progress_current="70"androID:layout_height="wrap_content" /><com.daimajia.numberprogressbar.NumberProgressbarandroID:ID="@+ID/numberbar8"androID:layout_wIDth="wrap_content"androID:layout_margin="20dp"custom:progress_current="80"androID:layout_height="wrap_content" /><com.daimajia.numberprogressbar.NumberProgressbarandroID: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;@OverrIDeprotected 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() {@OverrIDepublic voID run() {runOnUiThread(new Runnable() {@OverrIDepublic voID run() {bnp.incrementProgressBy(1);}});}},1000,100);}@OverrIDeprotected voID onDestroy() {super.onDestroy();timer.cancel();}@OverrIDepublic voID onProgressChange(int current,int max) {if(current == max) {Toast.makeText(getApplicationContext(),getString(R.string.finish),Toast.LENGTH_SHORT).show();bnp.setProgress(0);}}}

显示出的效果图为:

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

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

https://github.com/daimajia/NumberProgressBar

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


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

总结

以上是内存溢出为你收集整理的Android自定义View实现带数字的进度条实例代码全部内容,希望文章能够帮你解决Android自定义View实现带数字的进度条实例代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1141898.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存