Android examples for Graphics:Bitmap Effect
get Adapted Bitmap
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 . ja va 2 s . c o m*/ private static final String TAG = ImgUtil.class.getSimpleName(); public static Bitmap getAdaptedBitmap(Bitmap src, int width, int height, boolean crop, boolean recycleSrc) { int srcWidth = src.getWidth(); int srcHeight = src.getHeight(); int dstWidth = width; int dstHeight = height; if (width * srcHeight > height * srcWidth) { // scaleX > scaleY if (crop) { dstHeight = srcHeight * width / srcWidth; } else { dstWidth = srcWidth * height / srcHeight; } } else if (width * srcHeight < height * srcWidth) { // scaleX < scaleY if (crop) { dstWidth = srcWidth * height / srcHeight; } else { dstHeight = srcHeight * width / srcWidth; } } else { // scaleX == scaleY, crop = false; } Bitmap bitmap = getScaledBitmap(src, dstWidth, dstHeight, recycleSrc); if (crop) { bitmap = getCroppedBitmap(bitmap, (dstWidth - width) / 2, (dstHeight - height) / 2, width, height, true); } return bitmap; } public static Bitmap getScaledBitmap(Bitmap src, float sx, float sy, boolean recycleSrc) { Matrix matrix = new Matrix(); matrix.setScale(sx, sy); return getTransformedBitmap(src, matrix, recycleSrc); } public static Bitmap getScaledBitmap(Bitmap src, float sx, float sy, float px, float py, boolean recycleSrc) { Matrix matrix = new Matrix(); matrix.setScale(sx, sy, px, py); return getTransformedBitmap(src, matrix, recycleSrc); } public static Bitmap getScaledBitmap(Bitmap src, int w, int h, boolean recycleSrc) { if (src == null) { LogUtil.w(TAG, "getScaledBitmap", "source bitmap is null"); return null; } Bitmap dst = null; try { dst = Bitmap.createScaledBitmap(src, w, h, true); if (recycleSrc && dst != src) { src.recycle(); } } catch (Exception e) { e.printStackTrace(); } return dst; } public static Bitmap getCroppedBitmap(Bitmap src, int x, int y, int width, int height, boolean recycleSrc) { if (src == null) { LogUtil.w(TAG, "getCroppedBitmap", "source bitmap is null"); return null; } Bitmap dst = null; try { dst = Bitmap.createBitmap(src, x, y, width, height); if (recycleSrc && dst != src) { src.recycle(); } } catch (Exception e) { e.printStackTrace(); } return dst; } public static Bitmap getTransformedBitmap(Bitmap src, Matrix matrix, boolean recycleSrc) { if (src == null) { LogUtil.w(TAG, "getTransformedBitmap", "source bitmap is null"); return null; } Bitmap dst = null; try { dst = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true); if (recycleSrc && dst != src) { src.recycle(); } } catch (Exception e) { e.printStackTrace(); } return dst; } }