5

硬件加速自定义View中添加ScrollViewListView以下两个代码片段产生相同的结果:(请忽略最佳实践一秒钟)

@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
}

相同的结果:

在此处输入图像描述


但是随着后面的代码片段,一旦你滚动ScrollView (我在下面有看不见的虚拟对象View,所以我可以滚动)和 helloworld touches ActionBar,就会发生一些非常有趣的事情,你会看到智能人类过去在旧 Windows OS 中看到的东西。

在此处输入图像描述


在此处输入图像描述

我知道drawTextOnPath()硬件加速模式不支持,但是如果你先调用它,为什么它会起作用?

4

1 回答 1

2

drawTextOnPath()在 Android 4.1 之后受硬件加速支持 这在这里正式提到:https ://code.google.com/p/android/issues/detail?id=37925 但下一条评论似乎以某种方式表明了您的问题,所以也许是漏洞。

当然,对于 4.1 之前的版本,不要让它使用硬件加速 - 通过调用在视图上设置软件层类型,View.setLayerType(View.LAYER_TYPE_SOFTWARE, null)并尝试在性能与错误之间进行权衡

于 2013-07-30T18:34:24.803 回答