android實(shí)現(xiàn)倒計(jì)時(shí)動(dòng)態(tài)圈
本文實(shí)例為大家分享了android實(shí)現(xiàn)倒計(jì)時(shí)動(dòng)態(tài)圈的具體代碼,供大家參考,具體內(nèi)容如下
效果是這樣,沒(méi)動(dòng)圖:

布局:
<LinearLayout android:layout_width="wrap_content" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:layout_centerInParent="true" android:layout_height="wrap_content"> <com.example.herman.testui.CountDownView android:id="@+id/tv_red_skip" android:layout_width="130dp" android:text="跳過(guò)" android:textColor="#ffffff" android:textSize="10sp" android:layout_height="130dp" /> </LinearLayout>
values下新建一個(gè)attr.xml,內(nèi)容是:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CountDownView"> <!--顏色--> <attr name="ringColor" format="color" /> <!-- 進(jìn)度文本的字體大小 --> <attr name="progressTextSize" format="dimension" /> <!-- 圓環(huán)寬度 --> <attr name="ringWidth" format="float" /> <!--進(jìn)度文本顏色--> <attr name="progressTextColor" format="color"/> <!--倒計(jì)時(shí)--> <attr name="countdownTime" format="integer"/> </declare-styleable> </resources>
一個(gè)類(lèi),類(lèi)名CountDownView,代碼如下:
package com.example.herman.testui;
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.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;
//圓輪進(jìn)度值文本大小
private int mRingProgessTextSize;
//寬度
private int mWidth;
//高度
private int mHeight;
private Paint mPaint;
//圓環(huán)的矩形區(qū)域
private RectF mRectF;
//
private int mProgessTextColor;
private int mCountdownTime;
private float mCurrentProgress;
private OnCountDownFinishListener mListener;
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, context.getResources().getColor(R.color.deepOrange));
mRingWidth = a.getFloat(R.styleable.CountDownView_ringWidth, 20);
mRingProgessTextSize = a.getDimensionPixelSize(R.styleable.CountDownView_progressTextSize, DisplayUtil.sp2px(context, 20));
mProgessTextColor = a.getColor(R.styleable.CountDownView_progressTextColor, context.getResources().getColor(R.color.deepOrange));
mCountdownTime = a.getInteger(R.styleable.CountDownView_countdownTime, 10);
a.recycle();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setAntiAlias(true);
this.setWillNotDraw(false);
}
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);
/**
*圓環(huán)
*/
//顏色
mPaint.setColor(mRingColor);
//空心
mPaint.setStyle(Paint.Style.STROKE);
//寬度
mPaint.setStrokeWidth(mRingWidth);
canvas.drawArc(mRectF, -90, mCurrentProgress - 360, false, mPaint);
//繪制文本
Paint textPaint = new Paint();
textPaint.setAntiAlias(true);
textPaint.setTextAlign(Paint.Align.CENTER);
String text = mCountdownTime - (int) (mCurrentProgress / 360f * mCountdownTime) + "";
textPaint.setTextSize(mRingProgessTextSize);
textPaint.setColor(mProgessTextColor);
//文字居中顯示
Paint.FontMetricsInt fontMetrics = textPaint.getFontMetricsInt();
int baseline = (int) ((mRectF.bottom + mRectF.top - fontMetrics.bottom - fontMetrics.top) / 2);
canvas.drawText(text, mRectF.centerX(), baseline, textPaint);
}
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 valueAnimator = getValA(mCountdownTime * 1000);
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();
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);
}
});
}
public void setAddCountDownListener(OnCountDownFinishListener mListener) {
this.mListener = mListener;
}
public interface OnCountDownFinishListener {
void countDownFinished();
}
}
activity中這樣調(diào)用:
CountDownView cdv = (CountDownView) findViewById(R.id.tv_red_skip);
cdv.setAddCountDownListener(new CountDownView.OnCountDownFinishListener() {
@Override
public void countDownFinished() {
//時(shí)間完了 干的事情
}
});
cdv.startCountDown();
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android studio2.3.3升級(jí)到3.1.2坑(小記)
這篇文章主要介紹了Android studio2.3.3升級(jí)3.1.2坑(小記),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
android:descendantFocusability方法介紹
開(kāi)發(fā)中很常見(jiàn)的一個(gè)問(wèn)題,項(xiàng)目中的listview不僅僅是簡(jiǎn)單的文字,常常需要自己定義listview,問(wèn)題就出現(xiàn)了,可能會(huì)發(fā)生點(diǎn)擊每一個(gè)item的時(shí)候沒(méi)有反應(yīng),無(wú)法獲取的焦點(diǎn)2012-11-11
Android自定義View實(shí)現(xiàn)可以拖拽的GridView
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)可以拖拽的GridView,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
詳解Android ContentProvider的基本原理和使用
ContentProvider(內(nèi)容提供者)是 Android 的四大組件之一,管理 Android 以結(jié)構(gòu)化方式存放的數(shù)據(jù),以相對(duì)安全的方式封裝數(shù)據(jù)(表)并且提供簡(jiǎn)易的處理機(jī)制和統(tǒng)一的訪(fǎng)問(wèn)接口供其他程序調(diào)用2021-06-06
詳解flutter engine 那些沒(méi)被釋放的東西
這篇文章主要介紹了詳解flutter engine 那些沒(méi)被釋放的東西,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Android?Activity?View加載與繪制流程深入刨析源碼
這篇文章主要介紹了Android?Activity?View的加載與繪制流程源碼分析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Android仿微信和QQ多圖合并框架(類(lèi)似群頭像)的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Android仿微信和QQ多圖合并框架的相關(guān)資料,其實(shí)就是我們平時(shí)所見(jiàn)的群聊頭像,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
Android開(kāi)發(fā)中編寫(xiě)藍(lán)牙相關(guān)功能的核心代碼講解
這篇文章主要介紹了Android開(kāi)發(fā)中編寫(xiě)藍(lán)牙功能的核心部分講解,包括掃描和配對(duì)以及修改藍(lán)牙設(shè)備可見(jiàn)性等操作,需要的朋友可以參考下2016-02-02

