0
上multipeサイズの複数の線を引く私はすでにキャンバスキャンバス
上に複数の線を描画する方法を知っている。しかし、現在、私はできかつて彼らのために
を一つだけ文字サイズを設定するためにそして今、私は、複数のラインを描きたいです
テキスト1:詳細にキャンバス上に複数のサイズを有するサイズ16
テキスト2:サイズ32
テキスト3:サイズ14
私は、実際に知っている
人々をどのように行うのか分からない、私の詳細を助けてください、
は、
ありがとうp/s:キャンバスに複数行描画するサンプルコード:
私は間違って計算したので、テキストサイズと位置に関する複数の行が間違って表示されています。私はまだチェックしています。
private TextPaint mTp;
private String[] strings = {
" Topics : Asukabu Tawain \n ",
"512 \n ",
"Comments \n",
"7:30 22/12/2017"
};
private float height = 0;
public CustomImageView(Context context) {
super(context);
// Initialize a new Bitmap object
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inMutable = true;
Bitmap mutableBitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.red_circle, opt);
mTp = new TextPaint(Paint.ANTI_ALIAS_FLAG);
// text color - #3D3D3D
mTp.setColor(Color.WHITE);
// text shadow
mTp.setShadowLayer(1f, 0f, 1f, Color.WHITE);
textWidth = mutableBitmap.getWidth();
setImageBitmap(mutableBitmap);
}
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
x = (canvas.getWidth() - textWidth)/2;
for (int i = 0; i < strings.length; i++) {
if (canvas.getWidth() < 150)
mTp.setTextSize(0);
else
mTp.setTextSize(determineMaxTextSize(strings[i], height/2));
StaticLayout mSl = new StaticLayout(
strings[i], mTp, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
// get height of multiline text
int textHeight = mSl.getHeight();
// get position of text's top left corner
y = (canvas.getHeight() - textHeight)/2;
// draw text to the Canvas center
canvas.save();
canvas.translate(x, y);
mSl.draw(canvas);
canvas.restore();
}
}
public void setHeight(float height) {
this.height = height;
}
private int determineMaxTextSize(String str, float maxWidth){
int size = 0;
Paint paint = new Paint();
do {
paint.setTextSize(++ size);
} while(paint.measureText(str) < maxWidth);
return size;
}
あなたのアイデアは素晴らしいです。しかし、私は自動的に 'x''y'を計算する必要があります、テキスト値のためのセンター重力も設定する必要があります。私が質問で更新したイメージのように、見てください。 –