Android自定义View倒计时圆
程序员文章站
2023-08-12 13:06:55
本文实例为大家分享了android自定义view倒计时圆的具体代码,供大家参考,具体内容如下
创建attr
本文实例为大家分享了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, 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); /** *圆环 */ //颜色 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(); } }); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。