Android中怎么实现一个底部带刻度的进度条样式
这期内容当中小编将会给大家带来有关Android中怎么实现一个底部带刻度的进度条样式,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
10余年的泽普网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销的优势是能够根据用户设备显示端的尺寸不同,自动调整泽普建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联从事“泽普网站设计”,“泽普网站推广”以来,每个客户项目都认真落实执行。
首先得继承View,由于这个控件比较简单,我就没有搞那种在布局文件中设值的属性了,继承之后第一步,需要测量布局,得到画布的大小,这个值其实就是我们在布局文件中设置的控件的宽高。
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int realWidth = startMeasure(widthMeasureSpec); int realHeight = startMeasure(heightMeasureSpec); setMeasuredDimension(realWidth, realHeight); } private int startMeasure(int msSpec) { int result = 0; int mode = MeasureSpec.getMode(msSpec); int size = MeasureSpec.getSize(msSpec); if (mode == MeasureSpec.EXACTLY) { result = size; } else { result = PxUtils.dpToPx(400, mContext); } return result; }
这边拿到画布的大小,设置进度条显示的宽度,我这边设置的为画布宽度的80%
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mWidth = getWidth(); mHight = getHeight(); progressWidth = mWidth*0.8f; }
然后就是初始化画笔了,具体我就不多赘述了,我使用了五个画笔,分别是进度条背景底框,进度,刻度绘制,刻度下的字,当前数值的文字具体看代码。
private void initPaint() { //画进度条静态空心背景 paintProgressBackground = new Paint(); paintProgressBackground.setAntiAlias(true); paintProgressBackground.setStyle(Paint.Style.STROKE); paintProgressBackground.setColor(getResources().getColor(R.color.progressborder)); paintProgressBackground.setDither(true); //画进度的画笔,实心 paintProgress = new Paint(); paintProgress.setAntiAlias(true); paintProgress.setStyle(Paint.Style.FILL); paintProgress.setColor(getResources().getColor(R.color.progressfill)); paintProgress.setDither(true); //画刻度的画笔 paintNum = new Paint(); paintNum.setAntiAlias(true); paintNum.setColor(getResources().getColor(R.color.progresstext)); paintNum.setStrokeWidth(2); paintNum.setStyle(Paint.Style.FILL); paintNum.setDither(true); //画刻度数值的画笔 paintTikeStr = new Paint(); paintTikeStr.setAntiAlias(true); paintTikeStr.setStyle(Paint.Style.FILL); paintTikeStr.setTextAlign(Paint.Align.LEFT); paintTikeStr.setColor(getResources().getColor(R.color.progresstext)); paintTikeStr.setTextSize(16); //画数值的画笔 paintText = new Paint(); paintText.setAntiAlias(true); paintText.setColor(getResources().getColor(R.color.progresstext)); paintText.setStrokeWidth(1); paintText.setStyle(Paint.Style.FILL);//实心画笔 paintText.setDither(true); }
接下来就是onDraw方法进行绘制了,用canvas绘制,绘制的起点是你画布的左上角,横向为x,纵向为y,所以绘制的时候只要确定好x,y的坐标,那就好画了。
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //进度条的底框 canvas.drawRect(0+leftPadding,0,progressWidth+leftPadding,progressHeight,paintProgressBackground); //进度条的当前进度 canvas.drawRect(0+leftPadding,0,progressWidth*percent+leftPadding,progressHeight,paintProgress); drawScale(canvas,percent); drawText(canvas,percent); }
进度条其实很容易绘制,就是画两个矩形,一个地没有进度的矩形,另一个是当前进度的矩形就行了 ,percent是当前进度的百分比,之所以加个leftPadding是因为如果从0开始就顶到画布左边了,后面画刻度下的字体就会存在截断现象,显示不全。drawRect的每个参数是什么意思我就不多说了,这个很多文章都有介绍。
/** * 绘制刻度和刻度下的数字 * @param canvas * @param percent */ private void drawScale(Canvas canvas,float percent){ float span = progressWidth/8f; for (int i=0;i<9;i++){ canvas.save(); //记录画布状态 canvas.translate(span*i+leftPadding, 0); canvas.drawLine(0,numY,0,numY+10,paintNum); String text = String.valueOf(tikeStrArray[i]); Paint.FontMetricsInt fontMetrics = paintTikeStr.getFontMetricsInt(); float baseline = ((numY + 20) + (fontMetrics.bottom - fontMetrics.top) / 2); canvas.drawText(text, -getTextViewLength(paintTikeStr, text) / 2, baseline, paintTikeStr); canvas.restore(); } }
跟其他进度条不同的是,带刻度的最重要是怎么绘制刻度了,我这边默认总共9个刻度,可以自行修改,怎么画出刻度线,重要的就是通过canvas的平移,translate来实现,x为每次绘制的位置,画一条就会平移一段距离再画一条,原理就是这样。numY的参数其实就是与画布顶点的距离,由于我的进度条设置的是30的高度,刻度要紧挨着进度底部,所以开始画的y坐标也是30,+10是绘制刻度线的长度,所以刻度线长度就是10。刻度下的文字,也是获取文字的宽度,取中心位置。
private float getTextViewLength(Paint paint, String text) { if (TextUtils.isEmpty(text)) return 0; float textLength = paint.measureText(text); return textLength; }
接下来就是绘制右边显示当前数组的文字了,只要确定好位置,就很简单了。
* 绘制显示的数值 * @param canvas * @param percent */ private void drawText(Canvas canvas, float percent) { if (TextUtils.isEmpty(unit)) return; float length; paintText.setTextSize(16); numerical = StringUtil.floatFormat(startNum + (maxNum - startNum) * percent) + unit; length = paintText.measureText(numerical); canvas.drawText(numerical,progressWidth+leftPadding+textSpan , length / 2, paintText); }
显示的值是多少,也很简单算出来,具体怎么算的再上面的代码中。
基本上这个进度条就完工了,由于是做记录,就没写的很详细了,下面贴一下全部代码。
package com.anderson.dashboardview.view; import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.text.TextUtils;import android.util.AttributeSet;import android.util.Log;import android.view.View;import com.anderson.dashboardview.R;import com.anderson.dashboardview.util.PxUtils;import com.anderson.dashboardview.util.StringUtil;/** * 带刻度的进度条 */public class HorizontalProgressBar extends View { private Context mContext; private Paint paintProgressBackground; private Paint paintProgress; private Paint paintNum; private Paint paintTikeStr; private int mWidth, mHight; private float percent = 0; private float progressWidth = 320; private float startNum;//开始的数值 private float maxNum;//最大的数值 private float[] tikeStrArray = null; private int tikeGroup; private int mTikeCount;//刻度的个数 private Paint paintText; private String unit = "m";//显示单位 private String numerical; private int leftPadding = 25;//左边距 private int textSpan = 2;//数值文字与进度条的间隔 private int progressHeight = 30;//进度条高度 private float numY = 30;//在进度条底部绘制,相当于进度条的高度 public HorizontalProgressBar(Context context) { super(context); init(context); } public HorizontalProgressBar(Context context, AttributeSet attrs) { super(context, attrs); init(context); } public HorizontalProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private void init(Context context) { mContext = context; initPaint(); } private void initPaint() { //画进度条静态空心背景 paintProgressBackground = new Paint(); paintProgressBackground.setAntiAlias(true); paintProgressBackground.setStyle(Paint.Style.STROKE); paintProgressBackground.setColor(getResources().getColor(R.color.progressborder)); paintProgressBackground.setDither(true); //画进度的画笔,实心 paintProgress = new Paint(); paintProgress.setAntiAlias(true); paintProgress.setStyle(Paint.Style.FILL); paintProgress.setColor(getResources().getColor(R.color.progressfill)); paintProgress.setDither(true); //画刻度的画笔 paintNum = new Paint(); paintNum.setAntiAlias(true); paintNum.setColor(getResources().getColor(R.color.progresstext)); paintNum.setStrokeWidth(2); paintNum.setStyle(Paint.Style.FILL); paintNum.setDither(true); //画刻度数值的画笔 paintTikeStr = new Paint(); paintTikeStr.setAntiAlias(true); paintTikeStr.setStyle(Paint.Style.FILL); paintTikeStr.setTextAlign(Paint.Align.LEFT); paintTikeStr.setColor(getResources().getColor(R.color.progresstext)); paintTikeStr.setTextSize(16); //画数值的画笔 paintText = new Paint(); paintText.setAntiAlias(true); paintText.setColor(getResources().getColor(R.color.progresstext)); paintText.setStrokeWidth(1); paintText.setStyle(Paint.Style.FILL);//实心画笔 paintText.setDither(true); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mWidth = getWidth(); mHight = getHeight(); progressWidth = mWidth*0.8f; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int realWidth = startMeasure(widthMeasureSpec); int realHeight = startMeasure(heightMeasureSpec); setMeasuredDimension(realWidth, realHeight); } private int startMeasure(int msSpec) { int result = 0; int mode = MeasureSpec.getMode(msSpec); int size = MeasureSpec.getSize(msSpec); if (mode == MeasureSpec.EXACTLY) { result = size; } else { result = PxUtils.dpToPx(400, mContext); } return result; } private float getTextViewLength(Paint paint, String text) { if (TextUtils.isEmpty(text)) return 0; float textLength = paint.measureText(text); return textLength; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //进度条的底框 canvas.drawRect(0+leftPadding,0,progressWidth+leftPadding,progressHeight,paintProgressBackground); //进度条的当前进度 canvas.drawRect(0+leftPadding,0,progressWidth*percent+leftPadding,progressHeight,paintProgress); drawScale(canvas,percent); drawText(canvas,percent); } /** * 绘制刻度和刻度下的数字 * @param canvas * @param percent */ private void drawScale(Canvas canvas,float percent){ float span = progressWidth/8f; for (int i=0;i<9;i++){ canvas.save(); //记录画布状态 canvas.translate(span*i+leftPadding, 0); canvas.drawLine(0,numY,0,numY+10,paintNum); String text = String.valueOf(tikeStrArray[i]); Paint.FontMetricsInt fontMetrics = paintTikeStr.getFontMetricsInt(); float baseline = ((numY + 20) + (fontMetrics.bottom - fontMetrics.top) / 2); canvas.drawText(text, -getTextViewLength(paintTikeStr, text) / 2, baseline, paintTikeStr); canvas.restore(); } } /** * 绘制显示的数值 * @param canvas * @param percent */ private void drawText(Canvas canvas, float percent) { if (TextUtils.isEmpty(unit)) return; float length; paintText.setTextSize(16); numerical = StringUtil.floatFormat(startNum + (maxNum - startNum) * percent) + unit; length = paintText.measureText(numerical); canvas.drawText(numerical,progressWidth+leftPadding+textSpan , length / 2, paintText); } /** * 设置百分比 * @param percent */ public void setPercent(int percent) { this.percent = percent / 100f; invalidate(); } /** * 设置起始值 * @param startNum */ public void setStartNum(float startNum) { this.startNum = startNum; } /** * 设置最大值 * @param maxNum */ public void setMaxNum(float maxNum) { this.maxNum = maxNum; float[] tikeintArray = new float[9]; //默认8个大刻度 tikeintArray[0] = startNum; for (int i = 1;i<8;i++){ tikeintArray[i] = tikeintArray[i-1]+((maxNum-startNum)/8); } tikeintArray[8] = maxNum; setTikeArray(tikeintArray); } public void setTikeArray(float[] array){ this.tikeStrArray = array; tikeGroup = 5; // 默认1个长刻度间隔4个短刻度,加起来一组5 if (tikeStrArray != null && tikeStrArray.length != 0) { //根据需要绘制的刻度数组大小计算刻度总数 mTikeCount = (tikeStrArray.length - 1) * tikeGroup + 1; } else { tikeStrArray = new float[0]; mTikeCount = 36; } }}
上述就是小编为大家分享的Android中怎么实现一个底部带刻度的进度条样式了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。
网站栏目:Android中怎么实现一个底部带刻度的进度条样式
标题来源:http://scyanting.com/article/gdppid.html