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

private static Bitmap getScaleLogo(Bitmap logo, int w, int h) {
    if (logo == null)
        return null;
    Matrix matrix = new Matrix();
    float scaleFactor = Math.min(w * 1.0f / 5 / logo.getWidth(), h * 1.0f / 5 / logo.getHeight());
    matrix.postScale(scaleFactor, scaleFactor);
    Bitmap result = Bitmap.createBitmap(logo, 0, 0, logo.getWidth(), logo.getHeight(), matrix, true);
    return result;
}

From source file:Main.java

public static Bitmap zoom(Bitmap source, float width, float height) {
    Matrix matrix = new Matrix();
    float scaleX = width / source.getWidth();
    float scaleY = height / source.getHeight();
    matrix.postScale(scaleX, scaleY);

    Bitmap bitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
    return bitmap;
}

From source file:Main.java

public static Bitmap resizeBitmap(Bitmap input, int destWidth, int destHeight, int rotation)
        throws OutOfMemoryError {
    int dstWidth = destWidth;
    int dstHeight = destHeight;
    int srcWidth = input.getWidth();
    int srcHeight = input.getHeight();

    if ((rotation == 90) || (rotation == 270)) {
        dstWidth = destHeight;/*  w  ww  .  j  av a2 s. co  m*/
        dstHeight = destWidth;
    }

    boolean needsResize = false;

    if ((srcWidth > dstWidth) || (srcHeight > dstHeight)) {
        needsResize = true;

        float ratio1 = (float) srcWidth / dstWidth;
        float ratio2 = (float) srcHeight / dstHeight;
        Log.v("dsd", "ratio1:" + ratio1 + " ratio2:" + ratio2);

        if (ratio1 > ratio2) {
            float p = (float) dstWidth / srcWidth;
            dstHeight = (int) (srcHeight * p);
        } else {
            float p = (float) dstHeight / srcHeight;
            dstWidth = (int) (srcWidth * p);
        }
    } else {
        dstWidth = srcWidth;
        dstHeight = srcHeight;
    }

    Log.v("dsd", "dstWidth:" + dstWidth + " dstHeight:" + dstHeight + " srcWidth:" + srcWidth + " srcHeight:"
            + srcHeight);
    if ((needsResize) || (rotation != 0)) {
        Bitmap output = null;
        if (rotation == 0) {
            output = Bitmap.createScaledBitmap(input, dstWidth, dstHeight, true);
        } else {
            Matrix matrix = new Matrix();
            matrix.postScale((float) dstWidth / srcWidth, (float) dstHeight / srcHeight);
            matrix.postRotate(rotation);
            output = Bitmap.createBitmap(input, 0, 0, srcWidth, srcHeight, matrix, true);
        }
        return output;
    }
    return input;
}

From source file:Main.java

/**
 * Resize a bitmap object to fit the passed width and height
 * // w ww .j a v a 2  s.  com
 * @param input
 *           The bitmap to be resized
 * @param destWidth
 *           Desired maximum width of the result bitmap
 * @param destHeight
 *           Desired maximum height of the result bitmap
 * @return A new resized bitmap
 * @throws OutOfMemoryError
 *            if the operation exceeds the available vm memory
 */
public static Bitmap resizeBitmap(final Bitmap input, int destWidth, int destHeight, int rotation)
        throws OutOfMemoryError, Exception {

    int dstWidth = destWidth;
    int dstHeight = destHeight;
    final int srcWidth = input.getWidth();
    final int srcHeight = input.getHeight();

    if (rotation == 90 || rotation == 270) {
        dstWidth = destHeight;
        dstHeight = destWidth;
    }

    boolean needsResize = false;
    float p;
    if ((srcWidth > dstWidth) || (srcHeight > dstHeight)) {
        needsResize = true;
        if ((srcWidth > srcHeight) && (srcWidth > dstWidth)) {
            p = (float) dstWidth / (float) srcWidth;
            dstHeight = (int) (srcHeight * p);
        } else {
            p = (float) dstHeight / (float) srcHeight;
            dstWidth = (int) (srcWidth * p);
        }
    } else {
        dstWidth = srcWidth;
        dstHeight = srcHeight;
    }

    if (needsResize || rotation != 0) {
        Bitmap output;

        if (rotation == 0) {
            output = Bitmap.createScaledBitmap(input, dstWidth, dstHeight, true);
        } else {
            Matrix matrix = new Matrix();
            matrix.postScale((float) dstWidth / srcWidth, (float) dstHeight / srcHeight);
            matrix.postRotate(rotation);
            output = Bitmap.createBitmap(input, 0, 0, srcWidth, srcHeight, matrix, true);
        }
        return output;
    } else
        return input;
}

