Android Bitmap Scale getScaleImage(final Bitmap bitmap, final float boundBoxInDp)

Here you can find the source of getScaleImage(final Bitmap bitmap, final float boundBoxInDp)

Description

get Scale Image

License

Open Source License

Declaration

public static Bitmap getScaleImage(final Bitmap bitmap,
            final float boundBoxInDp) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Mirakel is an Android App for managing your ToDo-Lists
 *
 * Copyright (c) 2013-2014 Anatolij Zelenin, Georg Semmler.
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     any later version.//from  ww  w .j  a  va  2s .  com
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import android.graphics.Bitmap;

import android.graphics.Matrix;

public class Main {
    public static Bitmap getScaleImage(final Bitmap bitmap,
            final float boundBoxInDp) {
        // Get current dimensions
        final int width = bitmap.getWidth();
        final int height = bitmap.getHeight();
        // Determine how much to scale: the dimension requiring
        // less scaling is.
        // closer to the its side. This way the image always
        // stays inside your.
        // bounding box AND either x/y axis touches it.
        final float xScale = boundBoxInDp / width;
        final float yScale = boundBoxInDp / height;
        final float scale = xScale <= yScale ? xScale : yScale;
        // Create a matrix for the scaling and add the scaling data
        final Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);
        // matrix.postRotate(rotate);
        // Create a new bitmap and convert it to a format understood
        // by the
        // ImageView
        final Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                width, height, matrix, false);
        // Apply the scaled bitmap
        return scaledBitmap;
    }
}

Related

  1. scaleImg(Bitmap bitmap, float scale)
  2. scaleImg(Bitmap bitmap, int newWidth, int newHeight)
  3. scaleImg(File file, int newWidth, int newHeight)
  4. getScaleBitmap(Bitmap origialBitmap, int dstWidth, int dstHeight)
  5. getScaleBitmap(Context context, int resId, int dstWidth, int dstHeight)
  6. generateScaledBitmap(Bitmap bitmap, int width, int height)
  7. generateScaledBitmap(Drawable drawable, int width, int height)
  8. generateScaledBitmap(byte[] bitMapData, int width, int height)
  9. crossStretchImageX(Bitmap image, int xsize)