自定义倒计时View

自定义View

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2016/9/26.
 */
public class CountDownView extends TextView
{

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

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

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



    private String drawText;
    @Override
    protected void onDraw(Canvas canvas) {

        this.setText(drawText);
        super.onDraw(canvas);

    }


    private Long timeMllions;
    private SimpleDateFormat sdf;
    public void setTimeMllions(Long timeMllions, SimpleDateFormat sdf)
    {
           this.timeMllions=timeMllions;
           this.sdf=sdf;
    }

    private  Boolean isRefresh=true;
    public void start()
    {

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isRefresh) {
                    postInvalidateDelayed(1000);
                    timeMllions-=1000;
                    isRefresh=timeMllions>0?true:false;
                    drawText=sdf.format(new Date(timeMllions));
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                timeMllions = 0L;
                drawText=sdf.format(new Date(timeMllions));
            }
        }).start();

    }

}

引用

CountDownView countDownView= (CountDownView) findViewById(R.id.countView);
countDownView.setTimeMllions(90000L,new SimpleDateFormat("mm:ss"));
countDownView.start();

布局
<com.example.administrator.zxqdemo.CountDownView
    android:layout_width="500dp"
    android:layout_height="500dp"
    android:gravity="center"
    android:id="@+id/countView"
    android:textSize="25dp"
    />




 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值