From source file:Main.java

public static Bitmap setScale(Bitmap source, int width, int height) {
    if (source == null || width < 1 || height < 1) {
        return null;
    }/*ww  w  .  j  av  a 2s. c  o  m*/
    int w = source.getWidth();
    int h = source.getHeight();
    Matrix matrix = new Matrix();
    float scaleWidth = ((float) width / w);
    float scaleHeight = ((float) height / h);
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap bitmap = Bitmap.createBitmap(source, 0, 0, w, h, matrix, true);
    return bitmap;
}

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 sx = ((float) w / width);
    float sy = ((float) h / height);
    matrix.postScale(sx, sy);
    Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);
    return new BitmapDrawable(newbmp);
}

From source file:Main.java

/**
 * Resize a bitmap object to fit the passed width and height
 *
 * @param input/*ww w .  java 2  s .c o  m*/
 *           The bitmap to be resized
 * @param destWidth
 *           Desired maximum width of the result bitmap
 * @param destHeight
 *           Desired maximum height of the result bitmap
 * @return A new resized bitmap
 * @throws OutOfMemoryError
 *            if the operation exceeds the available vm memory
 */
public static Bitmap resizeBitmap(final Bitmap input, int destWidth, int destHeight, int rotation)
        throws OutOfMemoryError {

    int dstWidth = destWidth;
    int dstHeight = destHeight;
    final int srcWidth = input.getWidth();
    final int srcHeight = input.getHeight();

    if (rotation == 90 || rotation == 270) {
        dstWidth = destHeight;
        dstHeight = destWidth;
    }

    boolean needsResize = false;
    float p;
    if ((srcWidth > dstWidth) || (srcHeight > dstHeight)) {
        needsResize = true;
        if ((srcWidth > srcHeight) && (srcWidth > dstWidth)) {
            p = (float) dstWidth / (float) srcWidth;
            dstHeight = (int) (srcHeight * p);
        } else {
            p = (float) dstHeight / (float) srcHeight;
            dstWidth = (int) (srcWidth * p);
        }
    } else {
        dstWidth = srcWidth;
        dstHeight = srcHeight;
    }

    if (needsResize || rotation != 0) {
        Bitmap output;

        if (rotation == 0) {
            output = Bitmap.createScaledBitmap(input, dstWidth, dstHeight, true);
        } else {
            Matrix matrix = new Matrix();
            matrix.postScale((float) dstWidth / srcWidth, (float) dstHeight / srcHeight);
            matrix.postRotate(rotation);
            output = Bitmap.createBitmap(input, 0, 0, srcWidth, srcHeight, matrix, true);
        }
        return output;
    } else
        return input;
}

From source file:Main.java

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

From source file:Main.java

@SuppressWarnings("deprecation")
public static int PutImageTargetHeight(Canvas canvas, Drawable image, double Angle, int x, int y,
        int newHeight) {

    float scale = (float) newHeight / (float) image.getIntrinsicHeight();
    float newWidth = (int) Math.round((float) image.getIntrinsicWidth() * scale);

    Bitmap bmp = ((BitmapDrawable) image).getBitmap();
    int width = bmp.getWidth();
    int height = bmp.getHeight();

    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // createa matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // rotate the Bitmap
    matrix.postRotate((float) Angle);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true);
    // make a Drawable from Bitmap to allow to set the BitMap
    // to the ImageView, ImageButton or what ever
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

    bmd.setBounds(x, y, x + bmd.getIntrinsicWidth(), y + bmd.getIntrinsicHeight());
    bmd.draw(canvas);//from ww w  .  j  a  v  a 2s .  c  o m

    return bmd.getIntrinsicWidth();

}

From source file:Main.java

public static Bitmap zoom(Bitmap source, float width, float height) {

    Matrix matrix = new Matrix();

    float scaleW = width / source.getWidth();
    float scaleH = height / source.getHeight();

    matrix.postScale(scaleW, scaleH);

    Bitmap bm = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);

    return bm;/*from   w w  w  . ja  v  a  2s . co m*/
}