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?studio實(shí)現(xiàn)app登錄界面
這篇文章主要為大家詳細(xì)介紹了Android?studio實(shí)現(xiàn)app登錄界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Android編程實(shí)現(xiàn)泡泡聊天界面實(shí)例詳解(附源碼)
這篇文章主要介紹了Android編程實(shí)現(xiàn)泡泡聊天界面,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android泡泡聊天界面的窗體定義與功能實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android編程之ICS式下拉菜單PopupWindow實(shí)現(xiàn)方法詳解(附源碼下載)
這篇文章主要介紹了Android編程之ICS式下拉菜單PopupWindow實(shí)現(xiàn)方法,結(jié)合實(shí)例詳細(xì)分析了ICS式下拉菜單的實(shí)現(xiàn)原理與相關(guān)技巧,并附帶源碼供讀者下載,需要的朋友可以參考下2015-12-12
Jetpack?Compose?DropdownMenu手指跟隨點(diǎn)擊顯示
這篇文章主要為大家介紹了Jetpack?Compose?DropdownMenu手指跟隨點(diǎn)擊位置顯示實(shí)現(xiàn)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
android 仿微信demo——微信主界面實(shí)現(xiàn)
本系列文章主要介紹了微信小程序-閱讀小程序?qū)嵗╠emo),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望能給你們提供幫助2021-06-06
Android Studio和Gradle使用不同位置JDK的問(wèn)題解決
這篇文章主要介紹了Android Studio和Gradle使用不同位置JDK的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
淺析Android系統(tǒng)的架構(gòu)以及程序項(xiàng)目的目錄結(jié)構(gòu)
這篇文章主要介紹了Android系統(tǒng)的架構(gòu)以及程序項(xiàng)目的目錄結(jié)構(gòu),是安卓開(kāi)發(fā)入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2016-04-04
Android MediaPlayer音頻播放器封裝示例淺析
Android提供了許多方法來(lái)控制播放的音頻/視頻文件和流。其中該方法是通過(guò)一類稱為MediaPlayer。Android是提供MediaPlayer類訪問(wèn)內(nèi)置的媒體播放器的服務(wù),如播放音頻,視頻等為了使用MediaPlayer,我們要調(diào)用這個(gè)類的靜態(tài)create()方法2023-04-04

