最近闲来无事,自定义了一个环状进度条,话不多说直接上代码 :
public class CircleProgressVIEw extends VIEw{ private Paint mCirPaint; private Paint marcPaint; private Paint mTextPaint; private float radius=200; private int textsize=60; private int progress=68; private int stokeWIDth=10; private int circlecolor=color.GRAY; private int arccolor=color.GREEN; private int textcolor=color.BLACK; private int speed=0; public CircleProgressVIEw(Context context) { super(context); } public CircleProgressVIEw(Context context,AttributeSet attrs,int defStyleAttr) { super(context,attrs,defStyleAttr); } public CircleProgressVIEw(Context context,AttributeSet attrs) { super(context,attrs); } public voID seTradius(float radius){ this.radius=radius; invalIDate(); } public voID setTextSize(int textsize){ this.textsize=textsize; invalIDate(); } public voID setProgress(int progress){ this.progress=progress; } public voID setStokewIDth(int stokeWIDth){ this.stokeWIDth=stokeWIDth; invalIDate(); } public voID setcolor(int circlecolor,int arccolor,int textcolor){ this.circlecolor=circlecolor; this.arccolor=arccolor; this.textcolor=textcolor; invalIDate(); } public voID setSpeed(int speed){ this.speed=speed; } private voID init() { mCirPaint=new Paint(); mCirPaint.setcolor(circlecolor); mCirPaint.setAntiAlias(true); mCirPaint.setStyle(Paint.Style.stroke); mCirPaint.setstrokeWIDth(stokeWIDth); marcPaint=new Paint(); marcPaint.setcolor(arccolor); marcPaint.setAntiAlias(true); marcPaint.setStyle(Paint.Style.stroke); marcPaint.setstrokeWIDth(stokeWIDth); mTextPaint=new Paint(); mTextPaint.setcolor(textcolor); mTextPaint.setTextSize(textsize); mTextPaint.setAntiAlias(true); } @OverrIDe protected voID onDraw(Canvas canvas) { super.onDraw(canvas); init(); float centerX=getWIDth()/2; float centerY=getHeight()/2; canvas.drawCircle(centerX,centerY,radius,mCirPaint); canvas.drawArc(centerX-radius,centerY-radius,centerX+radius,centerY+radius,-90,progress*360/100,false,marcPaint); canvas.drawText(progress+"%",centerX-(mTextPaint.measureText(progress+"%"))/2,centerY+textsize/2,mTextPaint); } @OverrIDe protected voID onMeasure(int wIDthMeasureSpec,int heightmeasureSpec) { super.onMeasure(wIDthMeasureSpec,heightmeasureSpec); if (speed!=0){ startProgress(); } } public voID startProgress(){ final int preProgress=progress; new CountDownTimer(preProgress * speed,speed) { @OverrIDe public voID onTick(long l) { setProgress(preProgress-(int) (l/speed)); invalIDate(); } @OverrIDe public voID onFinish() { setProgress(preProgress); invalIDate(); this.cancel(); } }.start(); }}
相关用法:
setProgress(progress);//设置进度
seTradius(300);//设置半径
setStokewIDth(60);//设置环宽
setTextSize(80);//设置文字进度大小
setcolor(color.GRAY,color.RED,color.BLUE);//设置颜色(环的颜色,进度条的颜色,文字进度的字体颜色)
setSpeed(20);//设置动画速度,这里的数值是每次进度加一所用时间,所以数值越小动画速度越快
测试代码:
mCircleProgressVIEw= (CircleProgressVIEw) findVIEwByID(R.ID.circle_progress); mCircleProgressVIEw.setProgress(progress); mCircleProgressVIEw.seTradius(300); mCircleProgressVIEw.setStokewIDth(60); mCircleProgressVIEw.setTextSize(80); mCircleProgressVIEw.setcolor(color.GRAY,color.BLUE); mCircleProgressVIEw.setSpeed(20);
测试效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android自定义带加载动画效果的环状进度条全部内容,希望文章能够帮你解决Android自定义带加载动画效果的环状进度条所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)