最近很小的功能模块上要显示时间倒计时功能。功能比较小但是在里面封装了一个倒计时的handle了,还需要防止内存泄漏。于是做了一下小小的笔记
1.知识点:
1.继承布局的自定义view的实现。
自定义布局文件的三个构造函数调用场景
这个构造函数一般是被下面两个构造函数来调用,它的作用是当没有为自定义的属性赋值的时候,就可以使用defStyleAttr里面定义的默认属性值。
public TimerTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
在布局中使用时,系统new一个view时调用
public TimerTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
在代码中new 一个view对象时调用
public TimerTextView(Context context) {
super(context);
}
2.倒计时刷新功能实现
3.handle防止内存泄漏
2.下载地址
3.代码:
1.time_text_view.xml文件的编写
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:orientation="horizontal">
<TextView
android:id="@+id/countdown_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="@drawable/rounded_corner_white_bg"
android:gravity="center"
android:textColor="@color/white"
android:textSize="16sp" />
<TextView
android:id="@+id/countdown_unit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:textColor="@color/white"
android:textSize="16sp" />
</LinearLayout>
2.TimerTextView工具类的编写,继承自LinearLayout布局文件
public class TimerTextView extends LinearLayout{
// 时间变量
private long second;
private TextView tv_Time;
private TextView tv_Unit;
RefreshCallBack refreshCallBack;
public TimerTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context);
}
public TimerTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public TimerTextView(Context context) {
super(context);
initView(context);
}
private void initView(Context context) {
// 加载布局
LayoutInflater.from(context).inflate(R.layout.timer_text_view, this);
tv_Time = (TextView) findViewById(R.id.countdown_time);
tv_Unit = (TextView) findViewById(R.id.countdown_unit);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
// 在控件被销毁时移除消息
handler.removeMessages(0);
}
private boolean isRun = true; // 是否启动了
private Handler handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
if (isRun) {
if (second > 0) {
second = second - 1;
handler.sendEmptyMessageDelayed(0, 1000);
}else{
if(null != refreshCallBack){
refreshCallBack.refreshCallBack(true);
isRun = false;
}
}
}
break;
}
}
};
public boolean isRun() {
return isRun;
}
/**
* 开始计时
*/
public void start() {
isRun = true;
handler.removeMessages(0);
handler.sendEmptyMessage(0);
}
/**
* 结束计时
*/
public void stop() {
isRun = false;
}
public void diffTime(String endTime) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
String startTime = sdf.format(new Date());
String format = "yyyy-MM-dd hh:mm:ss";
//按照传入的格式生成一个simpledateformate对象
SimpleDateFormat sd = new SimpleDateFormat(format);
long nd = 1000 * 24 * 60 * 60;//一天的毫秒数
long nh = 1000 * 60 * 60;//一小时的毫秒数
long nm = 1000 * 60;//一分钟的毫秒数
long ns = 1000;//一秒钟的毫秒数long diff;try {
//获得两个时间的毫秒时间差异
long diff = 0;
try {
diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
} catch (ParseException e) {
e.printStackTrace();
}
if (diff < 0) {
if(null != refreshCallBack){
refreshCallBack.showCallBack(false);
}
return ;
} else {
if(null != refreshCallBack){
refreshCallBack.showCallBack(true);
}
long day = diff / nd;//计算差多少天
if (day > 0) {
tv_Time.setText(String.valueOf(day));
tv_Unit.setText("天");
} else {
long hour = diff % nd / nh;//计算差多少小时
if (hour > 0) {
tv_Time.setText(String.valueOf(hour));
tv_Unit.setText("小时");
} else {
long min = diff % nd % nh / nm;//计算差多少分钟
if (min > 0) {
tv_Time.setText(String.valueOf(min));
tv_Unit.setText("分钟");
} else {
second = diff%nd%nh%nm/ns;//计算差多少秒//输出结果
// if(min > 0){
// stringBuffer.append(sec+"秒");
// }
handler.removeMessages(0);
handler.sendEmptyMessage(0);
tv_Unit.setText("即将开始");
tv_Time.setVisibility(GONE);
}
}
}
}
}
public void setTextViewSize(int size){
if(null != tv_Time){
tv_Time.setTextSize(size);
}
if(null != tv_Unit){
tv_Unit.setTextSize(size);
}
}
public void setTextViewSpace(String type){
if("Big".equals(type)){
LinearLayout.LayoutParams lp2 = (LayoutParams) tv_Time.getLayoutParams();
lp2.setMargins(0, 0, DensityUtil.dip2px(tv_Time.getContext(), 12), 0);
tv_Time.setLayoutParams(lp2);
tv_Time.setBackground(getResources().getDrawable(R.drawable.bg_video_count_down));
}else if("Middle".equals(type)){
tv_Time.setPadding(12, 0, 12, 0);
LinearLayout.LayoutParams lp2 = (LayoutParams) tv_Time.getLayoutParams();
lp2.setMargins(0, 0,12, 0);
tv_Time.setLayoutParams(lp2);
}else {
tv_Time.setPadding(8, 0, 8, 0);
LinearLayout.LayoutParams lp2 = (LayoutParams) tv_Time.getLayoutParams();
lp2.setMargins(0, 0, 8, 0);
tv_Time.setLayoutParams(lp2);
}
}
public void setRefreshCallBack(RefreshCallBack refreshCallBack){
this.refreshCallBack = refreshCallBack;
}
public interface RefreshCallBack {
public void refreshCallBack(boolean flag);
public void showCallBack(boolean flag);
}
}