自定義View實戰(zhàn)二:計步器的實現

一参滴、概述


本文詳細結合自定義 View 和 屬性動畫,講述如何自定義一個圓弧計步器漩仙。

二缆镣、實現步驟分析


  1. 確定自定義屬性芽突,編寫attrs.xml
  2. 在自定義View中獲取自定義屬性,做好初始化工作
  3. onMeasure(int widthMeasureSpec, int heightMeasureSpec) 確保正方形
  4. onDraw(Canvas canvas) 畫外圓弧、內圓弧董瞻、文字
  5. 提供調用方法

三寞蚌、具體實現


  1. 確定自定義屬性,編寫attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="StepView">
        <attr name="outerColor" format="color" />
        <attr name="innerColor" format="color" />
        <attr name="borderWidth" format="dimension" />
        <attr name="stepTextSize" format="dimension" />
        <attr name="stepTextColor" format="color" />
    </declare-styleable>
</resources>
  1. 在自定義View中獲取自定義屬性,做好初始化工作
public StepView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.StepView);
        mOuterColor = array.getColor(R.styleable.StepView_outerColor,mOuterColor);
        mInnerColor = array.getColor(R.styleable.StepView_innerColor, mInnerColor);
        mBorderWidth = (int) array.getDimension(R.styleable.StepView_borderWidth,mBorderWidth);
        mStepTextSize = array.getDimensionPixelSize(R.styleable.StepView_stepTextSize,mStepTextSize);
        mStepTextColor = array.getColor(R.styleable.StepView_stepTextColor, mStepTextColor);
        array.recycle();

        mOutPaint = new Paint();
        mOutPaint.setAntiAlias(true);
        mOutPaint.setStrokeWidth(mBorderWidth);
        mOutPaint.setColor(mOuterColor);
        mOutPaint.setStrokeCap(Paint.Cap.ROUND);
        mOutPaint.setStyle(Paint.Style.STROKE);      // 畫筆空心

        mInnerPaint = new Paint();
        mInnerPaint.setAntiAlias(true);
        mInnerPaint.setStrokeWidth(mBorderWidth);
        mInnerPaint.setColor(mInnerColor);
        mInnerPaint.setStrokeCap(Paint.Cap.ROUND);
        mInnerPaint.setStyle(Paint.Style.STROKE);    // 畫筆空心


        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(mStepTextColor);
        mTextPaint.setTextSize(mStepTextSize);
        // 寬高默認 30 dp
        width = dp2px(context, 30);
        height = dp2px(context, 30);
    }
  1. onMeasure(int widthMeasureSpec, int heightMeasureSpec) 確保正方形
@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 調用者在布局文件中可能  wrap_content
        // 獲取模式 AT_MOST  默認30 dp

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (widthMode != MeasureSpec.AT_MOST){
            width = MeasureSpec.getSize(widthMeasureSpec);
        }
        if (heightMode != MeasureSpec.AT_MOST){
            height = MeasureSpec.getSize(heightMeasureSpec);
        }
        // 寬度高度不一致 取最小值力细,確保是個正方形
        setMeasuredDimension(width > height ? height : width, width > height ? height : width);
    }
  1. onDraw(Canvas canvas) 畫外圓弧睬澡、內圓弧、文字
