Android自定義View實現(xiàn)圓弧進度效果逐步完成過程
涉及到的知識Canvas(畫布),Paint(畫筆),自定義控件等有三種:一個是直接從View繼承,完全的自定義;二是對原有控件進行改造,達到想要的效果;還有一種自定義的組合控件,根據(jù)自己的需要將已有的控件組合起來達到效果。我對自定義視圖也略知一二,就簡單記錄一下自己對自定義視圖的學(xué)習(xí)吧(繼承自View)過程,方便日后閱讀。

技術(shù)實現(xiàn)
1.ArcView繼承自View
2.Canvas(畫布)
3.Paint(畫筆)
效果圖:類似于QQ的計步效果

1.繼承自View
重寫3個構(gòu)造方法(新的API中的構(gòu)造方法是4個)
public ArcView(Context context) {
this(context,null);
}
public ArcView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public ArcView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); //init();
}
重寫View的OnDraw方法
@SuppressLint("DrawAllocation")
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
centerX=getWidth()/2;
centerY=getHeight()/2;
//初始化paint
initPaint();
//繪制弧度
drawArc(canvas);
//繪制文本
drawText(canvas);
}
注:這里的paint初始化我放在了onDraw方法中進行的,當(dāng)然你也可以放在有三個參數(shù)的構(gòu)造方法中初始化。
2.Paint初始化
圓弧的畫筆mArcPaint
//圓弧的paint
mArcPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
//抗鋸齒
mArcPaint.setAntiAlias(true);
mArcPaint.setColor(Color.parseColor("#666666"));
//設(shè)置透明度(數(shù)值為0-255)
mArcPaint.setAlpha(100);
//設(shè)置畫筆的畫出的形狀
mArcPaint.setStrokeJoin(Paint.Join.ROUND);
mArcPaint.setStrokeCap(Paint.Cap.ROUND);
//設(shè)置畫筆類型
mArcPaint.setStyle(Paint.Style.STROKE);
mArcPaint.setStrokeWidth(dp2px(mStrokeWith));
文字的畫筆mTextPaint
//中心文字的paint
mTextPaint=new Paint();
mTextPaint.setAntiAlias(true);
mTextPaint.setColor(Color.parseColor("#FF4A40"));
//設(shè)置文本的對齊方式
mTextPaint.setTextAlign(Paint.Align.CENTER);
//mTextPaint.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_12));
mTextPaint.setTextSize(dp2px(25));
3.Canvas繪制
圓弧的繪制
/**
* 繪制圓弧
* @param canvas
*/
private void drawArc(Canvas canvas) {
//繪制圓弧背景
RectF mRectF=new RectF(mStrokeWith+dp2px(5),mStrokeWith+dp2px(5),getWidth()-mStrokeWith-dp2px(5),getHeight()-mStrokeWith);
canvas.drawArc(mRectF,startAngle,mAngle,false,mArcPaint);
//繪制當(dāng)前數(shù)值對應(yīng)的圓弧
mArcPaint.setColor(Color.parseColor("#FF4A40"));
//根據(jù)當(dāng)前數(shù)據(jù)繪制對應(yīng)的圓弧
canvas.drawArc(mRectF,startAngle,mIncludedAngle,false,mArcPaint);
}
文本的繪制
/**
* 繪制文本
* @param canvas
*/
private void drawText(Canvas canvas) {
Rect mRect=new Rect();
String mValue=String.valueOf(mAnimatorValue);
//繪制中心的數(shù)值
mTextPaint.getTextBounds(mValue,0,mValue.length(),mRect);
canvas.drawText(String.valueOf(mAnimatorValue),centerX,centerY+mRect.height(),mTextPaint);
//繪制中心文字描述
mTextPaint.setColor(Color.parseColor("#999999"));
mTextPaint.setTextSize(dp2px(12));
mTextPaint.getTextBounds(mDes,0,mDes.length(),mRect);
canvas.drawText(mDes,centerX,centerY+2*mRect.height()+dp2px(10),mTextPaint);
//繪制最小值
String minValue=String.valueOf(mMinValue);
String maxValue=String.valueOf(mMaxValue);
mTextPaint.setTextSize(dp2px(18));
mTextPaint.getTextBounds(minValue,0,minValue.length(),mRect);
canvas.drawText(minValue, (float) (centerX-0.6*centerX-dp2px(5)), (float) (centerY+0.75*centerY+mRect.height()+dp2px(5)),mTextPaint);
//繪制最大值 mTextPaint.getTextBounds(maxValue,0,maxValue.length(),mRect);
canvas.drawText(maxValue, (float) (centerX+0.6*centerX+dp2px(5)), (float) (centerY+0.75*centerY+mRect.height()+dp2px(5)),mTextPaint);
}
4.添加動畫效果及數(shù)據(jù)
動畫效果
/**
* 為繪制弧度及數(shù)據(jù)設(shè)置動畫
*
* @param startAngle 開始的弧度
* @param currentAngle 需要繪制的弧度
* @param currentValue 需要繪制的數(shù)據(jù)
* @param time 動畫執(zhí)行的時長
*/
private void setAnimation(float startAngle, float currentAngle,int currentValue, int time) {
//繪制當(dāng)前數(shù)據(jù)對應(yīng)的圓弧的動畫效果
ValueAnimator progressAnimator = ValueAnimator.ofFloat(startAngle, currentAngle);
progressAnimator.setDuration(time);
progressAnimator.setTarget(mIncludedAngle);
progressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mIncludedAngle = (float) animation.getAnimatedValue();
//重新繪制,不然不會出現(xiàn)效果
postInvalidate();
}
});
//開始執(zhí)行動畫
progressAnimator.start();
//中心數(shù)據(jù)的動畫效果
ValueAnimator valueAnimator = ValueAnimator.ofInt(mAnimatorValue, currentValue);
valueAnimator.setDuration(2500);
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
mAnimatorValue = (int) valueAnimator.getAnimatedValue();
postInvalidate();
}
});
valueAnimator.start();
}
數(shù)據(jù)添加
/**
* 設(shè)置數(shù)據(jù)
* @param minValue 最小值
* @param maxValue 最大值
* @param currentValue 當(dāng)前繪制的值
* @param des 描述信息
*/
public void setValues(int minValue,int maxValue, int currentValue,String des) {
mDes=des;
mMaxValue=maxValue;
mMinValue=minValue;
//完全覆蓋背景弧度
if (currentValue maxValue) {
currentValue = maxValue;
} //計算弧度比重
float scale = (float) currentValue / maxValue;
//計算弧度
float currentAngle = scale * mAngle;
//開始執(zhí)行動畫
setAnimation(0, currentAngle, currentValue,2500);
完整代碼:
/**
* 自定義的圓弧形view
*/
public class ArcView extends View {
//根據(jù)數(shù)據(jù)顯示的圓弧Paint
private Paint mArcPaint;
//文字描述的paint
private Paint mTextPaint;
//圓弧開始的角度
private float startAngle=135;
//圓弧結(jié)束的角度
private float endAngle=45;
//圓弧背景的開始和結(jié)束間的夾角大小
private float mAngle=270;
//當(dāng)前進度夾角大小
private float mIncludedAngle=0;
//圓弧的畫筆的寬度
private float mStrokeWith=10;
//中心的文字描述
private String mDes="";
//動畫效果的數(shù)據(jù)及最大/小值
private int mAnimatorValue,mMinValue,mMaxValue;
//中心點的XY坐標(biāo)
private float centerX,centerY;
public ArcView(Context context) {
this(context,null);
}
public ArcView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public ArcView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//init();
}
private void initPaint() {
//圓弧的paint
mArcPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
//抗鋸齒
mArcPaint.setAntiAlias(true);
mArcPaint.setColor(Color.parseColor("#666666"));
//設(shè)置透明度(數(shù)值為0-255)
mArcPaint.setAlpha(100);
//設(shè)置畫筆的畫出的形狀
mArcPaint.setStrokeJoin(Paint.Join.ROUND);
mArcPaint.setStrokeCap(Paint.Cap.ROUND);
//設(shè)置畫筆類型
mArcPaint.setStyle(Paint.Style.STROKE);
mArcPaint.setStrokeWidth(dp2px(mStrokeWith));
//中心文字的paint
mTextPaint=new Paint();
mTextPaint.setAntiAlias(true);
mTextPaint.setColor(Color.parseColor("#FF4A40"));
//設(shè)置文本的對齊方式
mTextPaint.setTextAlign(Paint.Align.CENTER);
//mTextPaint.setTextSize(getResources().getDimensionPixelSize(R.dimen.dp_12));
mTextPaint.setTextSize(dp2px(25));
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
centerX=getWidth()/2;
centerY=getHeight()/2;
//初始化paint
initPaint();
//繪制弧度
drawArc(canvas);
//繪制文本
drawText(canvas);
}
/**
* 繪制文本
* @param canvas
*/
private void drawText(Canvas canvas) {
Rect mRect=new Rect();
String mValue=String.valueOf(mAnimatorValue);
//繪制中心的數(shù)值
mTextPaint.getTextBounds(mValue,0,mValue.length(),mRect);
canvas.drawText(String.valueOf(mAnimatorValue),centerX,centerY+mRect.height(),mTextPaint);
//繪制中心文字描述
mTextPaint.setColor(Color.parseColor("#999999"));
mTextPaint.setTextSize(dp2px(12));
mTextPaint.getTextBounds(mDes,0,mDes.length(),mRect);
canvas.drawText(mDes,centerX,centerY+2*mRect.height()+dp2px(10),mTextPaint);
//繪制最小值
String minValue=String.valueOf(mMinValue);
String maxValue=String.valueOf(mMaxValue);
mTextPaint.setTextSize(dp2px(18));
mTextPaint.getTextBounds(minValue,0,minValue.length(),mRect);
canvas.drawText(minValue, (float) (centerX-0.6*centerX-dp2px(5)), (float) (centerY+0.75*centerY+mRect.height()+dp2px(5)),mTextPaint);
//繪制最大指
mTextPaint.getTextBounds(maxValue,0,maxValue.length(),mRect);
canvas.drawText(maxValue, (float) (centerX+0.6*centerX+dp2px(5)), (float) (centerY+0.75*centerY+mRect.height()+dp2px(5)),mTextPaint);
}
/**
* 繪制當(dāng)前的圓弧
* @param canvas
*/
private void drawArc(Canvas canvas) {
//繪制圓弧背景
RectF mRectF=new RectF(mStrokeWith+dp2px(5),mStrokeWith+dp2px(5),getWidth()-mStrokeWith-dp2px(5),getHeight()-mStrokeWith);
canvas.drawArc(mRectF,startAngle,mAngle,false,mArcPaint);
//繪制當(dāng)前數(shù)值對應(yīng)的圓弧
mArcPaint.setColor(Color.parseColor("#FF4A40"));
//根據(jù)當(dāng)前數(shù)據(jù)繪制對應(yīng)的圓弧
canvas.drawArc(mRectF,startAngle,mIncludedAngle,false,mArcPaint);
}
/**
* 為繪制弧度及數(shù)據(jù)設(shè)置動畫
*
* @param startAngle 開始的弧度
* @param currentAngle 需要繪制的弧度
* @param currentValue 需要繪制的數(shù)據(jù)
* @param time 動畫執(zhí)行的時長
*/
private void setAnimation(float startAngle, float currentAngle,int currentValue, int time) {
//繪制當(dāng)前數(shù)據(jù)對應(yīng)的圓弧的動畫效果
ValueAnimator progressAnimator = ValueAnimator.ofFloat(startAngle, currentAngle);
progressAnimator.setDuration(time);
progressAnimator.setTarget(mIncludedAngle);
progressAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mIncludedAngle = (float) animation.getAnimatedValue();
//重新繪制,不然不會出現(xiàn)效果
postInvalidate();
}
});
//開始執(zhí)行動畫
progressAnimator.start();
//中心數(shù)據(jù)的動畫效果
ValueAnimator valueAnimator = ValueAnimator.ofInt(mAnimatorValue, currentValue);
valueAnimator.setDuration(2500);
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
mAnimatorValue = (int) valueAnimator.getAnimatedValue();
postInvalidate();
}
});
valueAnimator.start();
}
/**
* 設(shè)置數(shù)據(jù)
* @param minValue 最小值
* @param maxValue 最大值
* @param currentValue 當(dāng)前繪制的值
* @param des 描述信息
*/
public void setValues(int minValue,int maxValue, int currentValue,String des) {
mDes=des;
mMaxValue=maxValue;
mMinValue=minValue;
//完全覆蓋
if (currentValue maxValue) {
currentValue = maxValue;
}
//計算弧度比重
float scale = (float) currentValue / maxValue;
//計算弧度
float currentAngle = scale * mAngle;
//開始執(zhí)行動畫
setAnimation(0, currentAngle, currentValue,2500);
}
public float dp2px(float dp) {
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
return dp * metrics.density;
}
}
總結(jié):設(shè)置Paint的畫筆形狀(Cap和Join設(shè)置為弧形);使用Canvas的drawArc方法繪制圓弧及drawText繪制文本信息等;ValueAnimator設(shè)置數(shù)據(jù)及當(dāng)前圓弧進度的動畫效果。
到此這篇關(guān)于Android自定義View實現(xiàn)圓弧進度效果逐步完成過程的文章就介紹到這了,更多相關(guān)Android自定義View圓弧進度內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺析KJFrameForAndroid框架如何高效加載Bitmap
Bitmap是Android系統(tǒng)中的圖像處理的最重要類之一。用它可以獲取圖像文件信息,進行圖像剪切、旋轉(zhuǎn)、縮放等操作,并可以指定格式保存圖像文件。本文主要是從KJFrameForAndroid框架中分析高效加載Bitmap的方法2014-07-07
Android?Compose狀態(tài)改變動畫animateXxxAsState使用詳解
這篇文章主要為大家介紹了Android?Compose狀態(tài)改變動畫animateXxxAsState使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
Android 第三方應(yīng)用接入微信平臺研究情況分享(一)
微信平臺開放后倒是挺火的,許多第三方應(yīng)用都想試下接入微信這個平臺,畢竟可以利用微信建立起來的關(guān)系鏈來拓展自己的應(yīng)用還是挺不錯的 最近由于實習(xí)需要也在研究這個東西,這里把我的整個研究情況給出來2013-01-01
詳解用RxJava實現(xiàn)事件總線(Event Bus)
本篇文章主要介紹了用RxJava實現(xiàn)事件總線(Event Bus),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
Android中使用Vectors(2)繪制優(yōu)美的路徑動畫
這篇文章主要介紹了Android中使用Vectors(2)繪制優(yōu)美的路徑動畫的相關(guān)資料,需要的朋友可以參考下2016-03-03

