我想對齊位圖水平和垂直文本,我讀了幾個帖子,但我找不到解決方案。位圖是一個簡單的圓形圖像。我發佈我的當前代碼。或多或少,它的作品,但文本不是完全居中,它似乎有點在左邊,有點在頂部,我的意思是我似乎必須添加一個偏移量將其移動到右側和底部。垂直和水平與畫布中心文本
public static float convertDpToPixel(float dp, Context context) {
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi/160f);
return px;
}
v = (ImageView) findViewById(R.id.imageView1);
Bitmap b = BitmapFactory.decodeResource(getResources(),
R.drawable.marker).copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(b);
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setARGB(255, 0, 0, 0);
textPaint.setTextAlign(Align.CENTER);
textPaint.setTypeface(Typeface.DEFAULT_BOLD);
textPaint.setTextSize(convertDpToPixel(9, this));
String text = "30";
int xPos = (canvas.getWidth()/2);
int yPos = (int) ((canvas.getHeight()/2) - ((textPaint.descent() +
textPaint.ascent())/2)) ;
canvas.drawText(text, xPos, yPos, textPaint);
v.setImageBitmap(b);
好一點,現在的代碼爲:int XPOS =(canvas.getWidth()/ 2) - ((r.width())/ 2); int yPos =(canvas.getHeight()/ 2)+((r.height())/ 2);但x不再對齊,現在我已經將文字移動到了左側。 – greywolf82
查看編輯答案 – Merlevede
好的,現在可以使用了,謝謝!!!!!!! – greywolf82