Android 倒計(jì)時(shí)控件 CountDownView的實(shí)例代碼詳解
一個(gè)精簡(jiǎn)可自定義的倒計(jì)時(shí)控件,使用 Canvas.drawArc() 繪制。實(shí)現(xiàn)了應(yīng)用開(kāi)屏頁(yè)的圓環(huán)掃過(guò)的進(jìn)度條效果。
代碼見(jiàn)https://github.com/hanjx-dut/CountDownView
使用
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.hanjx-dut:CountDownView:1.1'
}
實(shí)現(xiàn)的效果
效果圖

對(duì)應(yīng)的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="跳過(guò)" 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"/> <!-- 總時(shí)間 --> <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"/> <!-- 圓環(huán)寬度 --> <attr name="finished_color" format="color"/> <!-- 掃過(guò)完成的顏色 --> <attr name="unfinished_color" format="color"/> <!-- 未完成的顏色 --> <attr name="start_angle" format="float"/> <!-- 起始角度 默認(rèn) -90 即頂部 --> <attr name="clockwise" format="boolean"/> <!-- 順時(shí)針 默認(rèn) true --> <attr name="auto_start" format="boolean"/> <!-- 自動(dòng)開(kāi)始 默認(rèn) false --> <!-- 文字 --> <attr name="text" format="string"/> <!-- 設(shè)置文字 --> <attr name="text_mode"> <!-- 文字模式 固定 / 隨時(shí)間倒數(shù)(默認(rèn))--> <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>
文字部分沒(méi)有提供更多的自定義屬性,可以通過(guò) setTextDrawer()對(duì)畫(huà)筆和文字進(jìn)行自定義,如 demo 中的第五個(gè):
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 "跳過(guò)";
}
return String.format("%ss", leftTime == 0 ? leftTime : leftTime / 1000 + 1);
}
});
監(jiān)聽(tīng)
countDownView.setCountDownListener(new CountDownView.CountDownListener() {
@Override
public void onTick(long leftTime, float finishedAngle) {
// leftTime: 剩余時(shí)間, finishedAngle: 掃過(guò)的角度
}
@Override
public void onStop(boolean reset) {
// 主動(dòng)調(diào)用 countDownView.stop() 時(shí)會(huì)觸發(fā)此回調(diào)
}
@Override
public void onFinished() {
}
});
ps:接口都有默認(rèn)實(shí)現(xiàn),可以選擇實(shí)現(xiàn)任意方法
總結(jié)
到此這篇關(guān)于Android 倒計(jì)時(shí)控件 CountDownView的實(shí)例代碼詳解的文章就介紹到這了,更多相關(guān)Android 倒計(jì)時(shí)控件 CountDownView內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android實(shí)現(xiàn)啟動(dòng)頁(yè)倒計(jì)時(shí)效果
- Android 實(shí)現(xiàn)搶購(gòu)倒計(jì)時(shí)功能的示例
- android實(shí)現(xiàn)倒計(jì)時(shí)動(dòng)態(tài)圈
- android實(shí)現(xiàn)圓環(huán)倒計(jì)時(shí)控件
- android利用handler實(shí)現(xiàn)倒計(jì)時(shí)功能
- Android自定義view實(shí)現(xiàn)倒計(jì)時(shí)控件
- 解決Android-RecyclerView列表倒計(jì)時(shí)錯(cuò)亂問(wèn)題
- Android實(shí)現(xiàn)自定義倒計(jì)時(shí)
- Android倒計(jì)時(shí)神器(CountDownTimer)
- Android倒計(jì)時(shí)功能的實(shí)現(xiàn)代碼
- Android 簡(jiǎn)單實(shí)現(xiàn)倒計(jì)時(shí)功能
- Android自定義TimeButton實(shí)現(xiàn)倒計(jì)時(shí)按鈕
- Android實(shí)現(xiàn)倒計(jì)時(shí)的按鈕效果
- 利用Android設(shè)計(jì)一個(gè)倒計(jì)時(shí)組件
相關(guān)文章
Kotlin實(shí)現(xiàn)半圓形進(jìn)度條的方法示例
這篇文章主要給大家介紹了關(guān)于Kotlin實(shí)現(xiàn)半圓形進(jìn)度條的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03
Android 浮動(dòng)編輯框的具體實(shí)現(xiàn)代碼
本篇文章主要介紹了Android 浮動(dòng)編輯框的具體實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
android完美實(shí)現(xiàn) 拍照 選擇圖片 剪裁等代碼分享
本文給大家分享了2個(gè)安卓實(shí)現(xiàn)實(shí)現(xiàn) 拍照 選擇圖片 剪裁等的代碼,都是從正式項(xiàng)目中提取出來(lái)了,非常實(shí)用,有需要的小伙伴可以參考下。2016-01-01
Android應(yīng)用開(kāi)發(fā)中Fragment存儲(chǔ)功能的基本用法
這篇文章主要介紹了Android應(yīng)用開(kāi)發(fā)中使用Fragment存儲(chǔ)功能的基本用法,包括對(duì)Fragment的非中斷保存setRetaineInstance的講解,需要的朋友可以參考下2016-02-02
rxjava+retrofit實(shí)現(xiàn)多圖上傳實(shí)例代碼
本篇文章主要介紹了rxjava+retrofit實(shí)現(xiàn)多圖上傳實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
Android實(shí)現(xiàn)從本地圖庫(kù)/相機(jī)拍照后裁剪圖片并設(shè)置頭像
玩qq或者是微信的盆友都知道,這些聊天工具里都要設(shè)置頭像,一般情況下大家的解決辦法是從本地圖庫(kù)選擇圖片或是從相機(jī)拍照,然后根據(jù)自己的喜愛(ài)截取圖片,接下來(lái)通過(guò)本文給大家介紹Android實(shí)現(xiàn)從本地圖庫(kù)/相機(jī)拍照后裁剪圖片并設(shè)置頭像,需要的朋友參考下2016-02-02
Android開(kāi)發(fā)之MediaPlayer多媒體(音頻,視頻)播放工具類
這篇文章主要介紹了Android開(kāi)發(fā)之MediaPlayer多媒體(音頻,視頻)播放工具類,涉及Android針對(duì)音頻文件的讀取、播放、暫停、繼續(xù)等操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-12-12
使用PlatformView將?Android?控件view制作成Flutter插件
這篇文章主要為大家介紹了使用PlatformView將?Android?控件view制作成Flutter插件實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

