使用硬件加速的`drawTextOnPath()`的奇怪行为

时间:2013-07-20 11:52:24

标签: android android-custom-view hardware-acceleration

ViewScrollView中添加的硬件加速自定义ListView,以下两个代码段都会产生相同的结果:(请忽略最佳做法一秒钟)

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas); 
    // centering stuff
    float centerX = getWidth() / 2f;
    float centerY = getHeight() / 2f;
    float size = 80;
    float halfSize = size / 2f;
    float left = centerX - halfSize;
    float top = centerY - halfSize;

    RectF oval = new RectF(left, top, left + size, top + size);

    Path path = new Path();
    path.addArc(oval, 160, 359);

    Paint paint = new Paint();
    paint.setTextSize(30);
    paint.setStyle(Style.STROKE);

    canvas.drawTextOnPath("Hello world", path, 0, 0, paint); //<--- line A
    canvas.drawCircle(centerX, centerY, 10, paint);          //<--- line B
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas); 
    // centering stuff
    float centerX = getWidth() / 2f;
    float centerY = getHeight() / 2f;
    float size = 80;
    float halfSize = size / 2f;
    float left = centerX - halfSize;
    float top = centerY - halfSize;

    RectF oval = new RectF(left, top, left + size, top + size);

    Path path = new Path();
    path.addArc(oval, 160, 359);

    Paint paint = new Paint();
    paint.setTextSize(30);
    paint.setStyle(Style.STROKE);

    canvas.drawCircle(centerX, centerY, 10, paint);          //<--- line B
    canvas.drawTextOnPath("Hello world", path, 0, 0, paint); //<--- line A
}

相同结果:

enter image description here


但是在稍后的代码段中,只要您滚动ScrollView (我在下方有隐形虚拟View,我就可以滚动)和helloworld触及{{1} },发生了非常有趣的事情,你会看到intelligent humankind used to see in old Windows OS

enter image description here


enter image description here

我知道硬件加速模式不支持ActionBar,但如果先调用它,为什么它可以工作?

1 个答案:

答案 0 :(得分:2)

Android 4.1之后的硬件加速支持

drawTextOnPath() 这在这里正式提到:https://code.google.com/p/android/issues/detail?id=37925 但下一个评论似乎表明你的问题可能是一个错误。

当然对于4.1之前,不要让它使用HW accel - 通过调用View.setLayerType(View.LAYER_TYPE_SOFTWARE, null)在View上设置软件层类型并尝试权衡性能与错误