ImageView に 9 パッチ イメージがあり、イメージのコンテンツ領域にテキストを描画したいと考えています。Android は画像を引き伸ばしますが、別の画面では dp や px などの単位を使用できません。不正確すぎる。
この使命を達成する方法は?=)
カスタムImageView
を記述し、onDraw(Canvas canvas)
メソッドで次の方法でテキストを描画できます。
Paint paint = new Paint()
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1);
paint.setColor(Color.BLACK);
paint.setTextSize(11);
canvas.drawText("your text", x, y, paint);
ImageView
たとえば、テキストを中央に配置できます。
int imgWidth = getMeasuredWidth();
int imgHeight = getMeasuredHeight();
float txtWidth = paint.measureText("your text");
int x = imgWidth/2 - txtWidth/2;
int y = imgHeight/2 - 6; // 6 is half of the text size