Android進階之自定義View(2)高仿釘釘運動步數(shù)實現(xiàn)可動的進度圓環(huán)(下)

接著上篇Android進階之自定義控件(2)高仿釘釘運動步數(shù)實現(xiàn)可動的進度圓環(huán)(上)的基礎(chǔ),我們來實現(xiàn)釘釘運動的效果:

《一》View效果分析:

對釘釘運動的效果進行分析:
1、圓弧應(yīng)該是從135°起,繪制了270°。
2、步數(shù)小于10000步時,背景圓弧為灰色,進度圓弧為藍色漸變色;步數(shù)大于10000步時,進度圓弧為漸變的黃色
3、需要繪制中間的步數(shù)及步數(shù)上面的名次
4、實現(xiàn)步數(shù)及進度的動態(tài)變化


dingding.gif

有了上篇的基礎(chǔ),實現(xiàn)釘釘運動步數(shù)的效果,就很簡單了。還是先看一下我最終實現(xiàn)的高仿版效果,給大家點信心。變化的速度可以配置:


高仿釘釘運動效果.gif

《二》實現(xiàn)步驟分解:

(1)繪制灰色背景圓弧,135°起,繪制了270°
canvas.drawArc(oval, 135, 270, false, mPaint);
(2)繪制進度圓弧
 canvas.drawArc(oval, 0 + 135, mCurrentStep/ mMaxStep * 270, false, mProgressPaint);
(3)繪制文字很簡單,只需要以正中間的文字為標準,往上移動一個textRect.height()和文字之間的間距textPadding即可。上篇這些基礎(chǔ)的知識及需要注意的小細節(jié)都有詳細的講解,不多做解釋。到此步驟,實現(xiàn)的效果如下:
 //繪制中間的步數(shù)的文字
  Rect textRect = new Rect();
  String mShowText = (int) mCurrentStep + "";
  mTextPaint.setTextSize(sp2px(mTextSize));
  mTextPaint.getTextBounds(mShowText, 0, mShowText.length(), textRect);
  canvas.drawText(mShowText, getWidth() / 2 - textRect.width() / 2, getHeight() / 2 + textRect.height() / 2, mTextPaint);

 //繪制排名的文字,在步數(shù)文字的上方
 String mRandText = "第4名";//使用時,名次動態(tài)傳入即可,此處寫死為了測試
 mTextPaint.setTextSize(sp2px(mRandTextSize));
 mTextPaint.getTextBounds(mRandText, 0, mRandText.length(), textRect);
 canvas.drawText(mRandText, getWidth() / 2 - textRect.width() / 2, getHeight() / 2 + textRect.height() / 2 - (textRect.height() + textPadding), mTextPaint);
image.png
(4)可以看到,上圖還是靜態(tài)的效果,接下來我們通過兩種方式來讓進度圓弧和文字動起來。

1、方法一:還是上篇講解過的方法,通過開分線程的方式,不斷更新進度值,不斷重繪。

        new Thread(new Runnable() {
            @Override
            public void run() {
                setCurrentStep(0);
                float changeProgress = currentStep;
                for (float i = 0; i < changeProgress; i++) {
                    setCurrentStep(getCurrentStep() + rate);
                    SystemClock.sleep(20);
//                  invalidate();//invalidate()必須在主線程中執(zhí)行,此處不能使用
//                  postInvalidate();//強制重繪,postInvalidate()可以在主線程也可以在分線程中執(zhí)行
                    changeProgress = changeProgress - rate;
                }
                //由于上面的循環(huán)結(jié)束時,可能計算后最終無法到達mCurrentProgress的值,所以在循環(huán)結(jié)束后,將mCurrentProgress重新設(shè)置
                setCurrentStep(currentStep);
            }
        }).start();
  

2、方法二:使用屬性動畫和差值器實現(xiàn)

 ValueAnimator valueAnimator = ObjectAnimator.ofFloat(0, currentStep);
        valueAnimator.setDuration(1000);
        valueAnimator.setInterpolator(new DecelerateInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float currentStep = (float) animation.getAnimatedValue();
                setCurrentStep((int) currentStep);
            }
        });
        valueAnimator.start();
