在画布绘制的组件上编写onTouch事件

时间:2018-03-17 20:50:38

标签: android events canvas view custom-view

我需要在矩形上创建一个onTouch事件,但是当我触摸与其重叠的圆圈时,我不希望激活触摸事件。 以下是截图:
enter image description here

当我触摸矩形时,我想要一个动作,但是如果我触摸圆圈,我不希望它发生。 以下是此视图的代码:

  public class SquareViewContener extends View {
    public static final int CIRCLE_RADIUS = 150;

    Paint paint;
    RectF r;

    private int red;
    private int blue;
    private int green;
    private int color;
    public SquareViewContener(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.setDrawingCacheEnabled(true);
        r = new RectF(0, 0, 1000, 1000);
        red = 130; //Default green color
        green = 254; //Default green color
        blue = 200; //Default green color
        initPaint();
    }


    private void initPaint(){
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.rgb(this.red, this.green, this.blue));
        //0xFF82FEC8
    }
    public void reDraw(int red, int blue, int green){
        this.red = red;
        this.blue = blue;
        this.green = green;
        this.setPaintColor(red, blue, green);
        this.invalidate();
    }

    private void setPaintColor(int red, int blue, int green){
        this.color = Color.rgb(red, green, blue);
        paint.setColor(this.color);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.i("Event", "Repainting, color = " + Color.rgb(red, green, blue) + " , red = " + this.red + " , blue = " + this.blue + " , green = " + this.green);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.rgb(red, blue, green));
        canvas.drawRect(r, paint);

        Paint circlePaint = new Paint();
        circlePaint.setColor(Color.WHITE);
        canvas.drawCircle(0, 0, SquareViewContener.CIRCLE_RADIUS, circlePaint);

    }    

}

0 个答案:

没有答案