Android自定義View實現(xiàn)圓弧進度效果逐步完成過程
涉及到的知識Canvas(畫布),Paint(畫筆),自定義控件等有三種:一個是直接從View繼承,完全的自定義;二是對原有控件進行改造,達(dá)到想要的效果;還有一種自定義的組合控件,根據(jù)自己的需要將已有的控件組合起來達(dá)到效果。我對自定義視圖也略知一二,就簡單記錄一下自己對自定義視圖的學(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)文章
詳解Android中OkHttp3的例子和在子線程更新UI線程的方法
本篇文章主要介紹了詳解Android中OkHttp3的例子和在子線程更新UI線程的方法 ,非常具有實用價值,需要的朋友可以參考下2017-05-05Android 使用Vitamio打造自己的萬能播放器(5)——在線播放(播放優(yōu)酷視頻)
本文主要介紹Android Vitamio的使用,這里給大家提供效果圖和代碼實例,來說明Vitamio組件播放網(wǎng)絡(luò)視頻,有需要的小伙伴可以參考下2016-07-07Android Studio Intent隱式啟動,發(fā)短信,撥號,打電話,訪問網(wǎng)頁等實例代碼
這篇文章主要介紹了Android Studio Intent隱式啟動,發(fā)短信,撥號,打電話,訪問網(wǎng)頁等實例代碼的相關(guān)資料,需要的朋友可以參考下2016-12-12Android App中實現(xiàn)可以雙擊放大和縮小圖片功能的實例
這篇文章主要介紹了Android App中實現(xiàn)可以雙擊放大和縮小圖片功能的實例,文中的例子不能做到逐級放大但可以做到邊界控制和以觸摸點為中心進行放大,需要的朋友可以參考下2016-03-03Android實現(xiàn)網(wǎng)絡(luò)圖片瀏覽功能
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)網(wǎng)絡(luò)圖片瀏覽功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06Android仿拉手網(wǎng)團購App我的收藏界面實例代碼
這篇文章主要介紹了Android仿拉手團購網(wǎng)App我的收藏界面實例代碼,需要的朋友可以參考下2017-05-05