(5)最后一步了,來看看漸變色的進度圓弧如何處理。

處理漸變有兩個類:
1、線性漸變:LinearGradient
2、掃描式漸變:SweepGradient ——在中心點畫一個掃描梯度的著色器
很明顯,在這里我們需要用到SweepGradient來處理,如下,給畫筆設(shè)置Shader,簡單的設(shè)置如下。


image.png
int[] mGradientColorArray = new int[]{ContextCompat.getColor(mContext, R.color.color_5e9fff),  ContextCompat.getColor(mContext, R.color.color_4fd8f5), ContextCompat.getColor(mContext, R.color.color_5e9fff)};
SweepGradient shader = new SweepGradient(getWidth() / 2 - mRoundWidth / 2, getWidth() / 2 - mRoundWidth / 2, mGradientColorArray, null);
mProgressPaint.setShader(shader);
image.png

還是和釘釘?shù)男Ч悬c區(qū)別,釘釘?shù)膱A弧兩邊是淺色的,上面頂部的部分是深色的,我們稍作處理一下,代碼如下:


image.png
         /**
         *  處理漸變色
         */
        //漸變顏色數(shù)組
        int[] mGradientColorArray = new int[]{ContextCompat.getColor(mContext, R.color.color_4fd8f5), ContextCompat.getColor(mContext, R.color.color_4fd8f5), ContextCompat.getColor(mContext, R.color.color_5e9fff), ContextCompat.getColor(mContext, R.color.color_4fd8f5)};
        int count = mGradientColorArray.length;
        int[] colors = new int[count];
        System.arraycopy(mGradientColorArray, 0, colors, 0, count);
        float[] positions = new float[count];
        //由于此處繪制的是不完整的圓弧,故需要額外處理一下變化的比例值
        float v = (360f / 270);
        positions[0] = 0.0f;
        positions[1] = 0.33f * v;
        positions[2] = 0.67f * v;
        positions[3] = 1.0f;
        SweepGradient shader = new SweepGradient(getWidth() / 2 - mRoundWidth / 2, getWidth() / 2 - mRoundWidth / 2, mGradientColorArray, positions);
        mProgressPaint.setShader(shader);

漸變的效果搞定。


image.png

測試代碼,在使用的地方調(diào)用startCountStep()即可,是不是敲極簡單:

   //當前實際的步數(shù)
      float   mCurrentStep = 9709;
      sportStepView.startCountStep(mCurrentStep);

View的完整代碼:

package com.example.jojo.learn.customview;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.example.jojo.learn.R;

/**
 * Created by JoJo on 2018/8/1.
 * wechat:18510829974
 * description: 仿釘釘運動步數(shù)效果
 */

public class SportStepView extends View {


    private Context mContext;
    //內(nèi)圓環(huán)顏色
    private int innerRoundColor;
    //外圓環(huán)顏色
    private int outerRoundColor;
    //繪制背景圓環(huán)的畫筆
    private Paint mPaint;
    //繪制外面進度的圓環(huán)的畫筆
    private Paint mProgressPaint;
    //繪制外面進度的圓環(huán)的畫筆
    private Paint mTextPaint;
    //背景圓弧的繪制的寬度
    private int mRoundWidth = 10;
    //進度圓環(huán)的寬度
    private float mProgressRoundWidth = 15;
    //中間步數(shù)文字的大小
    private int mTextSize = 40;//單位 sp
    //名次文字大小
    private int mRandTextSize = 15;
    //圓環(huán)最大進度
    private int mMaxStep = 10000;
    //圓環(huán)當前進度
    private float mCurrentStep = 0;
    //排名與步數(shù)文字直接的間隔
    private float textPadding = 80;
    //速度,值越大,變化速度越快
    private float rate = 128;

    public SportStepView(Context context) {
        this(context, null);
    }

