使用clipRect - 解释

发布于 2024-09-12 02:18:40 字数 660 浏览 4 评论 0原文

public class POCII extends Activity { 

    myView mv = new myView(this); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(mv); 
    }
}


class myView extends View { 

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

    @Override 
    public void onDraw(Canvas canvas) { 

        Paint paint = new Paint(); 

        canvas.drawRect(0,0,100,100, paint); 
        canvas.clipRect(0,0,50,50);
    } 
}

我的问题是,上面的代码不应该绘制一个矩形,然后裁剪左上角部分吗?矩形没有被裁剪。

请解释一下clipRect的作用。它实际上剪辑了什么?给定坐标,它是否以矩形的形式剪辑?如果是这样,为什么上面的代码不起作用?

public class POCII extends Activity { 

    myView mv = new myView(this); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(mv); 
    }
}


class myView extends View { 

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

    @Override 
    public void onDraw(Canvas canvas) { 

        Paint paint = new Paint(); 

        canvas.drawRect(0,0,100,100, paint); 
        canvas.clipRect(0,0,50,50);
    } 
}

My question is, shouldn't the above code draw a rectangle and then crop the top left portion? The rectangle is not getting cropped.

Please explain what clipRect does. What is it actually clipping? Does it clip in the form of a rectangle, given the co-ordinates? If so, Why is the above code not working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

能否归途做我良人 2024-09-19 02:18:40

画布.clipRect(left, top, right, Bottom) 减少了未来绘制操作可以写入的屏幕区域。它将clipBounds 设置为当前剪切矩形与指定矩形的空间交集。 ClipRect 方法有很多变体,它们接受不同形式的区域并允许对剪切矩形进行不同的操作。如果您想显式设置剪切区域,请尝试:

canvas.clipRect(left, top, right, bottom, Region.Op.REPLACE);

第 5 个参数表示替换剪切矩形,而不是创建与先前版本的交集。

尝试将clipRect 语句移到drawRect 语句之前。或者,尝试

paint.setColor(Color.YELLOW);
drawRect(0,0,75,75);

在现有的 ClipRect 语句之后添加:。它应该在您之前的区域上绘制一个 50x50 的黄色方块。

另一个注意事项:(在对明显的、很大程度上未记录的 View/ViewGroup/绘图代码感到长期沮丧之后)我发现 canvas.translate(x,y) 也调整了 ClipRect。 ClipRect和绘图矩阵的交互非常混乱。指出以下内容会有所帮助:

canvas.getMatrix()

以及

canvas.getClipBounds()

在画布修改之前和之后以及在绘制内容之前。

Canvas.clipRect(left, top, right, bottom) reduces the region of the screen that future draw operations can write to. It sets the clipBounds to be the spacial intersection of the current clipping rectangle and the rectangle specified. There are lot of variants of the clipRect method that accept different forms for regions and allow different operations on the clipping rectangle. If you want to explicitly set the clipping region, try:

canvas.clipRect(left, top, right, bottom, Region.Op.REPLACE);

The 5th argument means replace the clipping rectangle rather than creating the intersection with the previous version.

Try moving the clipRect statement before the drawRect statement. Or, try adding:

paint.setColor(Color.YELLOW);
drawRect(0,0,75,75);

after your existing clipRect statement. It should draw a 50x50 yellow square over what you had before.

Another note: (after long frustration with the apparently, largely undocumented View/ViewGroup/drawing code) I found that canvas.translate(x,y) also adjusts the clipRect. The interaction of clipRect and the drawing matrix is very confusing. It is helpful to point out:

canvas.getMatrix()

and

canvas.getClipBounds()

before and after modifications to the canvas and before drawing things.

披肩女神 2024-09-19 02:18:40

要裁剪左上角部分,请执行以下操作:

canvas.clipRect(0,0,50,50, Region.Op.DIFFERENCE);
// secondly...
canvas.drawRect(0,0,100,100, paint); 

To crop the top left portion, do:

canvas.clipRect(0,0,50,50, Region.Op.DIFFERENCE);
// secondly...
canvas.drawRect(0,0,100,100, paint); 
陌上芳菲 2024-09-19 02:18:40

ICS及以上...

XOR、Difference 和 ReverseDifference 剪辑模式是
如果启用硬件加速,则被 ICS 忽略。

只需在您的视图中禁用 2D 硬件加速即可:

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

参考Android:如何在API15中使用clipRect

ICS and above ...

XOR, Difference and ReverseDifference clip modes are
ignored by ICS if hardware acceleration is enabled.

Just disable 2D hardware acceleration in your view:

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

Reference Android: Howto use clipRect in API15

蓝戈者 2024-09-19 02:18:40

您的绘图在不使用cliprect的情况下看起来像这样:

在此处输入图像描述

现在,如果我们使用剪贴矩形,我们将在已有的内容上覆盖一个矩形。它有点看不见。假设我们调用了以下内容:

 override fun onDraw(canvas: Canvas) {

        val paint =  Paint();
        paint.color = Color.RED
        canvas.clipRect(0f,0f,500f,500f, Region.Op.DIFFERENCE);
// secondly...
        canvas.drawRect(0f,0f,1000f,1000f, paint);
    }

因为我们使用了 DIFFERENCE 选项,并且我们知道剪切矩形现在位于画布红色矩形之上,所以我们可以告诉我一些特殊的事情。上面说我们应该保持剪切矩形和原始矩形之间的差异。所以它看起来像这样(因为我使用了 1000 的一半来剪切矩形):

在此处输入图像描述

,如果我们使用 intersect,则相反将如下所示:
输入图片这里的描述

我很想看看是否有人可以让它做圆角。

your drawing looks like this without using cliprect:

enter image description here

now if we use a cliprect we are putting a overlay of a rectange over what we already have. its sort of invisible. lets say we called the following:

 override fun onDraw(canvas: Canvas) {

        val paint =  Paint();
        paint.color = Color.RED
        canvas.clipRect(0f,0f,500f,500f, Region.Op.DIFFERENCE);
// secondly...
        canvas.drawRect(0f,0f,1000f,1000f, paint);
    }

since we use DIFFERENCE option and we know the clipping rectangle is now OVER our canvas red rectangle we can tell me special things. above says we should KEEP the DIFFERENCE between the clipping rectangle and the original. so it will look like this (since i used half of 1000 for clipping rectangle):

enter image description here

and the opposite if we used intersect would look like this:
enter image description here

i'd love to see if someone can make it do rounded corners.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文