1

ImageView に 9 パッチ イメージがあり、イメージのコンテンツ領域にテキストを描画したいと考えています。Android は画像を引き伸ばしますが、別の画面では dp や px などの単位を使用できません。不正確すぎる。

この使命を達成する方法は?=)

4

1 に答える 1

3

カスタム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
于 2012-07-25T08:22:02.670 に答える