自定义View-----点击小球转动

public class MyView3 extends View implements View.OnClickListener{

    private Paint paint;
    private int width;
    private int height;
    private int centerX;
    private int centerY;
    private RectF rectF;
    private Paint pain;
    private int[] colors;
    private  int jieshu;

    public MyView3(Context context) {
        this(context,null);
    }

    public MyView3(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyView3(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        setOnClickListener(this);
        paint = new Paint();
        paint.setColor(Color.RED);
        rectF = new RectF(-200, -200, 200, 200);
        pain = new Paint();
        pain.setColor(Color.GRAY);
        //定义颜色
        colors = new int[]{Color.GREEN, Color.BLUE, Color.YELLOW,Color.CYAN};
        jieshu=360/colors.length;


    }

    //测量
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //测量实际的高度和宽度
        width = getMeasuredWidth();
        height=getMeasuredHeight();
        //放在屏幕的中间
        centerX=width/2;
        centerY=height/2;
    }

    //画笔
    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(centerX,centerY);
        //矩形   浮点型比整型更精确
        canvas.drawRect(rectF,paint);
        //圆形
        //canvas.drawCircle(0,0,200,pain);

        //旋转
        canvas.rotate(d);
        //弧形
        huxing(canvas);


    }

    //用弧形 进行循环  画出一个圆
    private void huxing(Canvas canvas) {
        int qishi=0;
        for (int i=0;i<colors.length;i++ ){
            pain.setColor(colors[i]);
            canvas.drawArc(rectF,qishi,jieshu,true,pain);
            qishi+=jieshu;
        }
    }
    //事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.e("==","ACTION_DOWN");
                break;
            case MotionEvent.ACTION_UP:
                Log.e("==","ACTION_UP");
                break;
            case MotionEvent.ACTION_MOVE:
                Log.e("==","ACTION_MOVE");
                break;
        }

        return super.onTouchEvent(event);
    }


//先让他旋转 60度   循环一次 +5
    int d=60;
    @Override
    public void onClick(View v) {
        TimerTask timer = new TimerTask() {
            @Override
            public void run() {
                d+=5;
                //子线程中刷新
                postInvalidate();
            }
        };
        Timer timer1 = new Timer();
        timer1.schedule(timer,2000,50);


    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值