rotating ImageView - Android User Interface

Android examples for User Interface:ImageView

Description

rotating ImageView

Demo Code


//package com.java2s;

import android.graphics.Bitmap;

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

public class Main {

    public static Bitmap rotatingImageView(int angle, Bitmap bitmap) {
        //  /*from   w w w .  ja  v a 2  s .co  m*/
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        System.out.println("angle2=" + angle);
        // 
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return resizedBitmap;
    }

    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