scale And Rotate Bitmap Image - Android Graphics

Android examples for Graphics:Bitmap Scale

Description

scale And Rotate Bitmap Image

Demo Code


//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.Matrix;

import android.graphics.drawable.BitmapDrawable;

public class Main {
    public static BitmapDrawable scaleAndRotate(Bitmap bitmap,
            int newWidth, int newHeight, float degrees) {
        Bitmap bitmapOrg = bitmap;/*from  w w  w  .ja  v a 2s . c  o  m*/

        int width = bitmapOrg.getWidth();
        int height = bitmapOrg.getHeight();
        // calculate the scale - in this case = 0.4f
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // createa matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
        // rotate the Bitmap
        matrix.postRotate(degrees);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
                height, matrix, true);
        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
        return bmd;
    }

    public static BitmapDrawable scaleAndRotate(Bitmap bitmap, float scale,
            float degrees) {
        // load the origial BitMap (500 x 500 px)
        Bitmap bitmapOrg = bitmap;
        int width = bitmapOrg.getWidth();
        int height = bitmapOrg.getHeight();
        float scaleWidth = ((float) scale) * width;
        float scaleHeight = ((float) scale) * height;
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scale, scale);
        // rotate the Bitmap
        matrix.postRotate(degrees);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
                height, matrix, true);
        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
        return bmd;
    }
}

Related Tutorials