使用clipRect-说明 [英] Using clipRect - explanation

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

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

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/绘图代码进行长时间的挫败之后),我发现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 print out:

canvas.getMatrix()

canvas.getClipBounds()

在修改画布之前和之后以及在绘制事物之前.

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

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

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