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

static Bitmap resizeBitmap(Bitmap bitmap, int maximumDimension, boolean scaleUpIfSmaller) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    float newScale;

    if (Math.max(width, height) <= maximumDimension && !scaleUpIfSmaller) {
        return bitmap;
    }/*from www . j  a  v a  2s .  co m*/

    if (width > height) {
        newScale = (float) maximumDimension / (float) width;
    } else {
        newScale = (float) maximumDimension / (float) height;
    }

    Matrix matrix = new Matrix();
    matrix.postScale(newScale, newScale);

    return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
}

From source file:Main.java

public static Drawable getDrawableImage(Context context, Bitmap source, int row, int col, int rowTotal,
        int colTotal, float multiple) {

    Bitmap temp = getBitmap(source, (col - 1) * source.getWidth() / colTotal,
            (row - 1) * source.getHeight() / rowTotal, source.getWidth() / colTotal,
            source.getHeight() / rowTotal);
    if (multiple != 1.0) {
        Matrix matrix = new Matrix();
        matrix.postScale(multiple, multiple);
        temp = Bitmap.createBitmap(temp, 0, 0, temp.getWidth(), temp.getHeight(), matrix, true);
    }//from  w w w  . ja v  a2  s. c  o  m
    Drawable d = new BitmapDrawable(context.getResources(), temp);
    return d;
}

From source file:Main.java

public static Bitmap getMatrixBitmap(Bitmap bm, int w, int h, boolean needRecycle) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    boolean isCompress = (width > w && height > h) && (w != 0 && h != 0);
    if (isCompress) {
        float scaleWidth = ((float) w) / width;
        float scaleHeight = ((float) h) / height;
        float scale = Math.max(scaleWidth, scaleHeight);
        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);
        Bitmap bitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        if (needRecycle && bm != null && bm != bitmap) {
            bm.recycle();//from www. ja va2 s.c o m
        }
        return bitmap;
    } else {
        return bm;
    }

}

From source file:Main.java

private static Bitmap resizeBitmap(Bitmap bmp, float width, float height) {
    float xscale = width / bmp.getWidth();
    float yscale = height / bmp.getHeight();
    Matrix matrix = new Matrix();
    // resize original image
    matrix.postScale(xscale, yscale);
    Bitmap dstbmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
    return dstbmp;
}

From source file:Main.java

public static Bitmap getResizedBitmap(Bitmap bitmap, int newHeight, int newWidth) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    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
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, false);

    return resizedBitmap;
}

From source file:Main.java

public static Bitmap ScaleAndRotateBitmap(Bitmap bitmap, int rotation, int flipHorizontal) {
    Matrix m = new Matrix();
    if (flipHorizontal != 0) {
        m.postScale(-1, 1);
    }//from  w  w  w. ja va  2s.co m
    if (rotation != 0) {
        m.postRotate(rotation);
    }
    Bitmap finalBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
    if (finalBitmap != bitmap) {
        bitmap.recycle();
    }
    return finalBitmap;
}

From source file:Main.java

public static Bitmap getImage(Context context, Bitmap source, int row, int col, int rowTotal, int colTotal,
        float multiple, boolean isRecycle) {
    Bitmap temp = getBitmap(source, (col - 1) * source.getWidth() / colTotal,
            (row - 1) * source.getHeight() / rowTotal, source.getWidth() / colTotal,
            source.getHeight() / rowTotal);

    if (isRecycle) {
        recycleBitmap(source);//  www  .  j a  v a  2 s .  c o m
    }
    if (multiple != 1.0) {
        Matrix matrix = new Matrix();
        matrix.postScale(multiple, multiple);
        temp = Bitmap.createBitmap(temp, 0, 0, temp.getWidth(), temp.getHeight(), matrix, true);
    }
    return temp;
}

From source file:Main.java

public static BitmapDrawable sizeChanger(Context context, ImageView imageView, int sizeOnDps, int orientation) {
    Drawable drawing = imageView.getDrawable();
    if (drawing == null) {
        return null;
    }//from  w ww  . ja v a  2s .  com
    Bitmap bitmap = ((BitmapDrawable) drawing).getBitmap();

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int bounding = dpToPx(250, context);

    float xScale = ((float) bounding) / width;
    float yScale = ((float) bounding) / height;
    float scale = (xScale <= yScale) ? xScale : yScale;

    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);
    matrix.postRotate(orientation, imageView.getDrawable().getBounds().width() / 2,
            imageView.getDrawable().getBounds().height() / 2);

    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    width = scaledBitmap.getWidth();
    height = scaledBitmap.getHeight();
    return new BitmapDrawable(scaledBitmap);
}

From source file:Main.java

public static Bitmap zoomBitmap3(Bitmap bitmap, int w, int h) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    float ratio = ((float) w / width);

    matrix.postScale(ratio, ratio);
    Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    return newbmp;
}

From source file:Main.java

public static void scaleImage(ImageView view, int boundBoxInDp) {
    // Get the ImageView and its bitmap
    Drawable drawing = view.getDrawable();
    Bitmap bitmap = ((BitmapDrawable) drawing).getBitmap();
    // Get current dimensions
    int width = bitmap.getWidth();
    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.
    float xScale = ((float) boundBoxInDp) / width;
    float yScale = ((float) boundBoxInDp) / height;
    float scale = (xScale <= yScale) ? xScale : yScale;
    // Create a matrix for the scaling and add the scaling data
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);
    // Create a new bitmap and convert it to a format understood by the ImageView
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    BitmapDrawable result = new BitmapDrawable(scaledBitmap);
    width = scaledBitmap.getWidth();/*from  w ww .ja v a2s.com*/
    height = scaledBitmap.getHeight();
    // Apply the scaled bitmap
    view.setImageDrawable(result);
    // Now change ImageView's dimensions to match the scaled image
    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) view.getLayoutParams();
    params.width = width;
    params.height = height;
    view.setLayoutParams(params);
}