Java tutorial
//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. * * 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; } }