【发布时间】:2011-05-01 16:22:24
【问题描述】:
我正在尝试为 android 开发一个简单的饼图类。目前,它可以获取标签和值的地图并绘制饼图。我还没有为饼图添加图例,这是我需要将文本放置在屏幕角落的小矩形附近的地方。任何帮助表示赞赏,因为我是 Android 开发新手。
【问题讨论】:
我正在尝试为 android 开发一个简单的饼图类。目前,它可以获取标签和值的地图并绘制饼图。我还没有为饼图添加图例,这是我需要将文本放置在屏幕角落的小矩形附近的地方。任何帮助表示赞赏,因为我是 Android 开发新手。
【问题讨论】:
在画布上绘制文本的另一种(可以说是更好的)方法是使用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 和 StaticLayout 在此处使用之前已被实例化。不过,在onDraw 中这样做会损害性能。 Here is a better example 在绘制自己的文本的自定义视图的上下文中显示它们。
【讨论】:
这里曾经有另一个答案被删除,因为它只是一个链接。原始链接是here。代码基本相同,但我去掉了非文本绘图部分,还放大了尺寸以更好地适应现代屏幕密度。
这只是展示了你可以用文本绘图做的一些事情。
这是更新后的代码:
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();
}
}
}
稍后我想尝试的其他内容是drawing text along a path。
另请参阅this fuller answer here,它提供了以下图像。
【讨论】:
您必须使用 Canvas 类的 drawText 方法。
Paint paint = new Paint();
canvas.drawPaint(paint);
paint.setColor(Color.BLACK);
paint.setTextSize(16);
canvas.drawText("My Text", x, y, paint);
这是有关它的相关文档:
【讨论】:
canvas.drawPaint(paint); 在这里似乎是多余的。您可能还需要抗锯齿 (paint.setAntiAlias(true);) 和绘画风格 (paint.setStyle(Paint.Style.FILL);)