    public SportStepView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SportStepView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SportStepView);
        innerRoundColor = typedArray.getColor(R.styleable.SportStepView_innerRoundColor, ContextCompat.getColor(mContext, R.color.color_e4e4e4));
        outerRoundColor = typedArray.getColor(R.styleable.SportStepView_outerRoundColor, ContextCompat.getColor(mContext, R.color.color_4fd8f5));

        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //獲取寬的模式
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        //獲取寬的尺寸
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        //對wrap_content這種模式進行處理
        if (heightMode == MeasureSpec.AT_MOST) {
            heightSize = widthSize;
        }
        //以寬度為標準保存丈量結(jié)果
        setMeasuredDimension(widthSize, heightSize);
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(innerRoundColor);
        mPaint.setStrokeCap(Paint.Cap.ROUND);// 圓形筆頭
        mPaint.setStrokeWidth(mRoundWidth);

        mProgressPaint = new Paint();
        mProgressPaint.setAntiAlias(true);// 抗鋸齒效果
        mProgressPaint.setStyle(Paint.Style.STROKE);
        mProgressPaint.setColor(outerRoundColor);
        mProgressPaint.setStrokeCap(Paint.Cap.ROUND);// 圓形筆頭
        mProgressPaint.setStrokeWidth(mProgressRoundWidth);

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);// 抗鋸齒效果
        mTextPaint.setStyle(Paint.Style.STROKE);
        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setColor(ContextCompat.getColor(mContext, R.color.color_56a9ff));
        mTextPaint.setTextSize(sp2px(mTextSize));


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        /**
         *  處理漸變色
         */
        //默認的漸變顏色數(shù)組:
//        int[] mGradientColorArray = new int[]{ContextCompat.getColor(mContext, R.color.color_5e9fff),  ContextCompat.getColor(mContext, R.color.color_4fd8f5), ContextCompat.getColor(mContext, R.color.color_5e9fff)};
        int[] mGradientColorArray = new int[]{ContextCompat.getColor(mContext, R.color.color_4fd8f5), ContextCompat.getColor(mContext, R.color.color_4fd8f5), ContextCompat.getColor(mContext, R.color.color_5e9fff), ContextCompat.getColor(mContext, R.color.color_4fd8f5)};
        int count = mGradientColorArray.length;
        int[] colors = new int[count];
        System.arraycopy(mGradientColorArray, 0, colors, 0, count);
        float[] positions = new float[count];
        float v = (360f / 270);
        positions[0] = 0.0f;
        positions[1] = 0.33f * v;
        positions[2] = 0.67f * v;
        positions[3] = 1.0f;
        SweepGradient shader = new SweepGradient(getWidth() / 2 - mRoundWidth / 2, getWidth() / 2 - mRoundWidth / 2, mGradientColorArray, positions);
        mProgressPaint.setShader(shader);


        if (mRoundWidth < mProgressRoundWidth) {
            RectF oval = new RectF(0 + mProgressRoundWidth / 2, 0 + mProgressRoundWidth / 2, getWidth() - mProgressRoundWidth / 2, getWidth() - mProgressRoundWidth / 2);
            //繪制背景圓環(huán)
            canvas.drawArc(oval, 135, 270, false, mPaint);

            //繪制進度圓環(huán),繪制的角度最大不超過270°
            if (mCurrentStep * 1f / mMaxStep <= 1) {
                canvas.drawArc(oval, 0 + 135, mCurrentStep / mMaxStep * 270, false, mProgressPaint);
            } else {
                canvas.drawArc(oval, 0 + 135, 270, false, mProgressPaint);
            }
        } else {
            RectF oval = new RectF(0 + mRoundWidth / 2, 0 + mRoundWidth / 2, getWidth() - mRoundWidth / 2, getWidth() - mRoundWidth / 2);
            //繪制背景圓環(huán)
            canvas.drawArc(oval, 135, 270, false, mPaint);
            //繪制進度圓環(huán)
            if (mCurrentStep * 1f / mMaxStep <= 1) {
                canvas.drawArc(oval, 0 + 135, mCurrentStep / mMaxStep * 270, false, mProgressPaint);
            } else {
                canvas.drawArc(oval, 0 + 135, 270, false, mProgressPaint);
            }
        }

        //繪制中間的步數(shù)的文字
        Rect textRect = new Rect();
        String mShowText = (int) mCurrentStep + "";
        mTextPaint.setTextSize(

                sp2px(mTextSize));
        mTextPaint.getTextBounds(mShowText, 0, mShowText.length(), textRect);
        canvas.drawText(mShowText, getWidth() / 2 - textRect.width() / 2, getHeight() / 2 + textRect.height() / 2, mTextPaint);

        //繪制排名的文字,在步數(shù)文字的上方
        String mRandText = "第4名";
        mTextPaint.setTextSize(

                sp2px(mRandTextSize));
        mTextPaint.getTextBounds(mRandText, 0, mRandText.length(), textRect);
        canvas.drawText(mRandText, getWidth() / 2 - textRect.width() / 2, getHeight() / 2 + textRect.height() / 2 - (textRect.height() + textPadding), mTextPaint);


    }

    public void setCurrentStep(float currentStep) {
        this.mCurrentStep = currentStep;
        //強制重繪,postInvalidate()可以在主線程也可以在分線程中執(zhí)行
        postInvalidate();
    }

    public void setMaxProgress(int maxStep) {
        this.mMaxStep = maxStep;
    }

    public int getMaxtep() {
        return mMaxStep;
    }

    public float getCurrentStep() {
        return mCurrentStep;
    }

    /**
     * 將sp轉(zhuǎn)換成px
     *
     * @param sp
     * @return
     */
    private int sp2px(int sp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,
                getResources().getDisplayMetrics());
    }

    /**
     * 開始動態(tài)計步
     *
     * @param currentStep
     */
    public void startCountStep(final float currentStep) {
        //方法一:開一個分線程,動態(tài)改變進度的值,不斷繪制達到進度變化的效果
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                setCurrentStep(0);
//                float changeProgress = currentStep;
//                for (float i = 0; i < changeProgress; i++) {
//                    setCurrentStep(getCurrentStep() + rate);
//                    SystemClock.sleep(20);
////                  invalidate();//invalidate()必須在主線程中執(zhí)行,此處不能使用
////                  postInvalidate();//強制重繪,postInvalidate()可以在主線程也可以在分線程中執(zhí)行
//                    changeProgress = changeProgress - rate;
//                }
//                //由于上面的循環(huán)結(jié)束時,可能計算后最終無法到達mCurrentProgress的值,所以在循環(huán)結(jié)束后,將mCurrentProgress重新設(shè)置
//                setCurrentStep(currentStep);
//            }
//        }).start();

        /**
         * 方法二
         */
        ValueAnimator valueAnimator = ObjectAnimator.ofFloat(0, currentStep);
        valueAnimator.setDuration(1000);
        valueAnimator.setInterpolator(new DecelerateInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float currentStep = (float) animation.getAnimatedValue();
                setCurrentStep((int) currentStep);
            }
        });
        valueAnimator.start();
    }
}

