android 自定义view--点击屏幕生成圆点,点击圆点可以拖动

package com.minibox.mycpp;

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

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;

/**
 * Created by lingbeijing on 2020/9/5.
 */
public class DragBoll extends View implements GestureDetector.OnGestureListener {
     
    
    private Paint mPaint;
    private float point_x;
    private float point_y;
    private final float radio = 43.0f;
    private RectF targetRectF;
    private List<RectF> mCircles;
    
    private GestureDetector mDetector;
    public DragBoll(Context context) {
     
        super(context);
    }

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

    public DragBoll(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
     
        this(context, attrs, defStyleAttr,0);
    }

    public DragBoll(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
     
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
     

        if(mDetector.onTouchEvent(event)){
     
            return true;
        }
        switch (event.getAction()){
     
            case MotionEvent.ACTION_DOWN:
                targetRectF = getTarget(event);
                break;
            case MotionEvent.ACTION_MOVE:
                point_x = event.getX();
                point_y = event.getY();
                //在触摸点位置为中心绘制圆
                Log.i("lingbj", "draw:");
                if(targetRectF != null) {
     
                    targetRectF.left = point_x - radio;
                    targetRectF.right = point_x + radio;
                    targetRectF.bottom = point_y + radio;
                    targetRectF.top = point_y - radio;
                    invalidate();
                    
                }
                break;
            case MotionEvent.ACTION_CANCEL:
                point_x = 0;
                point_y = 0;
                targetRectF = null;
                invalidate();
                break;
        }
        return true;
    }

    private void init(){
     
        mPaint = new Paint();
        mCircles = new ArrayList<>();
        mDetector = new GestureDetector(getContext(),this);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.GREEN);
        mPaint.setStrokeWidth(5);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    }

    private RectF getTarget(MotionEvent event){
     
        for(RectF rect: mCircles){
     
            if(rect.contains(event.getX(), event.getY())) {
     
                return rect;
            }
        }
        return null;
    }
    @Override
    public void draw(Canvas canvas) {
     
        super.draw(canvas);
        for (RectF rect : mCircles) {
     
            canvas.drawRoundRect(rect, radio, radio, mPaint);
        }
    }

    @Override
    public boolean onDown(MotionEvent motionEvent) {
     
        return false;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {
     

    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
     

        Log.i("lingbj", "onSingleTapUp");
        if (mCircles.size() < 10) {
     
            float x = motionEvent.getX();
            float y = motionEvent.getY();
            RectF rectF = new RectF();
            rectF.left = x - radio;
            rectF.right = x + radio;
            rectF.bottom = y + radio;
            rectF.top = y - radio;
            mCircles.add(rectF);
            invalidate();
        }
        return true;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
     
        return false;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {
     

    }

    @Override
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
     
        return false;
    }
}

你可能感兴趣的:(android,绘图,拖动,java,android)