create Scaled Bitmap by rotation and scale - Android Graphics

Android examples for Graphics:Bitmap Scale

Description

create Scaled Bitmap by rotation and scale

Demo Code


//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.graphics.Matrix;

public class Main {
    private static Bitmap createScaledBitmap(File f, int rotation, int scale)
            throws FileNotFoundException {
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;//from   w w  w . ja  v a 2 s.  c  o  m
        Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(f),
                null, o2);
        if (0 < rotation) {
            Matrix matrix = new Matrix();
            matrix.postRotate(rotation);
            Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                    bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            if (rotatedBitmap != bitmap) {
                bitmap.recycle();
                bitmap = null;
            }
            return rotatedBitmap;
        }
        return bitmap;
    }
}

Related Tutorials