Example usage for android.graphics Matrix postScale

List of usage examples for android.graphics Matrix postScale

Introduction

In this page you can find the example usage for android.graphics Matrix postScale.

Prototype

public boolean postScale(float sx, float sy) 

Source Link

Document

Postconcats the matrix with the specified scale.

Usage

From source file:Main.java

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

From source file:Main.java

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

From source file:Main.java

/***
 * Downsize the bitmap to at most the indicated ratio of the max specified size
 * @param bm Bitmap to resize/*ww w  .  j av a2 s  .c o m*/
 * @param maxX pixels of max width
 * @param maxY pixels of max height
 * @param maxRatio The max ratio wrt the display size
 * @return the new bitmap, OR input bitmap if no change needed
 */
public static Bitmap getResizedBitmap(Bitmap bm, int maxX, int maxY, double maxRatio) {

    // we have an starting bitmap object to work from
    if (null == bm) {
        return bm;
    }

    // Get current size and h:w ratio
    int height = bm.getHeight();
    int width = bm.getWidth();

    // ensure bitmap size is valid
    if (0 == height || 0 == width) {
        return bm;
    }

    // What is the height to width ratio - will always be > 0 at this point
    double ratio = height / width;

    // Figure out new max size
    int newHeight = (int) (Math.min(maxX, maxY) * maxRatio);
    int newWidth = (int) (newHeight / ratio);

    // If we don't need to downsize, then return with the original
    if (newHeight >= height && newWidth >= width) {
        return bm;
    }

    // Calculate the scaling factors in both the x and y direction
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // create a matrix for the manipulation
    Matrix matrix = new Matrix();

    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);

    // recreate the new Bitmap, allowing for failure
    try {
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
        return resizedBitmap;
    } catch (Exception e) {
        return bm;
    }
}

From source file:Main.java

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

From source file:Main.java

public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
    Bitmap newbmp = null;/*  ww w  . ja  v a 2 s.  c o m*/
    if (bitmap != null) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidht = ((float) w / width);
        float scaleHeight = ((float) h / height);
        matrix.postScale(scaleWidht, scaleHeight);
        newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    }
    return newbmp;
}

From source file:Main.java

public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
    int width = drawable.getIntrinsicWidth();
    int height = drawable.getIntrinsicHeight();
    Bitmap oldbmp = drawableToBitmap(drawable);
    Matrix matrix = new Matrix();
    float scaleWidth = ((float) w / width);
    float scaleHeight = ((float) h / height);
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);
    return new BitmapDrawable(null, newbmp);
}

From source file:Main.java

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;
}

From source file:Main.java

public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
    Bitmap newbmp = null;/*from   w w  w  .ja  v  a2 s. c  om*/
    if (bitmap != null) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidht = ((float) w / width);
        float scaleHeight = ((float) h / height);
        matrix.postScale(scaleWidht, scaleHeight);
        try {
            newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
        } catch (Exception e) {
            newbmp = bitmap;
        }
    }
    return newbmp;
}

From source file:Main.java

public static void createScaledImage(String sourceFile, String destinationFile, int desiredWidth,
        int desiredHeight) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/* w w w. java  2  s. c  o m*/
    BitmapFactory.decodeFile(sourceFile, options);

    int srcWidth = options.outWidth;
    int srcHeight = options.outHeight;

    if (desiredWidth > srcWidth) {
        desiredWidth = srcWidth;
    }

    int inSampleSize = 1;
    while (srcWidth / 2 > desiredWidth) {
        srcWidth /= 2;
        srcHeight /= 2;
        inSampleSize *= 2;
    }

    float desiredScale = (float) desiredWidth / srcWidth;

    options.inJustDecodeBounds = false;
    options.inDither = false;
    options.inSampleSize = inSampleSize;
    options.inScaled = false;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(sourceFile, options);

    Matrix matrix = new Matrix();
    matrix.postScale(desiredScale, desiredScale);
    Bitmap scaledBitmap = Bitmap.createBitmap(sampledSrcBitmap, 0, 0, sampledSrcBitmap.getWidth(),
            sampledSrcBitmap.getHeight(), matrix, true);
    sampledSrcBitmap = null;

    try {
        FileOutputStream out = new FileOutputStream(destinationFile);
        scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 85, out);
        scaledBitmap = null;
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static Bitmap zoomBitmap(Bitmap bitmap, float w, float h) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    float scaleW = ((float) w / width);
    float scaleH = ((float) h / height);
    matrix.postScale(scaleW, scaleH);
    Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

    return newBmp;
}