安卓:RoundRectShape:修改圆角半径半径、圆角、RoundRectShape

2023-09-04 05:04:09 作者:当我没来过

我有一个 RoundRectShape 被绘制在查看的OnDraw() 功能。

I have a RoundRectShape that is drawn in a View's onDraw() function.

这种形状的角是由这一观点任意拖拽操作通过重写的onTouchEvent 函数修改(它调用无效()强制调用的OnDraw())。

The corners of this shape are modified by any drag actions on this view through an overridden onTouchEvent function (which calls invalidate() to force the call to onDraw()).

的OnDraw 初始化变量,Eclipse将显示一个警告说避免在抽签/布局操作的对象分配(preallocate和重用代替)。

When initialising variables in onDraw, Eclipse shows a warning saying Avoid object allocations during draw/layout operations (preallocate and reuse instead).

这个问题解释如下:

您应该避免图纸或布局操作过程中分配对象。这些被频繁调用,所以平滑的用户界面可以通过所造成的对象分配垃圾收集暂停中断。

这是一般处理的方法是将分配对象前面和重用他们为每个绘图操作

要做到这一点,我显然必须能够修改圆角半径在构造函数中设置。该 RoundedRectShape类没有提供二传(和我是pretty的确保相关成员变量都不可访问)。

To do this I obviously have to be able to modify the corner radii as set in the constructor. The RoundedRectShape class provides no setter (and I am pretty sure the related member variables are inaccessible).

我知道这是一个小物件和一个小问题,但我怎么能更改这些边角没有重新初始化的对象时,类提供了设置的圆角半径的唯一方法是通过构造函数?

I know it is a small object and a minor issue, but how can I change these corners without reinitializing the Object when the only method the class provides for setting the radii of the rounded corners is through the constructor?

或者这只是在这里做的最好的事情将不得不被替换做什么是可能的一个情况?

Or is this just a case where doing the best thing will have to be replaced with doing what is possible?

谢谢

P.S。要求在的onTouchEvent()方法构造将删除警告,但完成绝对没有因为这一事实,即的OnDraw()被称为每次通过无效()反正

P.S. Calling the constructor in the onTouchEvent() method will remove the warning but accomplishes absolutely nothing due to the fact that onDraw() is called every time through invalidate() anyway

推荐答案

我觉得你并不需要使用 RoundRectShape 可言。 RoundRectShape 是不可变的如此改变其值的唯一方法是使用反射。 你可以通过调用 Canvas.drawRoundRect(RectF矩形,浮RX,浮RY,油漆涂料)法的情况下直接使用 RoundRectShape 。或者,你可以看看 RoundRectShape 实施,只是利用其code在的OnDraw()方法。

I think you don't need to use RoundRectShape at all. RoundRectShape is immutable so the only way to change its values is to use reflection. You can easily accomplish the same drawnings by calling Canvas.drawRoundRect(RectF rect, float rx, float ry, Paint paint) method directly without using RoundRectShape. Or you can look at RoundRectShape implementation and just use its code in your onDraw() method.

编辑: 关于 RoundRectShape 不是正确的方式去寻找实施的意见是正确的。继实施中调用:

The comments about RoundRectShape not being the right way to go and looking at the implementation were right. Following the implementation found a call to:

mPath.addRoundedRect() 

其中有一个变化,允许圆角半径的持股量输入( Path.addRoundRect )

在回答这个问题:

使用,而不是一个形状可变的路径,并绘制新的圆角矩形的路径时,需要