Java tutorial
//package com.java2s; //License from project: Apache License import android.content.Context; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.os.Build; public class Main { /** * This method of image pixelization utilizes the bitmap scaling operations built * into the framework. By downscaling the bitmap and upscaling it back to its * original size (while setting the filter flag to false), the same effect can be * achieved with much better performance. */ public static BitmapDrawable builtInPixelization(Context context, float pixelizationFactor, Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); int downScaleFactorWidth = (int) (pixelizationFactor * width); downScaleFactorWidth = downScaleFactorWidth > 0 ? downScaleFactorWidth : 1; int downScaleFactorHeight = (int) (pixelizationFactor * height); downScaleFactorHeight = downScaleFactorHeight > 0 ? downScaleFactorHeight : 1; int downScaledWidth = width / downScaleFactorWidth; int downScaledHeight = height / downScaleFactorHeight; Bitmap pixelatedBitmap = Bitmap.createScaledBitmap(bitmap, downScaledWidth, downScaledHeight, false); /* Bitmap's createScaledBitmap method has a filter parameter that can be set to either * true or false in order to specify either bilinear filtering or point sampling * respectively when the bitmap is scaled up or now. * * Similarly, a BitmapDrawable also has a flag to specify the same thing. When the * BitmapDrawable is applied to an ImageView that has some scaleType, the filtering * flag is taken into consideration. However, for optimization purposes, this flag was * ignored in BitmapDrawables before Jelly Bean MR1. * * Here, it is important to note that prior to JBMR1, two bitmap scaling operations * are required to achieve the pixelization effect. Otherwise, a BitmapDrawable * can be created corresponding to the downscaled bitmap such that when it is * upscaled to fit the ImageView, the upscaling operation is a lot faster since * it uses internal optimizations to fit the ImageView. * */ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), pixelatedBitmap); bitmapDrawable.setFilterBitmap(false); return bitmapDrawable; } else { Bitmap upscaled = Bitmap.createScaledBitmap(pixelatedBitmap, width, height, false); return new BitmapDrawable(context.getResources(), upscaled); } } }