手指拖动小球移动,点击空白处,小球不跟着移动

版权声明:叮叮叮!!!! https://blog.csdn.net/Xuexx_520/article/details/83685021
package com.example.xxx.circle;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.Random;

/**
 * date:2018/11/1
 * author:薛鑫欣(吧啦吧啦)
 * function:
 */
public class Cicle  extends View {
    private Paint paint;        //定义画笔
    private int radius = 20;
    private int mHeight,mWiddth;
    private int X,Y;//小球的位置
    private int x1,y1;//鼠标点击的位置
    //定义颜色数组
    private int colorArray[] = {Color.BLACK,Color.BLACK,Color.GREEN,Color.YELLOW, Color.RED};
    private int paintColor = colorArray[0]; //定义画笔默认颜色
    private boolean mFlag;

    public Cicle(Context context) {
        this(context,null);
    }
    public Cicle(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }
    public Cicle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //初始化画笔
        initPaint();
    }
    private void initPaint(){
        paint = new Paint();
        //设置消除锯齿
        paint.setAntiAlias(true);
        //设置画笔颜色
        paint.setColor(paintColor);
    }

    //绘制小球
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //将屏幕设置为白色
        canvas.drawColor(Color.WHITE);
        //随机设置画笔颜色
        setPaintRandomColor();
        //绘制小圆作为小球
        canvas.drawCircle(X, Y, radius, paint);
        

    }
    //为画笔设置随机颜色
    private void setPaintRandomColor(){
        Random rand = new Random();
        int randomIndex = rand.nextInt(colorArray.length);
        paint.setColor(colorArray[randomIndex]);
    }
 //设置小球在中心位置
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
       mHeight=this.getHeight();
       mWiddth=this.getWidth();
        X=mWiddth/2;
        Y=mHeight/2;


    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

       switch (event.getAction()){
           //当手指按下时计算是否点在圆内
           case MotionEvent.ACTION_DOWN:
               x1 = (int) event.getX();
               y1 = (int) event.getY();
               mFlag =jiSuan(x1,y1);
                break;
           case MotionEvent.ACTION_MOVE:

           case MotionEvent.ACTION_UP:
               if(mFlag){
                   // 抬起
                   X= (int) event.getX();
                   Y= (int) event.getY();
                   // 通知重绘
                   postInvalidate();
               }
               break;
       }

        return true;
    }

    private boolean jiSuan(int x1, int y1) {
        //利用勾股定义计算小球是否在圆内
       double sqrt=Math.sqrt((x1-X)*(x1-X)+(y1-Y)*(y1-Y));
        if(sqrt<radius){
             return true;
        }
            return false;


    }
}

猜你喜欢

转载自blog.csdn.net/Xuexx_520/article/details/83685021