countTime
Custom View countdown time, support for custom text, background support circular, rectangular, and rounded corners (source address below)
First, look at the results
You can customize the background and can be customized time text style
The default implementation of this style, such as 12:26:34
public interface TimeViewListener {
default String[] getTextStyle(int[] times){
return new String[]{times[1] + "", ":", times[2] + "", ":", times[3] + "", ""};
}
}
It can be achieved through inheritance Custom TimeViewListener text style, for example, 4 days, 12 hours 3 minutes 23 seconds so
public class CountDownDefault implements TimeViewListener{
@Override
public String[] getTextStyle(int[] times) {
return new String[]{times[0] + "", "天", times[1] + "", "小时",times[2] + "", "分", times[3] + "", "秒"};
}
}
Currently supports three styles, by setting time_background_type property, you can set the background, circles, rectangles style
@interface TimeBgType {
/**
* 圆
*/
int CIRCLE = 10;
/**
* 圆角矩形
*/
int ROUND_CIRCLE = 11;
/**
* 自定义
*/
int CUSTOM = 12;
}
Here optimized, add a map you can maintain multiple time countdown
/**
* 添加并启动倒计时任务。注意:<br/>
* 1, 不能重复添加参数uniqueTag值相同的倒计时任务。<br/>
* 2, 调用该函数后,一定要调用 {@link #remove(String)},否则会导致内存泄漏。<br/>
*
* @param hook 倒计时回调
*/
public void start(String uniqueTag, AbsCountdownHook hook) {
if ((!TextUtils.isEmpty(uniqueTag)) && (hook != null)) {
// checkEnvironment(uniqueTag, hook);
initDefaultBeginTime(hook);
getHooksOrCreateIfNull().put(uniqueTag, hook);
startTickJob();
}
}
TextViewTimer4 custom view implementation class, only drawing view, the operation is not the countdown
public class TextViewTimer4 extends View {
......
/**
* @param canvas 绘制倒计时
*/
private void drawTimeView(Canvas canvas) {
if (times == null) {
return;
}
String[] stringTimes=countDown.getTextStyle(times);
float width = mBgWidth;
float left = 0;
float right;
float timeViewWidth = calculateTimeViewWidth(stringTimes, width, mSepWidth);
left = ((getWidth() - timeViewWidth) / 2) < 0 ? 0 : ((getWidth() - timeViewWidth) / 2);// 初始时绘制时间的起始位置
right = left + width;// 初始时右侧位置
for (int i = 0; i < stringTimes.length; i++) {
if (i % 2 != 0) {
float textWidth=mSepPaint.measureText(stringTimes[i]);
left =left+ textWidth/2+mSepWidth/2;
canvas.drawText(stringTimes[i], left, mBaseline, mSepPaint);
left =left+textWidth/2+mSepWidth/2;
right +=mSepWidth+textWidth;
} else {
final String timeStr = fixTimeString(stringTimes[i]);
//绘制背景色
mRadiusPaint.setColor(mTimeTextBg);
if (mTimeBgType == TimeBgType.CIRCLE) {
drawCircleBg(canvas, left + mRadius, mBgHeight / 2);
} else if (mTimeBgType == TimeBgType.ROUND_CIRCLE) {
drawRoundCircleBg(canvas, left, right, mBgHeight);
} else {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),mBitmapId);
Bitmap mBitmap = Bitmap.createScaledBitmap(bitmap, (int)width, (int)mBgHeight, false);
canvas.drawBitmap(mBitmap, left, 0, mRadiusPaint);
drawTextTimeView(canvas, left + mBitmap.getWidth() / 2, timeStr);
bitmap.recycle();
mBitmap.recycle();
}
//绘制时间值
if (mTimeBgType != TimeBgType.CUSTOM) {
drawTextTimeView(canvas, left + mRadius, timeStr);
}
// 重新计算位置
left += width;
right += width;
}
}
}
......
}
Finally, use a custom layout in the XML file
<com.example.win7.timertest.TextViewTimer4
android:id="@+id/tvFlashTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tvtime:colon_color="#ffffff"
tvtime:custom_background="@drawable/ic_detail_red_wish"
tvtime:sep_width="10dp"
tvtime:time_background="#FFEC8B"
tvtime:time_color="#ffffff"
tvtime:radius="5dp"
android:layout_marginTop="30dp"
tvtime:time_background_type="roundCircle"
tvtime:time_size="14sp"
android:layout_marginBottom="30dp"
tvtime:background_height="30dp"
tvtime:background_width="30dp"
/>
Under activity, write just fine
mFlashHook = new AbsCountdownHook(countdown) {
@Override
public TextViewTimer4 getCountdownView() {
return tvFlashTime;
}
@Override
public boolean isViewActive() {
return getContext() != null;
}
@Override
public void doTimeOver() {
/* if (mTimeOverListener != null) {
mTimeOverListener.doTimeOver();
}*/
Toast.makeText(TimeActivity.this,"时间到了",Toast.LENGTH_SHORT).show();
}
};
TimeViewListener timeViewListener=new CountDownDefault();
tvFlashTime.setListener(timeViewListener);
CountdownManager.getInstance().start("haha", mFlashHook);
Specific usage we can look at the code ha also welcome to help me test case whether the bug, to optimize future
Download Source address https://github.com/ccwccw123/countTime
to small series point change, small make it a reward! !