Android Bitmap Scale scaleBitmap(String path, int newWidth, int newHeight)

Here you can find the source of scaleBitmap(String path, int newWidth, int newHeight)

Description

scale Bitmap

Declaration

public static Bitmap scaleBitmap(String path, int newWidth,
            int newHeight) 

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;
        }//  w  w  w  . ja  v  a  2s.  c  o m
        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. generateScaledBitmap(Bitmap bitmap, int width, int height)
  2. generateScaledBitmap(Drawable drawable, int width, int height)
  3. generateScaledBitmap(byte[] bitMapData, int width, int height)
  4. crossStretchImageX(Bitmap image, int xsize)
  5. scaleBitmap(Bitmap bitmap, int width, int height)
  6. scaleClipBitmapByCircle(Bitmap src, int nRadius, float fStrokeWidth)
  7. getScaledImageFromUri(Activity activity, Uri uri, int size)
  8. imageScale(Bitmap bitmap, int dst_w, int dst_h)
  9. scaleBitmap(Bitmap bitmap, float scale)