涉及到的自定義屬性及color.xml

   <!--SportStepView-->
    <declare-styleable name="SportStepView">
        <!--圓環(huán)半徑-->
        <attr name="radius" format="dimension"></attr>
        <attr name="outerRoundColor" format="color"></attr>
        <attr name="innerRoundColor" format="color"></attr>
    </declare-styleable>
  <color name="color_4fd8f5">#4fd8f5</color>
    <color name="color_5e9fff">#5e9fff</color>
    <color name="color_e4e4e4">#e4e4e4</color>
最后,附上我的一個Kotlin編寫+組件化開發(fā)的開源項目Designer

Kotlin+組件化開發(fā)實踐—開源項目Designer-App

Designer項目算是傾注了我蠻多心血了,每個頁面和功能都當成是上線的App來做,App的logo還特地做了UI設(shè)計??力求做到精致和完善,其中還包括了很多自己項目開發(fā)中的經(jīng)驗匯總和對新技術(shù)的探索和整合,希望對各位讀者有所幫助,歡迎點個star,follow,或者給個小心心,嘻嘻??也可以分享給你更多的朋友一起學(xué)習(xí),您的支持是我不斷前進的動力。如果有任何問題,歡迎在GitHub上給我提issue或者留言。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,606評論 6 533
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,582評論 3 418
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,540評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,028評論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,801評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,223評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,294評論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,442評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,976評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,800評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,996評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,543評論 5 360
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,233評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,662評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,926評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,702評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,991評論 2 374

推薦閱讀更多精彩內(nèi)容