Canvas.drawTextOnPath(...)无法在Lollipop上运行

时间:2014-11-04 15:14:16

标签: android android-canvas android-5.0-lollipop

canvas.DrawTextOnPath似乎不适用于Lollipop设备。看到这里的差异。 (The Nexus 10 图像是正确的,但棒棒糖无法正确显示)

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

有什么东西"不同"我需要在这台设备上做什么?

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:0)

是的,从Lollipop打破了。在4.4.4中完美地工作。

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

我将文本大小设置为5.f(如果它更小),缩小画布,并适当缩放基线路径。慢但它有效,不能等到我能去掉这个可怕的污泥。