@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // int center = getWidth() / 2;
        // int radius = getWidth() / 2 - mBorderWidth / 2;
        // RectF rectF = new RectF(center - radius,center - radius ,center + radius,center + radius);
        // 下面代碼由上面代碼簡化而成
        RectF rectF = new RectF(mBorderWidth / 2 + getPaddingLeft(),mBorderWidth / 2 + getPaddingTop()
        ,getWidth() - (mBorderWidth / 2 + getPaddingRight()),getHeight() - (mBorderWidth / 2 + getPaddingBottom()));

        canvas.drawArc(rectF,135,270,false,mOutPaint);

        if(mStepMax == 0) return;
        // 畫內圓弧  怎么畫肯定不能寫死  百分比  是使用者設置的從外面?zhèn)?        float sweepAngle = (float)mCurrentStep / mStepMax;
        canvas.drawArc(rectF, 135, sweepAngle * 270, false, mInnerPaint);

        //  畫文字
        String stepText = mCurrentStep+"";
        Rect textBounds = new Rect();
        mTextPaint.getTextBounds(stepText, 0, stepText.length(), textBounds);
        int dx = getWidth() / 2 - textBounds.width() / 2;
        // 基線 baseLine
        Paint.FontMetricsInt  fontMetrics = mTextPaint.getFontMetricsInt();
        int dy = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        int baseLine = getHeight() / 2 + dy;
        canvas.drawText(stepText,dx,baseLine,mTextPaint);
    }
  1. 提供調用方法
/**
     * 設置步數最大值
     * @param stepMax
     */
    public synchronized void setStepMax(int stepMax){
        this.mStepMax = stepMax;
    }

    /**
     * 設置當前步數
     * @param currentStep
     */
    public synchronized void setCurrentStep(int currentStep){
        this.mCurrentStep = currentStep;
        // 刷新
        invalidate();
    }

四眠蚂、結合屬性動畫使用


xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.vegen.StepView
        android:id="@+id/step_view"
        app:outerColor="@color/colorPrimary"
        app:innerColor="@color/colorAccent"
        app:borderWidth="20dp"
        app:stepTextColor="@color/colorAccent"
        app:stepTextSize="30sp"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

Activity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final StepView qqStepView = (StepView) findViewById(R.id.step_view);
        qqStepView.setStepMax(10000);

        // 屬性動畫
        ValueAnimator valueAnimator = ObjectAnimator.ofFloat(0, 8000);
        valueAnimator.setDuration(1000);
        valueAnimator.setInterpolator(new DecelerateInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float currentStep = (float) animation.getAnimatedValue();
                qqStepView.setCurrentStep((int)currentStep);
            }
        });
        valueAnimator.start();
    }
}

五煞聪、最終實現的效果

xiaoguotu.jpg
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市逝慧,隨后出現的幾起案子昔脯,更是在濱河造成了極大的恐慌,老刑警劉巖笛臣,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件云稚,死亡現場離奇詭異,居然都是意外死亡沈堡,警方通過查閱死者的電腦和手機静陈,發(fā)現死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來诞丽,“玉大人鲸拥,你說我怎么就攤上這事∩猓” “怎么了刑赶?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長懂衩。 經常有香客問我撞叨,道長金踪,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任牵敷,我火速辦了婚禮胡岔,結果婚禮上,老公的妹妹穿的比我還像新娘劣领。我一直安慰自己姐军,他們只是感情好,可當我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布尖淘。 她就那樣靜靜地躺著,像睡著了一般著觉。 火紅的嫁衣襯著肌膚如雪村生。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天饼丘,我揣著相機與錄音趁桃,去河邊找鬼。 笑死肄鸽,一個胖子當著我的面吹牛卫病,可吹牛的內容都是我干的。 我是一名探鬼主播典徘,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼蟀苛,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了逮诲?” 一聲冷哼從身側響起帜平,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎梅鹦,沒想到半個月后裆甩,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡齐唆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年嗤栓,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片箍邮。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡茉帅,死狀恐怖,靈堂內的尸體忽然破棺而出媒殉,到底是詐尸還是另有隱情担敌,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布廷蓉,位于F島的核電站全封,受9級特大地震影響马昙,放射性物質發(fā)生泄漏。R本人自食惡果不足惜刹悴,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一行楞、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧土匀,春花似錦子房、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至妒御,卻和暖如春解愤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背乎莉。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工送讲, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人惋啃。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓哼鬓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親边灭。 傳聞我的和親對象是個殘疾皇子异希,可洞房花燭夜當晚...
    茶點故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內容