您好,登錄后才能下訂單哦!
好久沒(méi)有寫(xiě)博客了,趁著年末,總結(jié)了下最近一年所遇到的一些技術(shù)問(wèn)題,還有一些自定義控件,比如倒計(jì)時(shí)功能
首先倒計(jì)時(shí)的實(shí)現(xiàn)方式
1.Handler
2.Timer
3.RxJava
4.ValueAnimator
5.其他
這些方式中,我選擇了ValueAnimator,主要是它的API比較友好,不需要我們?nèi)シ庋b太多東西,具體的使用方式我就不單獨(dú)寫(xiě)了,下面的代碼都有備注
項(xiàng)目地址
項(xiàng)目圖片
代碼實(shí)現(xiàn):
package com.example.countdownview; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; 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.RectF; import android.util.AttributeSet; import android.view.View; import android.view.animation.LinearInterpolator; public class CountDownView extends View { //圓輪顏色 private int mRingColor; //圓輪寬度 private float mRingWidth; //寬度 private int mWidth; //高度 private int mHeight; private Paint mPaint; //圓環(huán)的矩形區(qū)域 private RectF mRectF; // private int mCountdownTime; private float mCurrentProgress; private OnCountDownFinishListener mListener; ValueAnimator valueAnimator; public CountDownView(Context context) { this(context, null); } public CountDownView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CountDownView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CountDownView); mRingColor = a.getColor(R.styleable.CountDownView_ringColor, Color.RED); mCountdownTime = a.getInteger(R.styleable.CountDownView_countdownTime, 10); mRingWidth=a.getDimension(R.styleable.CountDownView_ringWidth,2); a.recycle(); mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); /** *圓環(huán) */ //顏色 mPaint.setColor(mRingColor); //空心 mPaint.setStyle(Paint.Style.STROKE); mPaint.setAntiAlias(true); // 消除鋸齒 //寬度 mPaint.setStrokeWidth(mRingWidth); } public void setCountdownTime(int mCountdownTime) { this.mCountdownTime = mCountdownTime; } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); mWidth = getMeasuredWidth(); mHeight = getMeasuredHeight(); mRectF = new RectF(0 + mRingWidth / 2, 0 + mRingWidth / 2, mWidth - mRingWidth / 2, mHeight - mRingWidth / 2); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawArc(mRectF, -90, mCurrentProgress, false, mPaint); } private ValueAnimator getValA(long countdownTime) { ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 100); valueAnimator.setDuration(countdownTime); valueAnimator.setInterpolator(new LinearInterpolator()); valueAnimator.setRepeatCount(0); return valueAnimator; } /** * 開(kāi)始倒計(jì)時(shí) */ public void startCountDown() { setClickable(false); valueAnimator = getValA(mCountdownTime * 1000); //狀態(tài)更新監(jiān)聽(tīng) valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float i = Float.valueOf(String.valueOf(animation.getAnimatedValue())); mCurrentProgress = (int) (360 * (i / 100f)); invalidate(); } }); valueAnimator.start(); //狀態(tài)變化結(jié)束監(jiān)聽(tīng) valueAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); //倒計(jì)時(shí)結(jié)束回調(diào) if (mListener != null) { mListener.countDownFinished(); } setClickable(true); } }); } /** * 恢復(fù) */ public void resumeCountDown(){ if (valueAnimator!=null){ valueAnimator.resume(); } } /** * 暫停 */ public void pauseCountDown(){ if (valueAnimator!=null){ valueAnimator.pause(); } } /** * 停止倒計(jì)時(shí) */ public void stopCountDown(){ if (valueAnimator!=null){ valueAnimator.cancel(); } } public void setCountDownFinishListener(OnCountDownFinishListener mListener) { this.mListener = mListener; } public interface OnCountDownFinishListener { void countDownFinished(); } }
總結(jié)
以上所述是小編給大家介紹的Android倒計(jì)時(shí)功能的實(shí)現(xiàn)代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。