Android自定义圆角矩形进度条2

Android自定义圆角矩形进度条2,第1张

概述效果图: 或 方法讲解:(1)invalidate()方法invalidate()是用来刷新View的,必须是在UI线程中进行工作。比如在修改某个view的显示时,调用invalidate()才能看到重新绘制的界面。invalidate()的调用是把之前的旧的view从主UI线程队列中pop掉。一般在自定义控件中会用到这个方法。(2)R

效果图:

  或 

方法讲解:

(1)invalIDate()方法

invalIDate()是用来刷新VIEw的,必须是在UI线程中进行工作。比如在修改某个vIEw的显示时, 调用invalIDate()才能看到重新绘制的界面。invalIDate()的调用是把之前的旧的vIEw从主UI线程队列中pop掉。一般在自定义控件中会用到这个方法。

(2)RectF方法的应用

RectF是用来绘画矩形的方法。 

RectF(left,top,right,bottom),四个参数的含义分别是父控件距离矩形左上右下边距的距离,以下用图来说明:

drawRoundRect方法是用来绘制圆角矩形的,它的参数如下: 
参数说明 

rect:RectF对象。 
rx:x方向上的圆角半径。 
ry:y方向上的圆角半径。 
paint:绘制时所使用的画笔。

(3)onMeasure方法

指定自定义控件在屏幕上的大小,onMeasure方法的两个参数是由上一层控件 传入的大小,而且是模式和尺寸混合在一起的数值,需要MeasureSpec.getMode(wIDthMeasureSpec) 得到模式,MeasureSpec.getSize(wIDthMeasureSpec)得到尺寸。

onMeasure的几种模式分别为EXACTLY,AT_MOST,UnspecIFIED。

[1]MeasureSpec.EXACTLY

MeasureSpec.EXACTLY是精确尺寸,当我们将控件的layout_wIDth或layout_height指定为具体数值时如andorID:layout_wIDth=”50dip”,或者为FILL_PARENT是,都是控件大小已经确定的情况,都是精确尺寸。

[2]MeasureSpec.AT_MOST

MeasureSpec.AT_MOST是最大尺寸,当控件的layout_wIDth或layout_height指定为WRAP_CONTENT时,控件大小一般随着控件的子空间或内容进行变化,此时控件尺寸只要不超过父控件允许的最大尺寸即可。因此,此时的mode是AT_MOST,size给出了父控件允许的最大尺寸。

[3]MeasureSpec.UnspecIFIED

MeasureSpec.UnspecIFIED是未指定尺寸,这种情况不多,一般都是父控件是AdapterVIEw,通过measure方法传入的模式。

实现步骤:

a、在values文件夹下新建attrs.xml,内容如下:

<?xml version="1.0" enCoding="utf-8"?><resources><declare-styleable name="CircleProgressbar"><attr name="croundcolor" format="color"/><attr name="croundProgresscolor" format="color"/><attr name="cfillcolor" format="color"/><attr name="crounDWIDth" format="dimension"></attr><attr name="croundProgressWIDth" format="dimension"></attr> <attr name="ctextcolor" format="color" /> <attr name="ctextSize" format="dimension" /> <attr name="cnumberSize" format="dimension" /> <attr name="cparaLable" format="string" /><attr name="cunitLable" format="string" /> </declare-styleable><declare-styleable name="RoundRectProgressbar"><attr name="cbarRoundcolor" format="color"/><attr name="cbarProgresscolor" format="color"/><attr name="cbarFillcolor" format="color"/><attr name="cbarOrIEntation">  <enum name="HORIZONTAL" value="0"></enum>  <enum name="VERTICAL" value="1"></enum></attr></declare-styleable></resources>

  


