如何在Android的画的时候屏蔽掉一个简单的区域? [英] How to mask out a simple region when painting in Android?

查看:140
本文介绍了如何在Android的画的时候屏蔽掉一个简单的区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个简单的描述:想象一下,我给绘制在墙上的照片视图类,我想用切出一个窗口中绘制它。假设我扩展该视图类并覆盖其dispatchDraw()方法来做到以下几点。首先绘制背景(如果有),以透过窗户可以看到。接下来,我想以某种方式掩盖了矩形窗口区域,然后调用super.dispatchDraw()。最后,我想卸下面具,绘制站在窗口,使他们画了背景和墙壁都一个人。我怎样才能做到这一点?

下面是一些code,这似乎接近我所需要的:

  @覆盖
保护无效dispatchDraw(帆布进入){
    INT W = into.getWidth();
    INT H = into.getHeight();
    INT W3 = W / 3;
    INT H3 = H / 3;
    into.drawLine(0,0,W,H,mPaint);
    保护区=新地区(W / 3,H / 3,2 * W / 3,2 * H / 3);
    into.cli pregion(保护,Op.INTERSECT);
    into.drawColor(Color.MAGENTA); //或super.dispatchDraw()在这里。
}

这给了我这样的:

这是那种我想要的对面。注意评为code上面的保护区域。我想品红色填充到的在该地区除了的无处不在发生。具体来说,就是我希望看到的是这样的:

在窗口的比喻,我应该再在一个位置,以解除限制,并绘制重叠两个窗口,墙上的正常方式的人或东西。

编辑:这是拉杰什CP答案的简化工作版本。我还添加了绘制在一切红色的前台的条纹,最后才能证明我可以删除限制以及添加它们。拉杰什谢谢!

 公共类MaskTest延伸活动{    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(新ClippView(getApplicationContext()));
    }    私有类ClippView扩展视图{
        私人油漆line_paint =新的油漆();
        私人油漆strip_paint =新的油漆();        公共ClippView(上下文的背景下){
            超级(上下文);
            line_paint.setColor(Color.GRAY);
            line_paint.setStrokeWidth(20);
            strip_paint.setColor(Color.RED);
        }        @覆盖
        保护无效的onDraw(帆布油画){
            super.onDraw(画布);
            INT W = canvas.getWidth();
            INT H = canvas.getHeight();
            INT W3 = W / 3;
            INT H3 = H / 3;
            //此行再presents一些未知的情况下,我们绘制了过来。
            canvas.drawLine(0,0,W,H,line_paint);
            //保护再presents一些区域,直到希望不是油漆过。
            保护区=新区域(W3,H3,2 * W / 3,2 * H / 3);
            canvas.cli pregion(保护,Op.DIFFERENCE);
            //油漆品红在整个画布,避免受保护区域。
            canvas.drawColor(Color.MAGENTA);
            //删除受保护的区域。
            所有地区=新区域(0,0,W,H);
            canvas.cli pregion(所有,Op.UNION);
            //画一个广阔的应用前景条在整个画布。
            canvas.drawRect(重量/ 2 - W / 20,0,W / 2 + W / 20,小时,strip_paint);
        }
    }
}


解决方案

 公共类ClippView扩展视图{
    民营涂料粉刷=新的油漆();    公共ClippView(上下文的背景下){
        超级(上下文);
    }
    私人保护区;
    / *
     *(非Javadoc中)
     * @see android.view.View#的onDraw(android.graphics.Canvas)
     * @since 2013年4月12日
     * @author rajeshcp
     * /
    @覆盖
    保护无效的onDraw(帆布油画){
        super.onDraw(画布);
        // view.buildDrawingCache();
        INT W = canvas.getWidth();
        INT H = canvas.getHeight();
        INT W3 = W / 3;
        INT H3 = H / 3;
        canvas.drawLine(0,0,W,H,油漆);
        保护=(保护== NULL)?新的地区(W3,H3,2 * W / 3,2 * H / 3):保护;
        canvas.cli pregion(保护,Op.DIFFERENCE);
        canvas.drawColor(Color.MAGENTA);
    }}

为此,我想这是你想要的。

Here is a simplified description: Imagine that I'm given a View class that draws a picture of a wall and I want to draw it with a window cut out. Assume that I extend that View class and override its dispatchDraw() method to do the following. First draw the background if any to be seen through the window. Next I want to mask out the rectangular window region somehow and then call super.dispatchDraw(). Finally I want to remove the mask and draw a person standing at the window so that they are painted over both the background and the wall. How can I do this?

Here is some code that seems close to what I need:

@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.
}

That gives me this:

Which is sort of the opposite of what I want. Notice the Region named "protect" in the code above. I want the magenta fill to happen everywhere except in that region. Specifically, what I want to see is this:

In the analogy of the window, I should then be in a position to remove the restriction and draw a person or something in the normal way that overlaps both the window and the wall.

EDIT: Here is a simplified working version of the answer by Rajesh CP. I also added a red "foreground" stripe drawn over everything at the end to show that I can remove the restriction as well as add them. Thanks 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);
        }
    }
}

解决方案

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);


    }

}

Do this I think this is what you want.

这篇关于如何在Android的画的时候屏蔽掉一个简单的区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