gpt4 book ai didi

java - Android drawTextOnPath() 不显示输出

转载 作者:行者123 更新时间:2023-11-30 09:17:32 26 4
gpt4 key购买 nike

我在下面有以下代码。

public class CompassActivity extends Activity {  

public class OuterCircle extends View {
Paint paint = new Paint();
Path path = new Path();
private static final String s = "Hello world example";

public OuterCircle(Context context) {
super(context);
init();
}

private void init() {
paint.setColor(Color.WHITE);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(2);
paint.setAntiAlias(true);
}

private void drawDegreesOnCircle(Canvas c) {
path.addCircle(getWidth()/2, getHeight()/2, 180, Direction.CW);
c.drawTextOnPath(s, path, 0, 10, paint);
}

public void onDraw(Canvas c) {
int cx = getWidth()/2;
int cy = getHeight()/2;
c.drawCircle(cx, cy, 170, paint);
drawDegreesOnCircle(c);
}
}
}

圆绘制成功。但是,我指定的字符串没有显示。代码中没有错误或警告。我的代码中遗漏了什么吗?我正在尝试显示圆圈周围的字符串。我被困在这里了。 :D

最佳答案

我已经通过添加

解决了上述问题
setLayerType(View.LAYER_TYPE_SOFTWARE, null)

需要配合上述方法使用

Canvas.drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint)

适用于 Android API 级别 11 或更高级别。该字符串现在已成功显示在圆圈周围。这是正确的代码。

public class CompassActivity extends Activity {  

public class OuterCircle extends View {
Paint paint = new Paint();
Path path = new Path();
private static final String s = "Hello world example";

public OuterCircle(Context context) {
super(context);
init();
}

private void init() {
paint.setColor(Color.WHITE);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(2);
paint.setAntiAlias(true);
}

private void drawDegreesOnCircle(Canvas c) {
path.addCircle(getWidth()/2, getHeight()/2, 180, Direction.CW);
c.drawTextOnPath(s, path, 0, 10, paint);
setLayerType(View.LAYER_TYPE_SOFTWARE, null); // Required for API level 11 or higher.
}

public void onDraw(Canvas c) {
int cx = getWidth()/2;
int cy = getHeight()/2;
c.drawCircle(cx, cy, 170, paint);
drawDegreesOnCircle(c);
}
}
}

关于java - Android drawTextOnPath() 不显示输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18916930/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com