loads photo, downscales to fit screen imageview - Android Animation

Android examples for Animation:Scale Animation

Description

loads photo, downscales to fit screen imageview

Demo Code


//package com.java2s;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Log;

public class Main {
    static final String DEBUG_TAG = "Camera_Helpers";
    private static final float ROTATE_90_DEGREES = 90;
    private static final int FIRST_PIX_X = 0;
    private static final int FIRST_PIX_Y = 0;

    static public Bitmap loadAndScaleImage(String originalImagePath,
            int viewheight, int viewwidth) {

        if (originalImagePath.isEmpty() || viewheight == 0
                || viewwidth == 0)// w ww . ja va2 s .c om
            throw new IllegalArgumentException();

        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(originalImagePath, options);

        int height = viewheight;
        int width = viewwidth;
        boolean flipImage = false;

        if (options.outWidth > options.outHeight) {
            height = viewwidth;
            width = viewheight;

            //flip it later
            flipImage = true;
        }
        options.inSampleSize = calculateInSampleSize(options, width, height);

        options.inJustDecodeBounds = false;
        Bitmap bmp = BitmapFactory.decodeFile(originalImagePath, options);

        if (flipImage) {
            Matrix matrix = new Matrix();
            matrix.postRotate(ROTATE_90_DEGREES);
            bmp = Bitmap.createBitmap(bmp, FIRST_PIX_X, FIRST_PIX_Y,
                    bmp.getWidth(), bmp.getHeight(), matrix, true);
        }
        return bmp;
    }

    /**
     * Calculates the size of the image
     * @param options
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    private static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {

        if (options == null || reqWidth == 0 || reqHeight == 0)
            throw new IllegalArgumentException();

        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int heightRatio = Math.round((float) height
                    / (float) reqHeight);
            final int widthRatio = Math.round((float) width
                    / (float) reqWidth);

            inSampleSize = heightRatio < widthRatio ? heightRatio
                    : widthRatio;
        }
        return inSampleSize;
    }
}

Related Tutorials