canvas.clipPath()显示剪切视图但不能在android中查看后面的视图

时间:2018-03-22 05:11:18

标签: android canvas view

i want to create this type of view 我有Main Activity和一个DrawViewEdit画布类
所以为此我在main活动中添加了view1和view2,在view1和view2上我已经执行了canvas.clipPath() 我想在view1区域外执行点击操作,但结果我得到了view1如何获得View2点击? This is my issue

这是我的画布类

public class DrawViewEdit extends FrameLayout {

    Context context;
    Path path;
    JSONObject layersJsonObject;

    int displayWidth, displayHeight;
    int left, top,index;

    private void init() {
        setWillNotDraw(false);
    }

    public DrawViewEdit(Context context, JSONObject layersJsonObject, int displayHeight, int displayWidth,
                        int left, int top) {
        super(context);
        this.context = context;
        this.layersJsonObject = layersJsonObject;
        this.displayHeight = displayHeight;
        this.displayWidth = displayWidth;
        this.left = left;
        this.top = top;

        try {
            path = new Path();

            double w = (Float.parseFloat("" + displayWidth) / 720.0) * 2.0;
            double h = (Float.parseFloat("" + displayHeight) / 1080.0) * 2.0;

            path.rMoveTo(Float.valueOf("" + ((Integer.parseInt(layersJsonObject.getString("x1")) * w) + left)),
                    Float.valueOf("" + ((Integer.parseInt(layersJsonObject.getString("y1")) * h) + top)));
            path.lineTo(Float.valueOf("" + ((Integer.parseInt(layersJsonObject.getString("x2")) * w) + left)),
                    Float.valueOf("" + ((Integer.parseInt(layersJsonObject.getString("y2")) * h) + top)));
            path.lineTo(Float.valueOf("" + ((Integer.parseInt(layersJsonObject.getString("x3")) * w) + left)),
                    Float.valueOf("" + ((Integer.parseInt(layersJsonObject.getString("y3")) * h) + top)));
            path.lineTo(Float.valueOf("" + ((Integer.parseInt(layersJsonObject.getString("x4")) * w) + left)),
                    Float.valueOf("" + ((Integer.parseInt(layersJsonObject.getString("y4")) * h) + top)));
            path.lineTo(Float.valueOf("" + ((Integer.parseInt(layersJsonObject.getString("x1")) * w) + left)),
                    Float.valueOf("" + ((Integer.parseInt(layersJsonObject.getString("y1")) * h) + top)));
            path.close();

                    } catch (JSONException e1) {
            e1.printStackTrace();
        }
        init();

    }

    public DrawViewEdit(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DrawViewEdit(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(displayWidth, displayHeight);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.clipPath(path);
        super.draw(canvas);
    }
}

这是我的Activity类功能,我点击了

private void setViews() {
    try {
        JSONArray layersJsonArray = jsonObject.getJSONArray("layers");

        for (int i = 0; i < layersJsonArray.length(); i++) {
            JSONObject layerJsonObject = layersJsonArray.getJSONObject(i);

            drawView = new DrawViewEdit(EditActivity.this, layerJsonObject, displayHeight, displayWidth, left, top);
            drawView.setTag(i);

drawView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Toast.makeText(EditActivity.this, "" + view.getTag(), Toast.LENGTH_SHORT).show();
    }
});        
}    } catch (JSONException e) {
        e.printStackTrace();
    }
}

0 个答案:

没有答案