自定义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" />
自定义倒计时View
最新推荐文章于 2021-05-25 15:29:41 发布