我正在尝试为 android 开发一个简单的饼图类。目前,它可以获取标签和值的地图并绘制饼图。我还没有为饼图添加图例,这是我需要将文本放置在屏幕角落的小矩形附近的地方。任何帮助表示赞赏,因为我是 Android 开发新手。
问问题
80905 次
3 回答
54
您将不得不使用 Canvas 类的 drawText 方法。
Paint paint = new Paint();
canvas.drawPaint(paint);
paint.setColor(Color.BLACK);
paint.setTextSize(16);
canvas.drawText("My Text", x, y, paint);
这是有关它的相关文档:
于 2013-07-28T19:37:21.343 回答
15
这里曾经有另一个答案被删除,因为它只是一个链接。原始链接在这里。代码基本相同,但我去掉了非文本绘图部分并扩大了尺寸以更好地适应现代屏幕密度。
这仅显示了您可以使用文本绘图执行的一些操作。
这是更新的代码:
public class MainActivity extends AppCompatActivity {
DemoView demoview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
demoview = new DemoView(this);
setContentView(demoview);
}
private class DemoView extends View {
public DemoView(Context context){
super(context);
}
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// custom drawing code here
// remember: y increases from top to bottom
// x increases from left to right
int x = 0;
int y = 0;
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
canvas.save();
canvas.translate(100, 200);
// make the entire canvas white
canvas.drawColor(Color.WHITE);
// draw some text using STROKE style
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1);
paint.setColor(Color.MAGENTA);
paint.setTextSize(100);
canvas.drawText("Style.STROKE", 0, 0, paint);
canvas.translate(0, 200);
// draw some text using FILL style
paint.setStyle(Paint.Style.FILL);
//turn antialiasing on
paint.setAntiAlias(true);
//paint.setTextSize(30);
canvas.drawText("Style.FILL", 0, 0, paint);
canvas.translate(0, 200);
// draw some rotated text
// get text width and height
// set desired drawing location
x = 75;
y = 185;
paint.setColor(Color.GRAY);
//paint.setTextSize(25);
String str2rotate = "Rotated!";
// draw bounding rect before rotating text
Rect rect = new Rect();
paint.getTextBounds(str2rotate, 0, str2rotate.length(), rect);
canvas.translate(x, y);
paint.setStyle(Paint.Style.FILL);
// draw unrotated text
canvas.drawText("!Rotated", 0, 0, paint);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRect(rect, paint);
// undo the translate
canvas.translate(-x, -y);
// rotate the canvas on center of the text to draw
canvas.rotate(-45, x + rect.exactCenterX(),
y + rect.exactCenterY());
// draw the rotated text
paint.setStyle(Paint.Style.FILL);
canvas.drawText(str2rotate, x, y, paint);
//undo the translation and rotation
canvas.restore();
}
}
}
我稍后想尝试的其他方法是沿路径绘制文本。
另请参阅此处提供的更完整答案,该答案提供以下图像。
于 2017-01-26T10:14:17.023 回答
1
在画布上绘制文本的另一种(可以说是更好的)方法是使用StaticLayout
. 这会在需要时处理多行文本。
String text = "This is some text.";
TextPaint textPaint = new TextPaint();
textPaint.setAntiAlias(true);
textPaint.setTextSize(16 * getResources().getDisplayMetrics().density);
textPaint.setColor(0xFF000000);
int width = (int) textPaint.measureText(text);
StaticLayout staticLayout = new StaticLayout(text, textPaint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false);
staticLayout.draw(canvas);
为了说明起见,TextPaint
andStaticLayout
在此处使用之前已实例化。但是,这样做onDraw
会损害性能。这是一个更好的示例,在绘制自己的文本的自定义视图的上下文中显示它们。
于 2017-01-26T09:46:41.913 回答