2016-12-22 8 views
0

上multipeサイズの複数の線を引く私はすでにキャンバスキャンバス

上に複数の線を描画する方法を知っている。しかし、現在、私はできかつて彼らのために

を一つだけ文字サイズを設定するためにそして今、私は、複数のラインを描きたいです

  • テキスト1:詳細にキャンバス上に複数のサイズを有するサイズ16

  • テキスト2:サイズ32

  • テキスト3:サイズ14

私は、実際に知っている

人々をどのように行うのか分からない、私の詳細を助けてください、

は、

ありがとう

p/s:キャンバスに複数行描画するサンプルコード:

enter image description here

私は間違って計算したので、テキストサイズと位置に関する複数の行が間違って表示されています。私はまだチェックしています。

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; 
} 

答えて

0

ただ、テキストの3種類のサイズを描画するために3異なるTextPaintを使用しています。

protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 

    int width = canvas.getWidth(); 
    int height = width; 
    paint.setColor(Color.RED); 
    paint.setAntiAlias(true); 
    canvas.drawCircle(width/2,height/2,width/2 - 10,paint); 

    textPaint1.setColor(Color.WHITE); 
    textPaint1.setTextSize(36); 
    canvas.drawText("You received",width/2 - 108,height/2 - 80,textPaint1); 

    textPaint2.setTextSize(72); 
    textPaint2.setColor(Color.WHITE); 
    canvas.drawText("135",width/2 - 72,height/2,textPaint2); 

    textPaint3.setTextSize(32); 
    textPaint3.setColor(Color.WHITE); 
    canvas.drawText("comments",width/2 - 96,height/2 + 40,textPaint3); 
} 

、別の アドバイスはそれが毎回あなたのUIのアップデートと簡単にGCをトリガし、パフォーマンスが悪いの原因となりますので、多くのオブジェクトを作成する呼び出されますので、onDraw方法ではない、新しいオブジェクトを行います。

+0

あなたのアイデアは素晴らしいです。しかし、私は自動的に 'x''y'を計算する必要があります、テキスト値のためのセンター重力も設定する必要があります。私が質問で更新したイメージのように、見てください。 –