get Drawn Text Bitmap - Android Graphics

Android examples for Graphics:Bitmap String

Description

get Drawn Text Bitmap

Demo Code


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader.TileMode;
import android.graphics.YuvImage;
import android.graphics.drawable.Drawable;

public class Main{
    /*from   ww w  . j  av  a 2s .com*/

    private static final String TAG = ImgUtil.class.getSimpleName();
    
    public static Bitmap getDrawnTextBitmap(Bitmap src, String text, int x,
            int y, int color, boolean recycleSrc) {
        Bitmap drawnText = drawTextBitmap(src.copy(Config.ARGB_8888, true),
                text, x, y, color);
        if (recycleSrc && drawnText != src) {
            src.recycle();
        }
        return drawnText;
    }
    
    public static Bitmap drawTextBitmap(Bitmap src, String text, int x,
            int y, int color) {
        if (!src.isMutable()) {
            LogUtil.w(TAG, "drawTextBitmap", "source bitmap is not mutable");
            return src;
        }
        Canvas canvas = new Canvas(src);
        Paint paint = new Paint();
        paint.setColor(color);
        canvas.drawText(text, x, y, paint);
        canvas.save(Canvas.ALL_SAVE_FLAG);
        canvas.restore();
        return src;
    }
}

Related Tutorials