Here you can find the source of resizeDownToPixels(Bitmap bitmap, int targetPixels, boolean recycle)
public static Bitmap resizeDownToPixels(Bitmap bitmap, int targetPixels, boolean recycle)
//package com.java2s; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; public class Main { public static Bitmap resizeDownToPixels(Bitmap bitmap, int targetPixels, boolean recycle) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); float scale = (float) Math.sqrt((double) targetPixels / (width * height));/*from w w w . j av a 2 s. c om*/ if (scale >= 1.0f) return bitmap; return resizeBitmapByScale(bitmap, scale, recycle); } public static Bitmap resizeBitmapByScale(Bitmap bitmap, float scale, boolean recycle) { int width = Math.round(bitmap.getWidth() * scale); int height = Math.round(bitmap.getHeight() * scale); if (width == bitmap.getWidth() && height == bitmap.getHeight()) return bitmap; Bitmap target = Bitmap.createBitmap(width, height, getConfig(bitmap)); Canvas canvas = new Canvas(target); canvas.scale(scale, scale); Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG); canvas.drawBitmap(bitmap, 0, 0, paint); if (recycle) bitmap.recycle(); return target; } private static Bitmap.Config getConfig(Bitmap bitmap) { Bitmap.Config config = bitmap.getConfig(); if (config == null) { config = Bitmap.Config.ARGB_8888; } return config; } }