最近很小的功能模塊上要顯示時間倒計時功能。功能比較小但是在裏面封裝了一個倒計時的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);
}
}