如何在Android中绘画时屏蔽一个简单的区域?

时间:2013-04-04 05:56:10

标签: android shape mask

这是一个简化的描述:想象一下,我给了一个View类,它绘制了一张墙的图片,我想用一个切出的窗口绘制它。假设我扩展了View类并覆盖其dispatchDraw()方法以执行以下操作。首先绘制背景,如果有任何要通过窗口看到。接下来我想以某种方式屏蔽矩形窗口区域,然后调用super.dispatchDraw()。最后,我想取下面具并画一个站在窗户上的人,这样他们就会在背景和墙壁上画画。我怎么能这样做?

以下是一些似乎与我需要的代码相近的代码:

@Override
protected void dispatchDraw(Canvas into) {
    int w = into.getWidth();
    int h = into.getHeight();
    int w3 = w / 3;
    int h3 = h / 3;
    into.drawLine(0, 0, w, h, mPaint);
    Region protect = new Region(w / 3, h / 3, 2 * w / 3, 2 * h / 3);
    into.clipRegion(protect, Op.INTERSECT);
    into.drawColor(Color.MAGENTA); // or super.dispatchDraw() here.
}

这给了我这个:

enter image description here

这与我想要的完全相反。请注意上面代码中名为“protect”的区域。我希望洋红色填充发生在该区域的之外的任何地方。具体来说,我想看到的是:

enter image description here

在窗口的类比中,我应该能够移除限制并以正常方式绘制一个人或某物,与窗户和墙壁重叠。

编辑:这是Rajesh CP答案的简化工作版本。我还在末尾的所有内容上添加了一个红色的“前景”条纹,以表明我可以删除限制并添加它们。谢谢Rajesh!

public class MaskTest extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new ClippView(getApplicationContext()));
    }

    private class ClippView extends View {
        private Paint line_paint = new Paint();
        private Paint strip_paint = new Paint();

        public ClippView(Context context) {
            super(context);
            line_paint.setColor(Color.GRAY);
            line_paint.setStrokeWidth(20);
            strip_paint.setColor(Color.RED);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            int w = canvas.getWidth();
            int h = canvas.getHeight();
            int w3 = w / 3;
            int h3 = h / 3;
            // This line represents some unknown background that we are drawing over.
            canvas.drawLine(0, 0, w, h, line_paint);
            // 'protect' represents some area to not paint over until desired.
            Region protect = new Region(w3, h3, 2 * w / 3, 2 * h / 3);
            canvas.clipRegion(protect, Op.DIFFERENCE);
            // Paint magenta over the entire canvas, avoiding the protected region.
            canvas.drawColor(Color.MAGENTA);
            // Remove the protected region.
            Region all = new Region(0, 0, w, h);
            canvas.clipRegion(all, Op.UNION);
            // Draw a wide foreground strip over the entire canvas.
            canvas.drawRect(w / 2 - w / 20, 0, w / 2 + w / 20, h, strip_paint);
        }
    }
}

2 个答案:

答案 0 :(得分:6)

public class ClippView extends View{
    private Paint paint= new Paint();

    public ClippView(Context context) {
        super(context);
    }


    private Region protect;
    /*
     * (non-Javadoc)
     * @see android.view.View#onDraw(android.graphics.Canvas)
     * @since Apr 12, 2013
     * @author rajeshcp
     */
    @Override
    protected void onDraw(Canvas canvas) { 
        super.onDraw(canvas);
        // view.buildDrawingCache();      


        int w = canvas.getWidth();
        int h = canvas.getHeight();
        int w3 = w / 3;
        int h3 = h / 3;
        canvas.drawLine(0, 0, w, h, paint);
        protect = (protect == null) ? new Region(w3, h3, 2 * w / 3, 2 * h / 3) : protect;
        canvas.clipRegion(protect, Op.DIFFERENCE);
        canvas.drawColor(Color.MAGENTA);


    }

}

这样做我认为这就是你想要的。enter image description here

答案 1 :(得分:-1)

为什么不像这样画四个矩形作为面具 View Mask