使用clipRect - 解释 [英] Using clipRect - explanation

查看:21
本文介绍了使用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.

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

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?

推荐答案

Canvas.clipRect(left, top, right, bottom) 减少了未来绘制操作可以写入的屏幕区域.它将clipBounds 设置为当前裁剪矩形和指定矩形的空间交点.clipRect 方法有很多变体,它们接受不同的区域形式并允许对裁剪矩形进行不同的操作.如果要明确设置剪切区域,请尝试:

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

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

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

尝试在 drawRect 语句之前移动 clipRect 语句.或者,尝试添加:

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

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

在您现有的 clipRect 语句之后.它应该在你之前的东西上画一个 50x50 的黄色方块.

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

另一个注意事项:(在对显然没有记录的 View/ViewGroup/drawing 代码长期感到沮丧之后)我发现 canvas.translate(x,y) 也调整了 clipRect.clipRect 和绘制矩阵的交互非常混乱.指出以下几点很有帮助:

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

canvas.getClipBounds()

在修改画布之前和之后以及在绘制东西之前.

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

这篇关于使用clipRect - 解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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