CountdownView

本文详细介绍了CountdownView组件的使用方法及定制选项,包括如何通过XML布局文件配置外观属性,以及如何通过Java代码实现动态展示和回调等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Github地址

CountdownView项目Github地址

Gradle

compile 'com.github.iwgang:countdownview:2.1.3'

Layout

<cn.iwgang.countdownview.CountdownView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:isHideTimeBackground="true"
    app:isShowDay="true"
    app:isShowHour="true"
    app:isShowMinute="true"
    app:isShowSecond="true"
    app:isShowMillisecond="true"
    app:timeTextColor="#000000"
    app:timeTextSize="22sp"
    app:isTimeTextBold="true"
    app:suffixGravity="bottom"
    app:suffixTextColor="#000000"
    app:suffixTextSize="12sp"
    app:suffixHour="时"
    app:suffixMinute="分"
    app:suffixSecond="秒"
    app:suffixMillisecond="毫秒" />

Customization

attrtypedefault
isHideTimeBackground(隐藏倒计时背景)booleantrue
timeBgColor(倒计时的背景色)color#444444
timeBgSize(倒计时背景大小)dimensiontimeSize + 2dp * 4
timeBgRadius(倒计时背景的圆角)dimension0
isShowTimeBgDivisionLine(倒计时的横向的分割线)booleantrue
timeBgDivisionLineColor(倒计时的横向的分割线颜色)color#30FFFFFF
timeBgDivisionLineSize(倒计时的横向的分割线高度)dimension0.5dp
timeTextSize(倒计时的文字大小)dimension12sp
timeTextColor(倒计时的文字颜色)color#000000
isTimeTextBold(倒计时文字所在的边框)booleanfalse
isShowDay(自动显示 (天 > 1 显示, = 0 隐藏))booleanauto show (has value show if not hide)
isShowHour(自动显示 (小时 > 1 显示, = 0 隐藏))booleanauto show (has value show if not hide)
isShowMinute(是否显示分钟)booleantrue
isShowSecond(是否显示秒)booleantrue
isShowMillisecond( 是否显示毫秒)booleanfalse
isConvertDaysToHours(是否转换天为小时)booleanfalse
suffixTextSize(添加的分号:的大小)dimension12sp
suffixTextColor(添加的分号:的颜色)color#000000
isSuffixTextBold(添加的分号:的边框)booleanfalse
suffixGravity(添加的分号:对齐方式)‘top’ or ‘center’ or ‘bottom’‘center’
suffix(添加的分号:默认值)string‘:’
suffixDay(天默认值)stringnull
suffixHour(时默认值)stringnull
suffixMinute(分默认值)stringnull
suffixSecond(秒默认值)stringnull
suffixMillisecond(毫秒默认值)stringnull
suffixLRMargindimensionleft 3dp right 3dp
suffixDayLeftMargindimension0
suffixDayRightMargindimension0
suffixHourLeftMargindimension0
suffixHourRightMargindimension0
suffixMinuteLeftMargindimension0
suffixMinuteRightMargindimension0
suffixSecondLeftMargindimension0
suffixSecondRightMargindimension0
suffixMillisecondLeftMargindimension0
isShowTimeBgBorderbooleanfalse
timeBgBorderColorcolor#000000
timeBgBorderSizedimension1dp
timeBgBorderRadiusdimension0

代码调用

CountdownView mCvCountdownView = (CountdownView)findViewById(R.id.cv_countdownViewTest1);
//倒计时开始传毫秒
mCvCountdownView.start(995550000); 

/**或者自己编写倒计时逻辑,然后调用updateShow来更新UI*/
for (int time=0; time<1000; time++) {
    mCvCountdownView.updateShow(time);
}

给控件添加Tag回调实例

当使用ListView 或者RecycleView等控件使用倒计时,会出现一个界面有多个倒计时,但它的回调函数在Activity、Fragment里面只有一个,这时我们可以选择添加Tag标签,回调函数onEnd里判断Tag值匹配执行响应函数,个人觉得在Adapter里面使用有个更好的方法,onEnd(CountdownView ,position)把position回调回来就简单多了,这里不细说,先看给控件添加Tag回调实例

    // 第1步,设置tag
    mCvCountdownView.setTag(R.id.name, uid);
    // 第2步,从设置的CountdownView中取回tag
    @Override
    public void onEnd(CountdownView cv) {
        Object nameTag = cv.getTag(R.id.uid);
        if (null != nameTag) {
            Log.i(TAG, "name = " + nameTag.toString());
        }
    }

动态展示

动态显示/隐藏某些时间 (如:开始显示时、分、秒,后面到指定时间改成分、秒、毫秒)

customTimeShow(boolean isShowDay, 
               boolean isShowHour,
               boolean  isShowMinute,
               boolean isShowSecond,
               boolean isShowMillisecond)

动态显示设置, 支持所有xml中的配置项来使用java代码设置

dynamicShow(DynamicConfig dynamicConfig)

倒计时结束后回调

setOnCountdownEndListener(OnCountdownEndListener onCountdownEndListener);

指定间隔时间回调

setOnCountdownIntervalListener(long interval, OnCountdownIntervalListener onCountdownIntervalListener);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值