Android Bitmap Scale scaleBitmap(Bitmap bitmap, int targetwidth)

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

Description

scale Bitmap

Declaration

public static Bitmap scaleBitmap(Bitmap bitmap, int targetwidth) 

Method Source Code

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Picture;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.view.View;
import android.webkit.WebView;
import android.widget.ScrollView;

public class Main{
    public static Bitmap scaleBitmap(Bitmap bitmap, int targetwidth) {
        int imageWidth = targetwidth;
        if (bitmap != null
                && (bitmap.getWidth() < imageWidth || bitmap.getHeight() < imageWidth)) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            int min = width;
            if (width > height) {
                min = height;//from w  w  w .  j  av  a  2  s  .c o m
            }
            float scale = imageWidth / min;

            bitmap = BitmapUtils.zoomBitmap(bitmap, (int) (width * scale),
                    (int) (height * scale));

        }
        return bitmap;
    }
    public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidth = ((float) width / w);
        float scaleHeight = ((float) height / h);
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix,
                true);
        return newbmp;
    }
}

Related

  1. getScalBitmap(Context context, Uri uri, int destW, int destH)
  2. getScalBitmap(String file, int destW, int destH)
  3. getScaledBitmap(Bitmap bitmap, int width, int height)
  4. getScaledBitmapByHeight(Bitmap bm, int newHeight)
  5. scaleBitmap(Bitmap bitmap, int dstWidth, int dstHeight)
  6. scaleBitmapIfNeededToSize(Bitmap bitmap, long size)
  7. scaleBitmapKeepRatio(Bitmap bitmap, int dstWidth, int dstHeight)
  8. scaleToTextSize(Bitmap bmp, float textSize)
  9. scaleToScreen(Activity activity, Bitmap bitmap)