Rotate a created bitmap - Android Graphics

Android examples for Graphics:Bitmap Rotate

Description

Rotate a created bitmap

Demo Code


//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;
import android.graphics.Matrix;

public class Main {
    public static float curDegrees = 0;

    public static Bitmap left(Bitmap bitmap) {
        Matrix matrix = new Matrix();
        matrix.setRotate(curDegrees = (curDegrees - 90) % 360);
        Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0,
                bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        System.out.println(curDegrees);
        return resizeBmp;
    }/*from w ww . ja v  a 2  s  .c  o m*/

    public static Bitmap createBitmap(String path, int w, int h) {
        try {
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inJustDecodeBounds = true;
            // inJustDecodeBoundstrue
            BitmapFactory.decodeFile(path, opts);
            int srcWidth = opts.outWidth;// 
            int srcHeight = opts.outHeight;// 
            int destWidth = 0;
            int destHeight = 0;
            // 
            double ratio = 0.0;
            if (srcWidth < w || srcHeight < h) {
                ratio = 0.0;
                destWidth = srcWidth;
                destHeight = srcHeight;
            } else if (srcWidth > srcHeight) {// maxLength
                ratio = (double) srcWidth / w;
                destWidth = w;
                destHeight = (int) (srcHeight / ratio);
            } else {
                ratio = (double) srcHeight / h;
                destHeight = h;
                destWidth = (int) (srcWidth / ratio);
            }
            BitmapFactory.Options newOpts = new BitmapFactory.Options();
            // inSampleSizeSDK2
            newOpts.inSampleSize = (int) ratio + 1;
            // inJustDecodeBoundsfalse
            newOpts.inJustDecodeBounds = false;
            // inSampleSize
            newOpts.outHeight = destHeight;
            newOpts.outWidth = destWidth;
            // 
            return BitmapFactory.decodeFile(path, newOpts);
        } catch (Exception e) {
            return null;
        }
    }
}

Related Tutorials