我在下面提供了以下代码。
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
答案 0 :(得分:2)
我通过添加
解决了上述问题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);
}
}
}
答案 1 :(得分:0)
您错过了对drawPath()
private void drawDegreesOnCircle(Canvas c) {
path.addCircle(getWidth()/2, getHeight()/2, 180, Direction.CW);
c.drawPath(path, paint);
c.drawTextOnPath(s, path, 0, 10, paint);
}
答案 2 :(得分:0)
一个非常简单的例子,只是为了在显示屏的中心以一个角度获取文字。
public class DrawSomeText extends View {
Paint mPaint;
public DrawSomeText(Context context) {
super(context);
mPaint = new Paint();
mPaint.setColor(Color.BLACK);
}
@Override
public void onDraw(Canvas canvas) {
Path path = new Path();
path.moveTo(getWidth()/2, getHeight()/2);
path.lineTo(getWidth(), getHeight());
path.close();
canvas.drawPath(path, mPaint);
canvas.drawTextOnPath("Hello World", path, 0, 0, mPaint);
}
}