36

我正在移植一个在图形环境中编写的应用程序,该应用程序允许在剪切矩形的边界之外进行绘图。有什么方法可以在 Android 中做到这一点?

4

6 回答 6

50

尝试设置

android:clipChildren="false" 

到父视图

于 2013-11-21T10:38:10.130 回答
47

要在边界之外绘制,您需要扩展画布的 clipRect。

查看 Canvas 类上重载的 clipRect 方法。

注意 - 您需要指定区域操作,因为默认操作是 INTERSECT。所以是这样的:

Rect newRect = canvas.getClipBounds();
newRect.inset(-5, -5)  //make the rect larger

canvas.clipRect (newRect, Region.Op.REPLACE);
//happily draw outside the bound now
于 2013-03-19T17:21:09.037 回答
9

您可以在您喜欢的地方绘制,但在剪切矩形之外不会保存任何内容。

于 2010-10-26T22:46:55.627 回答
3

@numan 给出的答案几乎可以,问题是这种方法的内存分配,所以我们应该这样做,而不是:

// in constructor/elsewhere
Rect newRect = new Rect();

// in onDraw
canvas.getClipBounds(newRect);
newRect.inset(0, -20);  //make the rect larger
canvas.clipRect(newRect, Region.Op.REPLACE);

这解决了问题:-)

于 2016-10-16T17:04:43.063 回答
2

如果你想在 TextView 中画出越界的文本,你应该这样做:

<TextView
    ...
    android:shadowColor="#01000000"
    android:shadowDx="100" // out of right bound
    android:shadowDy="0"
    android:shadowRadius="1"
.../>

像@numan's answer那样使用clipRect()不起作用,因为TextView剪辑它在onDraw()中是自己的矩形:

if (mShadowRadius != 0) {
    clipLeft += Math.min(0, mShadowDx - mShadowRadius);
    clipRight += Math.max(0, mShadowDx + mShadowRadius);

    clipTop += Math.min(0, mShadowDy - mShadowRadius);
    clipBottom += Math.max(0, mShadowDy + mShadowRadius);
}

canvas.clipRect(clipLeft, clipTop, clipRight, clipBottom);

android:clipChildren="false"最后但并非最不重要的一点是,不要忘记android:clipToPadding="false"在您的父 ViewGroup中设置

于 2017-11-14T09:38:34.533 回答
0

如果您想要的只是在视图边界之外绘制(以编程方式),那么长话短说。

parentLayout.setClipChildren(false);

通过 xml

 android:clipChildren="false"

在此处输入图像描述

于 2021-12-31T14:06:39.237 回答