android实现倒计时动态圈
程序员文章站
2022-03-20 22:52:51
本文实例为大家分享了android实现倒计时动态圈的具体代码,供大家参考,具体内容如下效果是这样,没动图:布局:
本文实例为大家分享了android实现倒计时动态圈的具体代码,供大家参考,具体内容如下
效果是这样,没动图:
布局:
<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="跳过" android:textcolor="#ffffff" android:textsize="10sp" android:layout_height="130dp" /> </linearlayout>
values下新建一个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>
一个类,类名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; //圆轮进度值文本大小 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.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); /** *圆环 */ //颜色 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(); } }
activity中这样调用:
countdownview cdv = (countdownview) findviewbyid(r.id.tv_red_skip); cdv.setaddcountdownlistener(new countdownview.oncountdownfinishlistener() { @override public void countdownfinished() { //时间完了 干的事情 } }); cdv.startcountdown();
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Lua多行注释和取消多行注释的方法
下一篇: 米饭要蒸多久最合适(蒸大米的方法与步骤)