我是Android的新手,我编写了以下示例代码。
public class Circle extends View {
Paint paint = new Paint();
Path path = new Path();
private static final String s = "Hello world example";
public Circle(Context context) {
super(context);
paint.setColor(Color.BLACK);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(2);
paint.setAntiAlias(true);
paint.setTextSize(30);
}
public void onDraw(Canvas c) {
path.addCircle(getWidth()/2, getHeight()/2, 180, Direction.CW);
c.drawTextOnPath(s, path, 0, 10, paint);
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
}
Canvas.drawTextOnPath()
显示从右侧开始的文本(我的意思是正X轴)。我想以时钟方式显示顶部的文本。我想改变文本的起始位置。我对Android Canvas.translate()
和Canvas.scale()
感到困惑。或者我应该使用Canvas.rotate()
?我已粘贴下面的输出,以便清楚地理解我的问题。
我想以这种形式显示输出。
答案 0 :(得分:2)
可以使用arc代替圆圈:
Path path = new Path();
RectF rect = new RectF(width/2 - radius, height/2 - radius, width/2 + radius, height/2 + radius);
path.addArc(rect 270, 270);
canvas.drawTextOnPath(msg., path, 0, 0, paint);
可以使用Matrix翻译,旋转和缩放路径。 例如,上面的代码将从第一象限开始绘制文本,即从(y,0)开始。要从(-y,0)顺时针方向开始绘图,
Path path = new Path();
RectF rect = new RectF(width/2 - radius, height/2 - radius, width/2 + radius, height/2 + radius);
Matrix matrix = new Matrix();
matrix.setScale(-1, -1, width/2, height/2);
path.addArc(rect, 270, 270);
path.transform(matrix);
canvas.drawTextOnPath(msg, path, 0, 0, paint);
答案 1 :(得分:1)
在应用和Canvas
方法之前,我在代码中添加了Canvas.rotate(float degrees, float px, float py)
,最终修复了我的Canvas
问题。以下是代码。
public class Circle extends View {
Paint paint = new Paint();
Path path = new Path();
private static final String s = "Hello world example";
public Circle(Context context) {
super(context);
paint.setColor(Color.BLACK);
paint.setStyle(Style.STROKE);
paint.setStrokeWidth(2);
paint.setAntiAlias(true);
paint.setTextSize(30);
}
public void onDraw(Canvas c) {
c.rotate(-90, getWidth()/2, getHeight()/2);
path.addCircle(getWidth()/2, getHeight()/2, 180, Direction.CW);
c.drawTextOnPath(s, path, 0, 10, paint);
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
}
之前我尝试Canvas.rotate(float degrees)
但没有奏效。但是,Canvas.rotate(float degrees, float px, float py)
工作了!