我知道在onDraw
方法中创建对象非常昂贵。我想画一个圆角矩形矩阵,坐标是动态的,我不能缓存所有的矩形,因为我使用滚动视图,可能有很多矩形,没有其他超载for drawRoundRect
方法,它有原始参数,我强制在每次迭代中创建一个Rectangle对象。谁可以为此提出有效的解决方案?
@Override
protected void onDraw(Canvas canvas) {
int h = getMeasuredHeight();
int tileSize = h / rows;
for(int i = 0; i < rows; ++i) {
for(int j = 0; j < columns; ++j) {
int x = j * tileSize;
int y = i * tileSize;
canvas.drawRoundRect(new RectF(x, y, x + tileSize, y + tileSize), 10, 10, tilePaint);
}
}
}
这只是一个例子,矩形可以有任意坐标。
答案 0 :(得分:4)
RectF
具有set(left, top, right, bottom
)方法。您可以在构造函数上分配它,并使用此方法更改Rectf的边界。
mRect.set(x, y, x + tileSize, y + tileSize);
其中mRect为RectF mRect = new RectF();