使用CLI preCT - explantionCLI、preCT、explantion

2023-09-12 23:56:20 作者:最后的疼爱是手放开ゝ

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

我的问题是,应该不是上述code绘制一个矩形,然后裁剪左上部分?该矩形没有得到裁剪。

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

请解释一下CLI preCT一样。什么是它实际上剪辑?它夹在一矩形的形式,鉴于坐标?如果是这样,为什么上面的code不工作?

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?

推荐答案

画布。cli$p$pct(left,顶部,右侧和底部)减少了未来绘制操作可以写入到屏幕的区域。它集clipBounds是当前剪贴矩形的空间交点和指定的矩形。有不少接受不同形式的地区,允许在剪辑矩形不同操作的CLI preCT方法的变体。如果你想明确地设置裁剪区域,请尝试:

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

第五参数是指更换剪辑矩形,而不是创造的previous版本的交集。

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

尝试drawRect的语句之前移动CLI preCT声明。或者,尝试添加:

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

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

您现有的CLI preCT后声明。它应该画一个50×50平方黄了,你有什么了。

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

另注:(长沮丧的明显,主要是无证查看/ ViewGroup中/绘图code)后,我发现,canvas.translate(X,Y),同时也将调整CLI $ P $厘。的CLI preCT和绘图基质的相互作用是非常混乱。这是有帮助的打印出来:

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() and canvas.getClipBounds()

前和修改后的画布和绘图的事情了。

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