get Overlaid Bitmap - Android Graphics

Android examples for Graphics:Bitmap Effect

Description

get Overlaid 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{
    /*  w ww  .  java 2s .c  o  m*/

    private static final String TAG = ImgUtil.class.getSimpleName();
    
    public static Bitmap getOverlaidBitmap(Bitmap src, Bitmap overlay,
            boolean recycleSrc) {
        Bitmap overlaid = overlayBitmap(src.copy(Config.ARGB_8888, true),
                overlay);
        if (recycleSrc && overlaid != src) {
            src.recycle();
        }
        return overlaid;
    }
    
    public static Bitmap getOverlaidBitmap(Bitmap src, Bitmap overlay,
            int x, int y, boolean recycleSrc) {
        Bitmap overlaid = overlayBitmap(src.copy(Config.ARGB_8888, true),
                overlay, x, y);
        if (recycleSrc && overlaid != src) {
            src.recycle();
        }
        return overlaid;
    }
    
    public static Bitmap overlayBitmap(Bitmap src, Bitmap overlay) {
        if (!src.isMutable()) {
            LogUtil.w(TAG, "overlayBitmap", "source bitmap is not mutable");
            return src;
        }
        Canvas canvas = new Canvas(src);
        canvas.drawBitmap(overlay,
                (src.getWidth() - overlay.getWidth()) / 2,
                (src.getHeight() - overlay.getHeight()) / 2, null);
        canvas.save(Canvas.ALL_SAVE_FLAG);
        canvas.restore();
        return src;
    }
    
    public static Bitmap overlayBitmap(Bitmap src, Bitmap overlay, int x,
            int y) {
        if (!src.isMutable()) {
            LogUtil.w(TAG, "overlayBitmap", "source bitmap is not mutable");
            return src;
        }
        Canvas canvas = new Canvas(src);
        canvas.drawBitmap(overlay, x, y, null);
        canvas.save(Canvas.ALL_SAVE_FLAG);
        canvas.restore();
        return src;
    }
}

Related Tutorials