1

だから私は最初に以下のコードから始めました:

private Bitmap writeTextOnDrawable(int drawableId, String text) {

    Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId);
    Bitmap alteredBitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());
    Canvas canvas = new Canvas(alteredBitmap);
    Paint paint = new Paint();
    canvas.drawBitmap(bm, 0, 0, paint);
    paint.setColor(Color.WHITE); 
    paint.setTextSize(150f); 
    canvas.drawText(text, 100, 1000, paint); 

    return alteredBitmap;
}

背景画像がそこにあり、テキストが画面に対して長すぎることを除いてテキストも意図したとおりに機能し、何らかの方法でラップする必要がありました。

次に、このコードで複数行の問題を調べTextPaintて処理しました。StaticLayout

private Bitmap writeTextOnDrawable(int drawableId, String text) {

    Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId);
    Bitmap alteredBitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), bm.getConfig());
    Canvas canvas = new Canvas(alteredBitmap);
    TextPaint tp = new TextPaint();
    canvas.save();
    tp.setColor(Color.WHITE);
    tp.setTextSize(150f);
    tp.setTextAlign(Align.CENTER);
    tp.setAntiAlias(true);
    StaticLayout sl = new StaticLayout("" + text, tp,
            canvas.getWidth(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    canvas.translate(100, 1000);
    sl.draw(canvas);

    return alteredBitmap;
}

これはまったく機能しませんでした。背景画像がなくなり、何も表示されなくなりました。以前はテキストが中央に配置されていませんでした。唯一の利点は、テキストが複数行になったことです。

テキストが最初の開始点を変更した理由と、キャンバスの背景画像が消えた理由を知っている人はいますか? どんな助けでも大歓迎です。

4

1 に答える 1