【发布时间】:2012-01-18 22:31:17
【问题描述】:
在开发 Android 应用的过程中,我发现需要绘制 几个以任意点为中心的未填充同心圆,足以 其中一些仅在显示屏上部分可见。然而,这并不 似乎与硬件加速一起工作。我的测试台是三星 Galaxy 运行 Android 3.2 的 Tab 10.1。
以下代码来自我编写的 View 的一个测试子类,用于隔离 问题:
private Paint paint = new Paint();
private int count = 0;
private static final int[] COLORS = { 0xffff0000, 0xff00ff00, 0xff0000ff, 0xffff00ff };
public TestCircles(Context context) {
super(context);
paint.setStrokeWidth(1.0f);
paint.setStyle(Paint.Style.STROKE);
}
public TestCircles(Context context, AttributeSet attributes) {
super(context, attributes);
paint.setStrokeWidth(1.0f);
paint.setStyle(Paint.Style.STROKE);
}
public boolean onTouchEvent(MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN)
invalidate();
return true;
}
protected void onDraw(Canvas canvas) {
// Pick the color to use, cycling through the colors list repeatedly, so that we can
// see the different redraws.
paint.setColor(COLORS[count++]);
count %= COLORS.length;
// Set up the parameters for the circles; they will be centered at the center of the
// canvas and have a maximum radius equal to the distance between a canvas corner
// point and its center.
final float x = canvas.getWidth() / 2f;
final float y = canvas.getHeight() / 2f;
final float maxRadius = (float) Math.sqrt((x * x) + (y * y));
// Paint the rings until the rings are too large to see.
for (float radius = 20; radius < maxRadius;
radius += 20)
canvas.drawCircle(x, y, radius, paint);
}
我将 TestCircles 作为 Activity 中的唯一视图运行,将其布置为填充 可用的宽度和高度(即它几乎是全屏的)。我可以点击 仅在重绘之前显示(触发重绘)几次 发生(即圆圈的颜色不会改变)。实际上, onDraw() 代码是 仍在运行以响应每次点击 - 正如诊断消息所证明的那样 - 但屏幕上没有任何变化。
当 onDraw() 首次开始重绘失败时,调试日志包括 以下条目,每次调用 onDraw() 一次:
E/OpenGLRenderer(21867): OpenGLRenderer 内存不足!
如果我在清单中关闭硬件加速,这些问题就会消失—— 这并不奇怪,因为显然 OpenGL 有问题——实际上是 比它在硬件下实际工作的几次快很多 加速。
我的问题是:
我是在滥用 Canvas,还是这是一个错误,或两者兼而有之? Android分配大吗 引擎盖下的位图来绘制这些圆圈?这似乎不应该是 这对 OpenGL 来说具有挑战性,但我是硬件加速应用开发的新手。
有什么好的替代方法来绘制有部分的未填充的大圆圈 延伸出画布的剪辑区域?失去硬件加速 不是一个选项。
提前谢谢...
【问题讨论】:
标签: android