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

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

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

以下是一些看起来与我需要的代码相近的代码:

@OverrIDeprotected voID dispatchDraw(Canvas into) {    int w = into.getWIDth();    int h = into.getHeight();    int w3 = w / 3;    int h3 = h / 3;    into.drawline(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.}

这给了我这个:

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

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

编辑:这是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,line_paint);            // 'protect' represents some area to not paint over until desired.            Region protect = new Region(w3,h3,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,h);            canvas.clipRegion(all,Op.UNION);            // Draw a wIDe foreground strip over the entire canvas.            canvas.drawRect(w / 2 - w / 20,w / 2 + w / 20,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,paint);        protect = (protect == null) ? new Region(w3,2 * h / 3) : protect;        canvas.clipRegion(protect,Op.DIFFERENCE);        canvas.drawcolor(color.magenta);    }}

这样做我认为这就是你想要的.

总结

以上是内存溢出为你收集整理的如何在Android中绘画时屏蔽一个简单的区域?全部内容,希望文章能够帮你解决如何在Android中绘画时屏蔽一个简单的区域?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1129215.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存