我必須在地圖視圖上放置標記並在標記上寫入數字。我已經這樣做了,但文本對齊方式因不同的分辨率而異。波紋管是參考代碼繪製圖像的文本中間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