Android自定義View倒計(jì)時(shí)圓
本文實(shí)例為大家分享了Android自定義View倒計(jì)時(shí)圓的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)建attr
<?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>
創(chuàng)建DisplayUtil 類
import android.content.Context; /** * Created by 王 on 2017/10/21. */ public class DisplayUtil { /** * 將px裝換成dp,保證尺寸不變 * @param context * @param pxValue * @return */ public static int px2dp(Context context, float pxValue){ float density = context.getResources().getDisplayMetrics().density;//得到設(shè)備的密度 return (int) (pxValue/density+0.5f); } public static int dp2px(Context context,float dpValue){ float density = context.getResources().getDisplayMetrics().density; return (int) (dpValue*density+0.5f); } public static int px2sp(Context context,float pxValue){ float scaleDensity = context.getResources().getDisplayMetrics().scaledDensity;//縮放密度 return (int) (pxValue/scaleDensity+0.5f); } public static int sp2px(Context context,float spValue) { float scaleDensity = context.getResources().getDisplayMetrics().scaledDensity; return (int) (spValue*scaleDensity+0.5f); } }
繼承View
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; /** * Created by 王 on 2017/10/21. */ 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.colorAccent)); mRingWidth = a.getFloat(R.styleable.CountDownView_ringWidth, 40); mRingProgessTextSize = a.getDimensionPixelSize(R.styleable.CountDownView_progressTextSize, DisplayUtil.sp2px(context, 20)); mProgessTextColor = a.getColor(R.styleable.CountDownView_progressTextColor, context.getResources().getColor(R.color.colorAccent)); 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; } /** * 開始倒計(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(); } }
布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.circulardemo.MainActivity"> <com.example.circulardemo.CountDownView android:id="@+id/cdv" android:layout_width="50dp" android:layout_height="50dp" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout>
Mainactivity
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CountDownView countDownView = (CountDownView) findViewById(R.id.cdv); //啟動(dòng) countDownView.startCountDown(); countDownView.setAddCountDownListener(new CountDownView.OnCountDownFinishListener() { @Override public void countDownFinished() { Toast.makeText(MainActivity.this, "倒計(jì)時(shí)結(jié)束", Toast.LENGTH_SHORT).show(); } }); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)漸變圓環(huán)、圓形進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)漸變圓環(huán)、圓形進(jìn)度條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Android在啟動(dòng)adb時(shí)失敗解決方案
這篇文章主要介紹了Android在啟動(dòng)adb時(shí)失敗解決方案的相關(guān)資料,需要的朋友可以參考下2015-02-02EditText實(shí)現(xiàn)輸入限制和校驗(yàn)功能實(shí)例代碼
本文通過(guò)實(shí)例代碼給大家介紹EditText實(shí)現(xiàn)輸入限制和校驗(yàn)功能,感興趣的朋友參考下吧2017-08-08BootStrapValidator與My97日期校驗(yàn)的實(shí)例代碼
這篇文章給大家介紹了bootstrapvalidator與my97日期校驗(yàn)的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-01-01Android應(yīng)用的Material設(shè)計(jì)的布局兼容性的一些要點(diǎn)總結(jié)
這篇文章主要介紹了Android應(yīng)用的Material設(shè)計(jì)的布局兼容性的一些要點(diǎn)總結(jié),文中還給了一個(gè)RecyclerView布局管理的例子,需要的朋友可以參考下2016-04-04