Convert String text to Bitmap

前端 未结 5 1318
醉酒成梦
醉酒成梦 2020-12-02 13:29

Is it possible to convert string text that is inside an EditText box into a Bitmap? In other words, is there any way to convert string text into a Bitmap that m

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 14:30

    try this :

        public static Bitmap drawText(String text, int textWidth, int textSize) {
    // Get text dimensions
    TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG
    | Paint.LINEAR_TEXT_FLAG);
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setColor(Color.BLACK);
    textPaint.setTextSize(textSize);
    StaticLayout mTextLayout = new StaticLayout(text, textPaint,
    textWidth, Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    
    // Create bitmap and canvas to draw to
    Bitmap b = Bitmap.createBitmap(textWidth, mTextLayout.getHeight(), Config.RGB_565);
    Canvas c = new Canvas(b);
    
    // Draw background
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG
    | Paint.LINEAR_TEXT_FLAG);
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(Color.WHITE);
    c.drawPaint(paint);
    
    // Draw text
    c.save();
    c.translate(0, 0);
    mTextLayout.draw(c);
    c.restore();
    
    return b;
    }
    

提交回复
热议问题