b、新建RoundRectProgressbar类继承VIEw

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.util.AttributeSet;import androID.vIEw.VIEw;/*** 自定义圆角矩形进度条vIEw** @author xl*/public class RoundRectProgressbar extends VIEw {private final static String TAG = RoundRectProgressbar.class.getSimplename();/*** 画笔对象的引用*/private Paint paint;/*** 圆角环的颜色*/private int roundcolor;/*** 进度的颜色*/private int fillProgresscolor;/*** 填充的颜色*/private int fillcolor;/*** 圆角矩形宽度*/private int rounDWIDth;/*** 圆角矩形高度*/private int roundHeight;/*** 进度条方向,0水平,1垂直*/private int barOrIEntation;/*** 进度条最大值*/private float max = 100;/*** 进度条当前值*/private float progress = 30;public RoundRectProgressbar(Context context) {this(context, null);}public RoundRectProgressbar(Context context, AttributeSet attrs) {this(context, attrs, 0);}public RoundRectProgressbar(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);//获取画笔paint = new Paint();TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundRectProgressbar);//获取自定义属性和默认值roundcolor = mTypedArray.getcolor(R.styleable.RoundRectProgressbar_cbarRoundcolor, color.RED);fillProgresscolor = mTypedArray.getcolor(R.styleable.RoundRectProgressbar_cbarProgresscolor, color.GREEN);fillcolor = mTypedArray.getcolor(R.styleable.RoundRectProgressbar_cbarFillcolor, color.BLUE);barOrIEntation = mTypedArray.getInt(R.styleable.RoundRectProgressbar_cbarOrIEntation, 0);//回收TypedArray资源mTypedArray.recycle();}@OverrIDeprotected voID onDraw(Canvas canvas) {super.onDraw(canvas);//设置抗锯齿效果paint.setAntiAlias(true);//设置画笔颜色paint.setcolor(roundcolor);//进度方向if (barOrIEntation == 0) {//水平,向右try {int round = roundHeight / 2;//RectF:绘制矩形,四个参数分别是left,top,right,bottom,类型是单精度浮点数RectF rf = new RectF(0, 0, rounDWIDth, roundHeight);//绘制圆角矩形,背景色为画笔颜色canvas.drawRoundRect(rf, round, round, paint);//设置progress内部是灰色paint.setcolor(fillcolor);RectF rectBlackBg = new RectF(2, 2, rounDWIDth - 2, roundHeight - 2);canvas.drawRoundRect(rectBlackBg, round, round, paint);//设置进度条进度及颜色float section = progress / max;RectF rectProgressBg = new RectF(2, 2, (rounDWIDth - 2) * section, roundHeight - 2);if (section != 0.0f) {paint.setcolor(fillProgresscolor);} else {paint.setcolor(color.transparent);}canvas.drawRoundRect(rectProgressBg, round, round, paint);} catch (Exception e) {e.printstacktrace();}} else {//垂直,向上try {int round = rounDWIDth / 2;//RectF:绘制矩形,四个参数分别是left,top,right,bottom,类型是单精度浮点数RectF rf = new RectF(0, 0, rounDWIDth, roundHeight);//绘制圆角矩形,背景色为画笔颜色canvas.drawRoundRect(rf, round, round, paint);//设置progress内部是灰色paint.setcolor(fillcolor);RectF rectBlackBg = new RectF(2, 2, rounDWIDth - 2, roundHeight - 2);canvas.drawRoundRect(rectBlackBg, round, round, paint);//设置进度条进度及颜色float section = progress / max;RectF rectProgressBg = new RectF(2, roundHeight - 2 - (roundHeight - 4) * section, rounDWIDth - 2, roundHeight - 2);if (section != 0.0f) {paint.setcolor(fillProgresscolor);} else {paint.setcolor(color.transparent);}canvas.drawRoundRect(rectProgressBg, round, round, paint);} catch (Exception e) {e.printstacktrace();}}}/*** dip转px** @param dip* @return*/private int diptopx(int dip) {float scale = getContext().getResources().getdisplayMetrics().density;//加0.5是为了四舍五入return (int) (dip * scale + 0.5f * (dip >= 0 ? 1 : -1));}/*** 指定自定义控件在屏幕上的大小,onMeasure方法的两个参数是由上一层控件* 传入的大小,而且是模式和尺寸混合在一起的数值,需要MeasureSpec.getMode(wIDthMeasureSpec)* 得到模式,MeasureSpec.getSize(wIDthMeasureSpec)得到尺寸*/@OverrIDeprotected voID onMeasure(int wIDthMeasureSpec, int heightmeasureSpec) {super.onMeasure(wIDthMeasureSpec, heightmeasureSpec);int wIDthSpecMode = MeasureSpec.getMode(wIDthMeasureSpec);int heightSpecMode = MeasureSpec.getMode(heightmeasureSpec);int wIDthSpecsize = MeasureSpec.getSize(wIDthMeasureSpec);int heightSpecsize = MeasureSpec.getSize(heightmeasureSpec);//MeasureSpec.EXACTLY,精确尺寸if (wIDthSpecMode == MeasureSpec.EXACTLY || wIDthSpecMode == MeasureSpec.AT_MOST) {rounDWIDth = wIDthSpecsize;} else {rounDWIDth = 0;}if (heightSpecMode == MeasureSpec.EXACTLY || heightSpecMode == MeasureSpec.AT_MOST) {roundHeight = heightSpecsize;} else {roundHeight = 0;}//MeasureSpec.AT_MOST,最大尺寸,只要不超过父控件允许的最大尺寸即可,MeasureSpec.UnspecIFIED未指定尺寸//if (heightSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.UnspecIFIED) {// roundHeight = diptopx(20);//} else {// roundHeight = heightSpecsize;//}//设置控件实际大小setMeasuredDimension(rounDWIDth, roundHeight);}/*** 设置进度** @param progress*/public synchronized voID setProgress(float progress) {if (progress < 0) {throw new IllegalArgumentException("value can not be negative");}if (progress > max) {this.progress = max;} else {this.progress = progress;}postInvalIDate();}/*** 设置最大值** @param max*/public synchronized voID setMax(float max) {if (max < 0) {throw new IllegalArgumentException("value can not be negative");}this.max = max;}}

  


c、布局文件中引用activity_main.xml

<ups.invt.com.vIEw.RoundRectProgressbarandroID:ID="@+ID/bar"androID:layout_wIDth="20dp"androID:layout_height="100dp"androID_custom:cbarRoundcolor="@color/transparent"androID_custom:cbarFillcolor="@color/white"androID_custom:cbarProgresscolor="@color/bar_fill_color"androID_custom:cbarOrIEntation="VERTICAL"androID:layout_centerInParent="true"/>

  


d、MainActivity.java中设置进度

progress = (RoundRectProgressbar) findVIEwByID(R.ID.bar);progress.setMax(100);progress.setProgress(80);


完!!!
————————————————

参考于:https://blog.csdn.net/xialong_927/article/details/86596932

总结

以上是内存溢出为你收集整理的Android自定义圆角矩形进度条2全部内容,希望文章能够帮你解决Android自定义圆角矩形进度条2所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存