自定义View画圆跳转

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();
            }
        });
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值