我必须在地图视图上放置标记并在标记上写入数字。我已经这样做了,但文本对齐方式因不同的分辨率而异。波纹管是参考代码绘制图像的文本中间android
float xVal = (float) curScreenCoords.x; // Point curScreenCoords
float yVal = (float) curScreenCoords.y-20; // Point curScreenCoords
Bitmap bitmap = BitmapFactory.decodeResource (context.getResources() , (R.drawable.pin_number)) ;
canvas.drawBitmap(bitmap, xVal, yVal, getInnerPaint());
public Paint getInnerPaint() {
if (innerPaint == null) {
innerPaint = new Paint();
}
innerPaint.setARGB(255, 117, 161, 220); // blue
innerPaint.setAntiAlias(true);
innerPaint.setStyle(Style.FILL);
return innerPaint;
}
canvas.drawText(String.valueOf(10), xVal+20, yVal+22, getCountPaint()); // 10 is just for example, it can vary to one digit to two to three
public Paint getCountPaint() {
if (innerPaint == null) {
innerPaint = new Paint();
}
innerPaint.setARGB(255, 255, 255, 255);
innerPaint.setAntiAlias(true);
innerPaint.setStyle(Style.FILL);
innerPaint.setTextSize(12f);
innerPaint.setTextAlign(Align.CENTER);
return innerPaint;
}
一切工作正常,除了文本对齐,此代码适用于480 * 800分辨率。文字在画布中完美居中对齐。 x,y位置在图像上是完美的,但在320 * 480上看起来并不完美。文本的x和y位置在此分辨率上看起来不同。任何人都可以请建议我究竟发生了什么错误?在不同大小的设备上做同样的事情有没有什么基础?提前致谢。
或者,如果垂直对齐是不是真的很重要,你可以直接调用'paint.setTextAlign(Paint.Align.CENTER)'[Paint.html#setTextAlign(HTTP ://developer.android.com/reference/android/graphics/Paint.html#setTextAlign(android.graphics.Paint.Align)) – jlhuertas