M-W*_*eEh 5 android hardware-acceleration android-custom-view
在硬件加速自定义中View添加ScrollView或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
}
Run Code Online (Sandbox Code Playgroud)
@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
}
Run Code Online (Sandbox Code Playgroud)
相同的结果:

但是对于后来的代码片段,只要你滚动ScrollView (我有隐形假人View,我可以滚动)和helloworld触摸ActionBar,就会发生一些非常有趣的事情,你会看到智能人类曾经在旧的Windows操作系统中看到的东西.


我知道drawTextOnPath()硬件加速模式不支持,但是如果你先调用它,为什么它可以工作?
drawTextOnPath()Android 4.1 之后由硬件加速支持这在这里正式提到: https: //code.google.com/p/android/issues/detail
?id=37925 但下一条评论似乎以某种方式表明了你的问题,所以可能是漏洞。
当然,对于 4.1 之前的版本,不要让它使用 HW accel - 通过调用在视图上设置软件层类型,View.setLayerType(View.LAYER_TYPE_SOFTWARE, null)并尝试在性能与错误之间进行权衡