Canvas.drawTextOnPath(...) 在 Lollipop 上无法工作

8

canvas.DrawTextOnPath在Lollipop设备上似乎无法正常工作。看看这里的区别。(Nexus 10图像是正确的,但Lollipop无法正确显示)

Image

代码是简单的路径绘制。

// Path for the inner circle
unitPath = new Path();
unitPath.addArc(unitRect, 180.0f, 180.0f);

// Draw the text and the path
canvas.drawTextOnPath("Inner Circle", unitPath, 0.0f, 0.0f, unitPaint);
canvas.drawPath(unitPath,unitPaint);

这里有一个Android Studio测试项目,用于展示此问题。任何想查看的人都可以在这里看到。 https://dl.dropboxusercontent.com/u/6768304/WebLinks/TestApp.rar 我需要在这个设备上做些什么“不同”的事情吗?

你确定路径是正确的吗?路径的来源是什么? - Philipp Jahoda
这是在两台设备上运行的相同应用程序。您可以在图片中看到路径。上面的最后两行代码是内部圆形文本和填充的半圆。 - Kuffs
2个回答

3

好的,看起来DrawTextOnPath在字体大小低于1.0f时有些问题。

解决方法是将所有内容放大,绘制文本,然后再缩小回去。

演示项目中的drawTitle方法将从以下内容更改:

private void drawTitle(Canvas canvas) {
    canvas.drawTextOnPath(upperTitle, upperTitlePath, 0.0f, 0.02f, unitPaint);
    canvas.drawTextOnPath(lowerTitle, lowerTitlePath, 0.0f, 0.0f, unitPaint);
    canvas.drawTextOnPath(unitTitle, unitPath, 0.0f, 0.0f, unitPaint);
    canvas.drawPath(unitPath,unitPaint);
}

转换为:

private void drawTitle(Canvas canvas) {
    //Save original font size
    float originalTextSize = unitPaint.getTextSize();

    // set a magnification factor
    final float magnifier = 100f;

    // Scale the canvas
    canvas.save();
    canvas.scale(1f / magnifier, 1f / magnifier);

    // create new rects and paths based on the new scale
    unitRect = new RectF();
    unitRect.set((faceRect.left + unitPosition) * magnifier, (faceRect.top + unitPosition) * magnifier, (faceRect.right - unitPosition) * magnifier, (faceRect.bottom - unitPosition) * magnifier);
    unitPath = new Path();
    unitPath.addArc(unitRect, 180.0f, 180.0f);

    titleRect = new RectF();
    titleRect.set((faceRect.left + titlePosition) * magnifier, (faceRect.top + titlePosition) * magnifier, (faceRect.right - titlePosition) * magnifier, (faceRect.bottom - titlePosition) * magnifier);
    upperTitlePath = new Path();
    upperTitlePath.addArc(titleRect, 180.0f, 180.0f);

    titleRect = new RectF();
    titleRect.set((faceRect.left + titlePosition) * magnifier, (faceRect.top + titlePosition) * magnifier, (faceRect.right - titlePosition) * magnifier, (faceRect.bottom - titlePosition) * magnifier);
    lowerTitlePath = new Path();
    lowerTitlePath.addArc(titleRect, -180.0f, -180.0f);

    // increase the font size
    unitPaint.setTextSize(originalTextSize * magnifier);

    // do the drawing of the text
    canvas.drawTextOnPath(unitTitle, unitPath, 0.0f, 0.0f, unitPaint);
    canvas.drawTextOnPath(upperTitle, upperTitlePath, 0.0f, 0.02f, unitPaint);
    canvas.drawTextOnPath(lowerTitle, lowerTitlePath, 0.0f, 0.0f, unitPaint);

    // bring everything back to normal
    canvas.restore();
    unitPaint.setTextSize(originalTextSize);

    canvas.drawPath(unitPath, unitPaint);
}

0

是的,从Lollipop开始就出现了问题。在4.4.4中完美运行。

https://code.google.com/p/android/issues/detail?id=40965

我正在将文本大小设置为5.f,如果它更小,则缩小画布,并适当地放大基线路径。速度较慢,但它可以工作,我迫不及待地想要删除这个可怕的补丁。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接