Android倒計(jì)時(shí)功能的實(shí)現(xiàn)代碼
好久沒有寫博客了,趁著年末,總結(jié)了下最近一年所遇到的一些技術(shù)問題,還有一些自定義控件,比如倒計(jì)時(shí)功能
首先倒計(jì)時(shí)的實(shí)現(xiàn)方式
1.Handler
2.Timer
3.RxJava
4.ValueAnimator
5.其他
這些方式中,我選擇了ValueAnimator,主要是它的API比較友好,不需要我們?nèi)シ庋b太多東西,具體的使用方式我就不單獨(dú)寫了,下面的代碼都有備注
項(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;
}
/**
* 開始倒計(jì)時(shí)
*/
public void startCountDown() {
setClickable(false);
valueAnimator = getValA(mCountdownTime * 1000);
//狀態(tài)更新監(jiān)聽
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)聽
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ì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)我們網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
上一篇:android 自定義圓角button效果的實(shí)例代碼(自定義view Demo)
欄 目:Android
下一篇:android實(shí)現(xiàn)簡單音樂播放器
本文標(biāo)題:Android倒計(jì)時(shí)功能的實(shí)現(xiàn)代碼
本文地址:http://www.jygsgssxh.com/a1/Android/8974.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)登錄
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10Emoji表情在Android JNI中的兼容性問題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10android開發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解
- 01-10android異步消息機(jī)制 源碼層面徹底解析(1)


閱讀排行
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問題詳
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置


