Android自定义带动画的半圆环型进度效果
本文实例为大家分享了Android半圆环型进度效果的具体代码,供大家参考,具体内容如下
packagecom.newair.ondrawtext;
importandroid.animation.ValueAnimator;
importandroid.annotation.TargetApi;
importandroid.content.Context;
importandroid.content.res.TypedArray;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.graphics.RectF;
importandroid.graphics.Typeface;
importandroid.os.Build;
importandroid.util.AttributeSet;
importandroid.view.View;
importandroid.view.animation.OvershootInterpolator;
/**
*Createdbyouhimehimeon16/6/15.
*--------自定义控件-------
*/
publicclassCustomViewextendsView{
//画笔
privatePaintpaint;
privateRectFoval;
//圆弧颜色
privateintroundColor;
//进度颜色
privateintprogressColor;
//文字内容
privatebooleantextIsShow;
//字体大小
privatefloattextSize=14;
//文字颜色
privateinttextColor;
//最大进度
privateintmax=1000;
//当前进度
privateintprogress=300;
//圆弧宽度
privateintroundWidth=30;
privateintviewWidth;//宽度--控件所占区域
privatefloatnowPro=0;//用于动画
privateValueAnimatoranimator;
publicCustomView(Contextcontext){
super(context);
}
publicCustomView(Contextcontext,AttributeSetattrs){
super(context,attrs);
initAttrs(attrs,context);
}
publicCustomView(Contextcontext,AttributeSetattrs,intdefStyleAttr){
super(context,attrs,defStyleAttr);
initAttrs(attrs,context);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
publicCustomView(Contextcontext,AttributeSetattrs,intdefStyleAttr,intdefStyleRes){
super(context,attrs,defStyleAttr,defStyleRes);
initAttrs(attrs,context);
}
privatevoidinitAttrs(AttributeSetattr,Contextcontext){
TypedArrayarray=context.obtainStyledAttributes(attr,R.styleable.CustomView);
roundColor=array.getColor(R.styleable.CustomView_roundColor,Color.BLACK);//环形颜色
progressColor=array.getColor(R.styleable.CustomView_progressColor,Color.RED);//进度颜色
textIsShow=array.getBoolean(R.styleable.CustomView_textIsShow,false);//文字
textSize=array.getDimension(R.styleable.CustomView_textSize,14);//文字大小
textColor=array.getColor(R.styleable.CustomView_textColor,Color.BLACK);//文字颜色
roundWidth=array.getInt(R.styleable.CustomView_roundWidth,30);//圆环宽度
array.recycle();
//动画
animator=ValueAnimator.ofFloat(0,progress);
animator.setDuration(1800);
animator.setInterpolator(newOvershootInterpolator());
animator.addUpdateListener(newValueAnimator.AnimatorUpdateListener(){
@Override
publicvoidonAnimationUpdate(ValueAnimatoranimation){
nowPro=(float)animation.getAnimatedValue();
postInvalidate();
}
});
animator.start();
}
@Override
protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
finalintwidthSpecMode=MeasureSpec.getMode(widthMeasureSpec);
finalintwidthSpecSize=MeasureSpec.getSize(widthMeasureSpec);
if(widthSpecMode==MeasureSpec.AT_MOST){//可获得最大空间
setMeasuredDimension(widthMeasureSpec,(widthSpecSize/2)+(int)(Math.cos(20)*(widthSpecSize/2)));
}elseif(widthMeasureSpec==MeasureSpec.EXACTLY){//一般指精确值
setMeasuredDimension(widthMeasureSpec,(widthSpecSize/2)+(int)(Math.cos(20)*(widthSpecSize/2)));
}else{
setMeasuredDimension(widthMeasureSpec,(viewWidth/2)+(int)(Math.cos(20)*(viewWidth/2)));
}
}
@Override
protectedvoidonSizeChanged(intw,inth,intoldw,intoldh){
super.onSizeChanged(w,h,oldw,oldh);
viewWidth=w;//得到宽度以此来计算控件所占实际大小
//计算画布所占区域
oval=newRectF();
oval.left=roundWidth+getPaddingLeft();
oval.top=roundWidth+getPaddingTop();
oval.right=viewWidth-roundWidth-getPaddingRight();
oval.bottom=viewWidth-roundWidth-getPaddingBottom();
}
@Override
protectedvoidonDraw(Canvascanvas){
super.onDraw(canvas);
Paintpaint=newPaint();
paint.setAntiAlias(true);//设置画笔为无锯齿
paint.setColor(roundColor);//设置画笔颜色
paint.setStrokeWidth(roundWidth);//线宽
paint.setStyle(Paint.Style.STROKE);//空心
canvas.drawArc(oval,160,220,false,paint);//绘制圆弧
//画进度层
paint.setColor(progressColor);
paint.setStrokeWidth(roundWidth+1);
canvas.drawArc(oval,160,220*nowPro/max,false,paint);//绘制圆弧
if(textIsShow){
paint.setColor(textColor);
paint.setStrokeWidth(0);
paint.setTypeface(Typeface.DEFAULT);
paint.setTextSize(textSize*2);
floattextWidth=paint.measureText((int)((nowPro/(float)max)*100)+"%");
canvas.drawText((int)((nowPro/(float)max)*100)+"%",viewWidth/2-textWidth/2,viewWidth/2,paint);
}
}
privateintgetDefaultHeight(){
return0;
}
privateintgetDefaultWidth(){
return0;
}
publicintgetRoundColor(){
returnroundColor;
}
publicvoidsetRoundColor(introundColor){
this.roundColor=roundColor;
}
publicintgetProgressColor(){
returnprogressColor;
}
publicvoidsetProgressColor(intprogressColor){
this.progressColor=progressColor;
}
publicbooleangetText(){
returntextIsShow;
}
publicvoidsetText(booleantext){
this.textIsShow=text;
}
publicfloatgetTextSize(){
returntextSize;
}
publicvoidsetTextSize(floattextSize){
this.textSize=textSize;
}
publicintgetTextColor(){
returntextColor;
}
publicvoidsetTextColor(inttextColor){
this.textColor=textColor;
}
publicintgetMax(){
returnmax;
}
publicvoidsetMax(intmax){
this.max=max;
}
publicintgetProgress(){
returnprogress;
}
publicvoidsetProgress(intprogress){
this.progress=progress;
}
}
自定义属性
用法
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。