【问题标题】:Canvas.drawCircle() with large circles under hardware acceleration硬件加速下画大圆的canvas.drawCircle()
【发布时间】: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 有问题——实际上是 比它在硬件下实际工作的几次快很多 加速。

我的问题是:

  1. 我是在滥用 Canvas,还是这是一个错误,或两者兼而有之? Android分配大吗 引擎盖下的位图来绘制这些圆圈?这似乎不应该是 这对 OpenGL 来说具有挑战性,但我是硬件加速应用开发的新手。

  2. 有什么好的替代方法来绘制有部分的未填充的大圆圈 延伸出画布的剪辑区域?失去硬件加速 不是一个选项。

提前谢谢...

【问题讨论】:

    标签: android


    【解决方案1】:

    我从其他人那里了解到,我在这里描述的问题是由 Android 3.2 中的错误造成的。现在的解决方法当然是使用软件层而不是硬件加速。显然这个问题在 Android 4.0 (Ice Cream Sandwich) 中得到了修复。

    【讨论】:

    • 很遗憾没有;我在 Google android-developers group 上问了同样的问题,Google Android 团队的 Romain Guy 解释说这是 4.0 中修复的 3.2 错误;解决方法当然是使用软件渲染而不是硬件加速。
    猜你喜欢
    • 1970-01-01
    • 2012-03-24
    • 2011-09-20
    • 1970-01-01
    • 2023-03-31
    • 2015-03-16
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    相关资源
    最近更新 更多