Java tutorial
//package com.java2s; //License from project: Apache License import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.PixelFormat; import android.graphics.drawable.Drawable; public class Main { public static Bitmap bitmapZoomByHeight(Bitmap srcBitmap, float newHeight) { int srcWidth = srcBitmap.getWidth(); int srcHeight = srcBitmap.getHeight(); float scaleHeight = ((float) newHeight) / srcHeight; float scaleWidth = scaleHeight; return bitmapZoomByScale(srcBitmap, scaleWidth, scaleHeight); } public static Bitmap bitmapZoomByHeight(Drawable drawable, float newHeight) { Bitmap srcBitmap = drawableToBitmap(drawable); int srcWidth = srcBitmap.getWidth(); int srcHeight = srcBitmap.getHeight(); float scaleHeight = ((float) newHeight) / srcHeight; float scaleWidth = scaleHeight; return bitmapZoomByScale(srcBitmap, scaleWidth, scaleHeight); } public static Bitmap bitmapZoomByScale(Bitmap srcBitmap, float scaleWidth, float scaleHeight) { int srcWidth = srcBitmap.getWidth(); int srcHeight = srcBitmap.getHeight(); Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap resizedBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcWidth, srcHeight, matrix, true); if (resizedBitmap != null) { srcBitmap = null; return resizedBitmap; } else { return srcBitmap; } } public static Bitmap drawableToBitmap(Drawable drawable) { int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565); Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, width, height); drawable.draw(canvas); return bitmap; } }