当前位置: 移动技术网 > IT编程>移动开发>Android > Android 倒计时控件 CountDownView的实例代码详解

Android 倒计时控件 CountDownView的实例代码详解

2020年06月23日  | 移动技术网IT编程  | 我要评论

狂神续集,丑女也疯狂剧情介绍,陕西挖出千年棺木 竟出现诡异一幕

一个精简可自定义的倒计时控件,使用 canvas.drawarc() 绘制。实现了应用开屏页的圆环扫过的进度条效果。

代码见https://github.com/hanjx-dut/countdownview

使用

allprojects {
 repositories {
  ...
  maven { url 'https://jitpack.io' }
 }
}

dependencies {
 implementation 'com.github.hanjx-dut:countdownview:1.1'
}

实现的效果

效果图

对应的view:

 <com.hanjx.ui.countdownview
  android:id="@+id/count_down_1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  app:auto_start="true"
  app:text_mode="time_variant"
  app:duration="3000"
  app:paint_stroke="3dp"/>

 <com.hanjx.ui.countdownview
  android:id="@+id/count_down_2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  app:finished_color="#000000"
  app:auto_start="true"
  app:start_angle="90"
  app:text_mode="time_variant"
  app:duration="3000"
  app:paint_stroke="3dp"/>

 <com.hanjx.ui.countdownview
  android:id="@+id/count_down_3"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  app:finished_color="#ff0000"
  app:unfinished_color="#00ff00"
  app:auto_start="true"
  app:duration="2000"
  app:refresh_interval="quick"
  app:text="跳过"
  app:text_size="12sp"
  app:text_color="#ff0000"
  app:text_mode="fixed"
  app:paint_stroke="2dp"/>

 <com.hanjx.ui.countdownview
  android:id="@+id/count_down_4"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  app:auto_start="true"
  app:text_mode="fixed"
  app:clockwise="false"
  app:text=""
  app:duration="2000"
  app:paint_stroke="3dp"/>

 <com.hanjx.ui.countdownview
  android:id="@+id/count_down_5"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_gravity="center_horizontal"
  app:text_mode="time_variant"
  app:duration="5000"
  app:paint_stroke="1.5dp"/>

全部属性:

<declare-styleable name="countdownview">
  <attr name="duration" format="integer"/> <!-- 总时间 -->
  <attr name="refresh_interval"> <!-- 刷新间隔 ms -->
   <enum name="normal" value="16"/>
   <enum name="quick" value="11"/>
   <enum name="slow" value="20"/>
  </attr>
  <attr name="paint_stroke" format="dimension"/> <!-- 圆环宽度 -->
  <attr name="finished_color" format="color"/> <!-- 扫过完成的颜色 -->
  <attr name="unfinished_color" format="color"/> <!-- 未完成的颜色 -->
  <attr name="start_angle" format="float"/> <!-- 起始角度 默认 -90 即顶部 -->
  <attr name="clockwise" format="boolean"/> <!-- 顺时针 默认 true -->
  <attr name="auto_start" format="boolean"/> <!-- 自动开始 默认 false -->

  <!-- 文字 -->
  <attr name="text" format="string"/> <!-- 设置文字 -->
  <attr name="text_mode"> <!-- 文字模式 固定 / 随时间倒数(默认)-->
   <enum name="fixed" value="0"/>
   <enum name="time_variant" value="1"/>
  </attr>
  <attr name="text_size" format="dimension"/> <!-- 文字尺寸 -->
  <attr name="text_color" format="color"/> <!-- 文字颜色 -->
 </declare-styleable>

文字部分没有提供更多的自定义属性,可以通过 settextdrawer()对画笔和文字进行自定义,如 demo 中的第五个:

countdownview countdownview = findviewbyid(r.id.count_down_5);
countdownview.settextdrawer(new countdownview.textdrawer() {
 @override
 public void settextpaint(paint paint, long lefttime, int textmode) {
  if (lefttime < 2000) {
   paint.settextsize(sizeutils.sp2px(12));
  }
  paint.settypeface(typeface.default_bold);
  paint.setcolor(0xffff802e);
 }

 @override
 public string gettext(long lefttime, int mode, string origintext) {
  if (lefttime < 2000) {
   return "跳过";
  }
  return string.format("%ss", lefttime == 0 ? lefttime : lefttime / 1000 + 1);
 }
});

监听

countdownview.setcountdownlistener(new countdownview.countdownlistener() {
 @override
 public void ontick(long lefttime, float finishedangle) {
  // lefttime: 剩余时间, finishedangle: 扫过的角度
 }

 @override
 public void onstop(boolean reset) {
  // 主动调用 countdownview.stop() 时会触发此回调
 }

 @override
 public void onfinished() {

 }
});

ps:接口都有默认实现,可以选择实现任意方法

总结

到此这篇关于android 倒计时控件 countdownview的实例代码详解的文章就介绍到这了,更多相关android 倒计时控件 countdownview内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网