public class CountDownView extends View{ Paint paint; int time; float radius; int textColor; int bgColor; Handler handler; int cx,cy; public CountDownView(Context context) { super(context); init(context,null); } private void init(Context context,AttributeSet attrs) { if (attrs!=null){ TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CountDownView); time = typedArray.getInteger(R.styleable.CountDownView_CountDownTime, 3); radius = typedArray.getDimension(R.styleable.CountDownView_CountDownRaius, 25); textColor = typedArray.getColor(R.styleable.CountDownView_CountDownBgColor, Color.BLACK); bgColor = typedArray.getColor(R.styleable.CountDownView_CountDownBgColor,Color.WHITE); typedArray.recycle(); } else { time = 3; radius=30; textColor = Color.BLACK; bgColor=Color.WHITE; } paint =new Paint(); paint.setStyle(Paint.Style.STROKE); paint.setColor(bgColor); paint.setAntiAlias(true); paint.setStrokeWidth(2); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); cx=w/2; cy =h/2; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Path path = new Path(); path.addCircle(cx,cy,radius,Path.Direction.CW); canvas.drawPath(path,paint); paint.setColor(textColor); paint.setTextSize(25); paint.setTextAlign(Paint.Align.CENTER); canvas.drawText(time+"",cx,cy,paint); } public void setTime(int time) { this.time = time; invalidate(); } public void start(final Activity cur,final Class<?> next){ handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (time==0){ cur.startActivity(new Intent(cur,next)); cur.finish(); }else { time--; setTime(time); sendEmptyMessageDelayed(1,1000); } } }; handler.sendEmptyMessageDelayed(1,1000); } public void stop(){ if (handler!=null){ handler.removeCallbacksAndMessages(null); } } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction()==MotionEvent.ACTION_DOWN){ if (listener!=null){ listener.onClick(this); } } return super.onTouchEvent(event); } MyListener listener; public void setListener(MyListener listener) { this.listener = listener; } public interface MyListener{ void onClick(View view); } public CountDownView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(context,attrs); } public CountDownView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context,attrs); }
}
布局
<com.example.lenovo.zhangxin0708.CountDownView android:layout_width="65dp" android:layout_height="65dp" android:layout_margin="8dp" android:id="@+id/countView" android:layout_alignParentRight="true" android:layout_alignParentTop="true" />
/跳转页面
public class Sql extends AppCompatActivity { private CountDownView countDownView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sql); countDownView = findViewById(R.id.countView); countDownView.start(this,Sql.class); countDownView.setListener(new CountDownView.MyListener() { @Override public void onClick(View view) { countDownView.stop(); Intent intent = new Intent(Sql.this, MainActivity.class); startActivity(intent); finish(); } }); } }