当前位置: 首页 > 编程笔记 >

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

万俟铭
2023-03-14
本文向大家介绍Android 倒计时控件 CountDownView的实例代码详解,包括了Android 倒计时控件 CountDownView的实例代码详解的使用技巧和注意事项,需要的朋友参考一下

一个精简可自定义的倒计时控件,使用 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内容请搜索小牛知识库以前的文章或继续浏览下面的相关文章希望大家以后多多支持小牛知识库!

 类似资料:
  • 本文向大家介绍android倒计时控件示例,包括了android倒计时控件示例的使用技巧和注意事项,需要的朋友参考一下 本文为大家分享了android倒计时控件,供大家参考,具体代码如下 希望本文所述对大家学习Android软件编程有所帮助。

  • 本文向大家介绍VB实现的倒计时类代码详解,包括了VB实现的倒计时类代码详解的使用技巧和注意事项,需要的朋友参考一下 本文所述为用VB制作倒计时程序用到的一个Module类代码,是基于控制台的倒计时程序,可供VB初学者或者VB爱好者参考学习,当然读者也可以将其拷贝代码到VB工程里面直接使用,不过需要自己创建相关的代码,对于初学者来说,也是很容易看懂的一段代码。 具体功能代码如下:

  • 本文向大家介绍Android实现倒计时的按钮的示例代码,包括了Android实现倒计时的按钮的示例代码的使用技巧和注意事项,需要的朋友参考一下 最近有人问我如何实现倒计时的按钮功能,例如发送验证码,我记得有个CountDownTimer,因为好久没用过了,自己就写了一个,代码如下 点击按钮后开始倒计时,貌似很简单啊,但是运行起来发现有一些问题,先给大家看效果图 我们打印一下时间 这里我们可以看到8

  • 本文向大家介绍Android 自定义View之倒计时实例代码,包括了Android 自定义View之倒计时实例代码的使用技巧和注意事项,需要的朋友参考一下 Android 自定义View之倒计时实例代码 需求: 大多数app在注册的时候,都有一个获取验证码的按钮,点击后,访问接口,最终用户会收到短信验证码。为了不多次写这个获取验证码的接口,下面将它自定义成一个view,方便使用。 分析一下,这是一

  • 本文向大家介绍ReactNative短信验证码倒计时控件的实现代码,包括了ReactNative短信验证码倒计时控件的实现代码的使用技巧和注意事项,需要的朋友参考一下 由于最近刚开始认真的搞RN,可能有一些封装的不是最佳实践,还是希望大家多提意见,和大家一起进步吧。本文介绍了ReactNative短信验证码倒计时控件,分享给大家 功能 根据项目的需要,需要写一个自定义的控件,实现如下功能: 默认文

  • 本文向大家介绍Flutter倒计时/计时器的实现代码,包括了Flutter倒计时/计时器的实现代码的使用技巧和注意事项,需要的朋友参考一下 在我们实现某些功能时,可能会有倒计时的需求。 比如发送短信验证码,发送成功后可能要求用户一段时间内不能再次发送,这时候我们就需要进行倒计时,时间到了才允许再次操作。 如下图: 为了实现这样场景的需求,我们需要使用 Timer.periodic 。 一、引入Ti