
本文实例为大家分享了AndroID自定义view倒计时圆的具体代码,供大家参考,具体内容如下
创建attr
<?xml version="1.0" enCoding="utf-8"?><resources> <declare-styleable name="CountDownVIEw"> <!--颜色--> <attr name="ringcolor" format="color" /> <!-- 进度文本的字体大小 --> <attr name="progresstextSize" format="dimension" /> <!-- 圆环宽度 --> <attr name="ringWIDth" format="float" /> <!--进度文本颜色--> <attr name="progresstextcolor" format="color"/> <!--倒计时--> <attr name="countdownTime" format="integer"/> </declare-styleable></resources>
创建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;//得到设备的密度 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; //圆轮进度值文本大小 private int mRingProgesstextSize; //宽度 private int mWIDth; //高度 private int mHeight; private Paint mPaint; //圆环的矩形区域 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,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); /** *圆环 */ //颜色 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; } /** * 开始倒计时 */ 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); //倒计时结束回调 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); //启动 countDownVIEw.startCountDown(); countDownVIEw.setAddCountDownListener(new CountDownVIEw.OnCountDownFinishListener() { @OverrIDe public voID countDownFinished() { Toast.makeText(MainActivity.this,"倒计时结束",Toast.LENGTH_SHORT).show(); } }); }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android自定义View倒计时圆全部内容,希望文章能够帮你解决Android自定义View倒计时圆所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)