Android实现自定义倒计时
程序员文章站
2024-01-06 23:30:40
最近工作中遇到个要做倒计时60秒的进度条,经过参考别人的资料做出来需求的效果。废话少说先来个效果:一定想知道是怎么实现的吧!下面是代码public class countdownview extend...
最近工作中遇到个要做倒计时60秒的进度条,经过参考别人的资料做出来需求的效果。废话少说先来个效果:
一定想知道是怎么实现的吧!下面是代码
public class countdownview extends view { //圆轮颜色 private int mringcolor; //默认圆颜色 private int mringnormalcolor ; //圆轮宽度 private float mringwidth; //圆轮进度值文本大小 private int mringprogesstextsize; //宽度 private int mwidth; //高度 private int mheight; private paint mpaint; private paint paintnormal; //圆环的矩形区域 private rectf mrectf; // private int mprogesstextcolor; private int mcountdowntime; private float mcurrentprogress; private oncountdownfinishlistener mlistener; private 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, context.getresources().getcolor(r.color.circle_progress)); mringwidth = a.getfloat(r.styleable.countdownview_ringwidth, 8); mringprogesstextsize = a.getdimensionpixelsize(r.styleable.countdownview_progresstextsize, displayutil.sp2px(context, 12)); mprogesstextcolor = a.getcolor(r.styleable.countdownview_progresstextcolor, context.getresources().getcolor(r.color.circle_progress)); mcountdowntime = a.getinteger(r.styleable.countdownview_countdowntime, 60); mringnormalcolor = a.getcolor(r.styleable.countdownview_ringcolor, context.getresources().getcolor(r.color._circle_progress)); a.recycle(); paintnormal = new paint(paint.anti_alias_flag); paintnormal.setantialias(true); 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); /** *默认圆环 */ //颜色 paintnormal.setcolor(mringnormalcolor); //空心 paintnormal.setstyle(paint.style.stroke); //宽度 paintnormal.setstrokewidth(mringwidth); canvas.drawarc(mrectf, 360, 360, false, paintnormal); 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 = 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(); } public void stopcountdown(){ valueanimator.end(); } }
然后新建一个attr.xml;
<?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>
这样一个自定义的view就写完了;那怎么用呢;布局就不说了;
lostime.setaddcountdownlistener(new countdownview.oncountdownfinishlistener() { @override public void countdownfinished() { 倒计时结束 } }); lostime.setcountdowntime(60); lostime.startcountdown();
这样就轻轻松松的跑起来了,希望能帮助到需要的你;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。