Android自定义View之倒计时分秒

项目中可能会需要倒计时的效果,今天给大家带来一个自定义倒计时:时分秒,分秒,秒三种效果:
Android自定义View之倒计时分秒_第1张图片
Android自定义View之倒计时分秒_第2张图片
Android自定义View之倒计时分秒_第3张图片

1,上代码:

package com.gyq.countdown.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Chronometer;

import java.text.SimpleDateFormat;

/**
 * 倒计时分秒
 * author: gyq
 * create at 2017/2/15 11:04
 */
public class CountDownView extends Chronometer{
    private long mTime;
    private long mNextTime;
    private OnTimeCompleteListener mListener;
    private SimpleDateFormat mTimeFormat;

    public CountDownView(Context context) {
        super(context);
    }

    public CountDownView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mTimeFormat = new SimpleDateFormat("hh:mm:ss");
        this.setOnChronometerTickListener(listener);
    }

    //重新启动计时
    public void reStart(long _time_s)
    {
        if (_time_s == -1)
        {
            mNextTime = mTime;
        }
        else
        {
            mTime = mNextTime = _time_s;
        }
        this.start();
    }

    public void reStart()
    {
        reStart(-1);
    }

    //不建议方法名用onResume()或onPause(),容易和activity生命周期混淆
    //继续计时
    public void onResume()
    {
        this.start();
    }

    //暂停计时
    public void onPause()
    {
        this.stop();
    }

    /**
     * 设置时间格式
     *
     * @param pattern 计时格式
     */
    public void setTimeFormat(String pattern)
    {
        mTimeFormat = new SimpleDateFormat(pattern);
    }

    public void setOnTimeCompleteListener(OnTimeCompleteListener l)
    {
        mListener = l;
    }

    OnChronometerTickListener listener = new OnChronometerTickListener()
    {
        @Override
        public void onChronometerTick(Chronometer chronometer)
        {
            if (mNextTime <= 0)
            {
                if (mNextTime == 0)
                {
                    CountDownView.this.stop();
                    if (null != mListener)
                        mListener.onTimeComplete();
                }
                mNextTime = 0;
                updateTimeText();
                return;
            }

            mNextTime--;

            updateTimeText();
        }
    };

    //初始化时间(秒)
    public void initTime(long _time_s)
    {
        mTime = mNextTime = _time_s;
        updateTimeText();
    }

    //初始化时间(分秒)
    public void initTime(long _time_h,long _time_m,long _time_s) {
        initTime(_time_h*3600+_time_m * 60 + _time_s);
    }

    private void updateTimeText()
    {
        this.setText(FormatMiss(mNextTime));
    }

    // 将秒转化成小时分钟秒
    public String FormatMiss(long miss){
        String hh=miss/3600>9?miss/3600+"":"0"+miss/3600;
        String  mm=(miss % 3600)/60>9?(miss % 3600)/60+"":"0"+(miss % 3600)/60;
        String ss=(miss % 3600) % 60>9?(miss % 3600) % 60+"":"0"+(miss % 3600) % 60;
        return hh+":"+mm+":"+ss;
    }

    public interface OnTimeCompleteListener
    {
        void onTimeComplete();
    }
}

修改FormatMiss方法的返回值,就可以改变显示效果。
2,使用:
首先布局:

<com.gyq.countdown.view.CountDownView
        android:id="@+id/countdown_timer_hour"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:textColor="#88000000"
        android:layout_gravity="center_horizontal"
        android:layout_height="wrap_content"
        android:textSize="16sp"/>

    

package com.gyq.countdown;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.gyq.countdown.view.CountDownView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private CountDownView mTimerHour,mTimerMin,mTimerS;
private Button mStart_h,mStart_m,mStart_s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

    initView();

    initListener();

    mTimerHour.initTime(1,30,0);

    mTimerMin.initTime(0,20,0);

    mTimerS.initTime(30);

}

private void initView() {
    mTimerHour = (CountDownView)findViewById(R.id.countdown_timer_hour);
    mTimerMin = (CountDownView)findViewById(R.id.countdown_timer_min);
    mTimerS = (CountDownView)findViewById(R.id.countdown_timer_s);

    mStart_h = (Button)findViewById(R.id.btn_start_hour);
    mStart_m = (Button)findViewById(R.id.btn_start_min);
    mStart_s = (Button)findViewById(R.id.btn_start_s);
}

private void initListener() {
    mStart_h.setOnClickListener(this);
    mStart_m.setOnClickListener(this);
    mStart_s.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_start_hour :
            mTimerHour.start();
            mTimerHour.setOnTimeCompleteListener(new CountDownView.OnTimeCompleteListener() {
                @Override
                public void onTimeComplete() {
                    Toast.makeText(MainActivity.this, "计时完成!", Toast.LENGTH_LONG).show();
                }
            });
            break;

        case R.id.btn_start_min :
            mTimerMin.start();
            mTimerMin.setOnTimeCompleteListener(new CountDownView.OnTimeCompleteListener() {
                @Override
                public void onTimeComplete() {
                    Toast.makeText(MainActivity.this, "计时完成!", Toast.LENGTH_LONG).show();
                }
            });
            break;

        case R.id.btn_start_s :
            mTimerS.start();
            mTimerS.setOnTimeCompleteListener(new CountDownView.OnTimeCompleteListener() {
                @Override
                public void onTimeComplete() {
                    Toast.makeText(MainActivity.this, "计时完成!", Toast.LENGTH_LONG).show();
                }
            });
            break;
    }
}

}

你可能感兴趣的:(自定义View)