Android倒計(jì)時(shí)功能的實(shí)現(xiàn)代碼
好久沒(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)目圖片

代碼實(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)注明出處,謝謝!
- Android實(shí)現(xiàn)啟動(dòng)頁(yè)倒計(jì)時(shí)效果
- Android 實(shí)現(xiàn)搶購(gòu)倒計(jì)時(shí)功能的示例
- android實(shí)現(xiàn)倒計(jì)時(shí)動(dòng)態(tài)圈
- android實(shí)現(xiàn)圓環(huán)倒計(jì)時(shí)控件
- android利用handler實(shí)現(xiàn)倒計(jì)時(shí)功能
- Android自定義view實(shí)現(xiàn)倒計(jì)時(shí)控件
- 解決Android-RecyclerView列表倒計(jì)時(shí)錯(cuò)亂問(wèn)題
- Android實(shí)現(xiàn)自定義倒計(jì)時(shí)
- Android 倒計(jì)時(shí)控件 CountDownView的實(shí)例代碼詳解
- Android倒計(jì)時(shí)神器(CountDownTimer)
- Android 簡(jiǎn)單實(shí)現(xiàn)倒計(jì)時(shí)功能
- Android自定義TimeButton實(shí)現(xiàn)倒計(jì)時(shí)按鈕
- Android實(shí)現(xiàn)倒計(jì)時(shí)的按鈕效果
- 利用Android設(shè)計(jì)一個(gè)倒計(jì)時(shí)組件
相關(guān)文章
Android編程實(shí)現(xiàn)的微信支付功能詳解【附Demo源碼下載】
這篇文章主要介紹了Android編程實(shí)現(xiàn)的微信支付功能,結(jié)合實(shí)例形式詳細(xì)分析了Android微信支付功能的實(shí)現(xiàn)步驟與具體操作技巧,并附帶了Demo源碼供讀者下載參考,需要的朋友可以參考下2017-07-07
Android Flutter實(shí)現(xiàn)興趣標(biāo)簽選擇功能
我們?cè)谑状问褂脙?nèi)容類(lèi) App 的時(shí)候,不少都會(huì)讓我們選擇個(gè)人偏好,通過(guò)這些標(biāo)簽選擇可以預(yù)先知道用戶(hù)的偏好信息。我們本篇就來(lái)看看 Flutter 如何實(shí)現(xiàn)興趣標(biāo)簽的選擇,需要的可以參考一下2022-11-11
Android防止點(diǎn)擊過(guò)快造成多次響應(yīng)事件的解決方法
btn點(diǎn)擊用戶(hù)可能只點(diǎn)擊了一次但是后臺(tái)響應(yīng)了多次,像一些表單的提交出現(xiàn)這種問(wèn)題比較棘手,本篇文章主要介紹Android防止點(diǎn)擊過(guò)快造成多次響應(yīng)事件的解決方法,有興趣的可以了解一下。2016-12-12
Android實(shí)現(xiàn)原生鎖屏頁(yè)面音樂(lè)控制
這篇文章主要介紹了Android實(shí)現(xiàn)原生鎖屏頁(yè)面音樂(lè)控制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12
Android中使用Expandablelistview實(shí)現(xiàn)微信通訊錄界面
本文主要介紹了Android中使用Expandablelistview實(shí)現(xiàn)微信通訊錄界面(完善防微信APP)的方法,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2016-12-12
Android Studio中生成aar文件及本地方式使用aar文件的方法
這篇文章給大家講解Android Studio中生成aar文件以及本地方式使用aar文件的方法,也就是說(shuō) *.jar 與 *.aar 的生成與*.aar導(dǎo)入項(xiàng)目方法,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2017-12-12
Android使用httpPost向服務(wù)器發(fā)送請(qǐng)求的方法
這篇文章主要介紹了Android使用httpPost向服務(wù)器發(fā)送請(qǐng)求的方法,實(shí)例分析了Android針對(duì)HttpPost類(lèi)的操作技巧,需要的朋友可以參考下2015-12-12
基于adbkit的android設(shè)備管理(精簡(jiǎn)版stf)
這篇文章主要為大家介紹了基于adbkit的android設(shè)備管理(精簡(jiǎn)版stf)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

