Android Bitmap Scale scaleBitmap(Bitmap bitmap, int width, int height)

Here you can find the source of scaleBitmap(Bitmap bitmap, int width, int height)

Description

scale Bitmap

Declaration

public static Bitmap scaleBitmap(Bitmap bitmap, int width, int height) 

Method Source Code

//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Matrix;

import android.util.Log;

public class Main {
    public static Bitmap scaleBitmap(Bitmap bitmap, int width, int height) {
        long start = System.currentTimeMillis();
        if (bitmap == null) {
            return null;
        }//from  w ww.jav a2s  .c  om
        int newWidth = width;
        int newHeight = height;
        if (newHeight == 0) {
            newHeight = (int) (newWidth / (float) bitmap.getWidth() * bitmap
                    .getHeight());
        }
        Bitmap result = Bitmap.createBitmap(newWidth, newHeight,
                Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        Matrix matrix = new Matrix();
        float scaleX = 1;
        float scaleY = 1;
        scaleX = newWidth / (float) bitmap.getWidth();
        if (height != 0) {
            scaleY = newHeight / (float) bitmap.getHeight();
        } else {
            scaleY = scaleX;
        }
        matrix.postScale(scaleX, scaleY);
        canvas.drawBitmap(bitmap, matrix, null);
        bitmap.recycle();
        Log.i("map", "map cost=" + (System.currentTimeMillis() - start));
        return result;
    }

    public static Bitmap scaleBitmap(String path, int newWidth,
            int newHeight) {
        Bitmap bm = BitmapFactory.decodeFile(path);
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
                true);
        bm.recycle();
        return newbm;
    }
}

Related

  1. getScaleImage(final Bitmap bitmap, final float boundBoxInDp)
  2. generateScaledBitmap(Bitmap bitmap, int width, int height)
  3. generateScaledBitmap(Drawable drawable, int width, int height)
  4. generateScaledBitmap(byte[] bitMapData, int width, int height)
  5. crossStretchImageX(Bitmap image, int xsize)
  6. scaleBitmap(String path, int newWidth, int newHeight)
  7. scaleClipBitmapByCircle(Bitmap src, int nRadius, float fStrokeWidth)
  8. getScaledImageFromUri(Activity activity, Uri uri, int size)
  9. imageScale(Bitmap bitmap, int dst_w, int dst_h)