Example usage for android.graphics Matrix Matrix

List of usage examples for android.graphics Matrix Matrix

Introduction

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

Prototype

public Matrix() 

Source Link

Document

Create an identity matrix

Usage

From source file:Main.java

public static Bitmap scaleBitmap(Bitmap srcBitmap, int newSize) {
    int w = srcBitmap.getWidth();
    int h = srcBitmap.getHeight();

    // Determine the scaling required to get desired result.
    float scaleW = newSize / (float) w;
    float scaleH = newSize / (float) h;

    Matrix matrix = new Matrix();
    matrix.postScale(scaleW, scaleH);// w  w w  .  j a  va2 s. c  om

    return Bitmap.createBitmap(srcBitmap, 0, 0, w, h, matrix, false);
}

From source file:Main.java

public static Bitmap rotateAndMirror(Bitmap bitmap, int degree, boolean isMirror) throws OutOfMemoryError {

    if ((degree != 0 || isMirror) && bitmap != null) {
        Matrix m = new Matrix();
        m.setRotate(degree, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2);

        if (isMirror) {
            m.postScale(-1, 1);//w  ww  .jav  a2  s  . c  om
            degree = (degree + 360) % 360;
            if (degree == 0 || degree == 180) {
                m.postTranslate((float) bitmap.getWidth(), 0);
            } else if (degree == 90 || degree == 270) {
                m.postTranslate((float) bitmap.getHeight(), 0);
            } else {
                throw new IllegalArgumentException("Invalid degrees=" + degree);
            }
        }

        Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
        if (bitmap != bitmap2) {
            bitmap.recycle();
            System.gc();
            bitmap = bitmap2;
        }
    }
    return bitmap;
}

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 createResizedBitmap(Bitmap srcBit, int newWidth, int newHeight) {
    int width = srcBit.getWidth();
    int height = srcBit.getHeight();

    // calculate the scale - in this case = 0.4f
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap resizedBitmap = Bitmap.createBitmap(srcBit, 0, 0, width, height, matrix, true);

    width = resizedBitmap.getWidth();/*from w  ww  .j a v a2 s . c  o  m*/
    height = resizedBitmap.getHeight();

    Log.i("ImageResize",
            "Image Resize Result : " + Boolean.toString((newHeight == height) && (newWidth == width)));
    return resizedBitmap;
}

From source file:Main.java

/**
 * For rotating images/* w  w w .  j  a  va  2 s .  co m*/
 * @param bitmap Bitmap to rotate
 * @param degree Degree amount to rotate
 * @return
 */
public static Bitmap rotate(Bitmap bitmap, int degree) {
    if (bitmap == null) {
        return null;
    }
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    Matrix mtx = new Matrix();
    mtx.setRotate(degree);

    return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}

From source file:Main.java

public static Bitmap resizeImageByWidth(Bitmap defaultBitmap, int targetWidth) {
    int rawWidth = defaultBitmap.getWidth();
    int rawHeight = defaultBitmap.getHeight();
    float targetHeight = targetWidth * (float) rawHeight / (float) rawWidth;
    float scaleWidth = targetWidth / (float) rawWidth;
    float scaleHeight = targetHeight / (float) rawHeight;
    Matrix localMatrix = new Matrix();
    localMatrix.postScale(scaleHeight, scaleWidth);
    return Bitmap.createBitmap(defaultBitmap, 0, 0, rawWidth, rawHeight, localMatrix, true);
}

From source file:Main.java

public static Bitmap scaleBitmap(Bitmap bitmap, int reW, int reH) {
    if (bitmap == null) {
        return null;
    }/* w  w  w.java 2s.c om*/
    int bmpW = bitmap.getWidth();
    int bmpH = bitmap.getHeight();
    float wScale = reW * 1.0f / bmpW;
    float hScale = reH * 1.0f / bmpH;
    float scale = Math.max(wScale, hScale);
    Matrix matrix = new Matrix();
    matrix.setScale(scale, scale);
    return Bitmap.createBitmap(bitmap, 0, 0, bmpW, bmpH, matrix, true);
}

From source file:Main.java

static public Bitmap scaleBitmap(Bitmap bitmap, float scaleX, float scaleY) {
    Matrix matrix = new Matrix();
    matrix.postScale(scaleX, scaleY);/*from  w  w w.ja  v  a2  s . c om*/
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

From source file:Main.java

/**
 * Shrink the bitmap to the specified scale.
 *
 * @param bitmap to shrink.//www.j  ava 2s.c  o m
 * @param scale  the shrink scale, must be < 1.0.
 * @return the shrunk bitmap.
 */
public static Bitmap shrink(Bitmap bitmap, float scale) {
    if (scale >= 1.0f) {
        return bitmap.copy(bitmap.getConfig(), false);
    }

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

    return Bitmap.createBitmap(bitmap, 0, 0, (int) (scale * bitmap.getWidth()),
            (int) (scale * bitmap.getHeight()), matrix, true);
}

From source file:Main.java

/**
 * Expand the bitmap to the specified scale.
 *
 * @param bitmap to expand.//from  w w w . j a v  a2s.c  o  m
 * @param scale  the expand scale, must be > 1.0.
 * @return the expanded bitmap.
 */
public static Bitmap expand(Bitmap bitmap, float scale) {
    if (scale <= 1.0f) {
        return bitmap.copy(bitmap.getConfig(), false);
    }

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

    return Bitmap.createBitmap(bitmap, 0, 0, (int) (scale * bitmap.getWidth()),
            (int) (scale * bitmap.getHeight()), matrix, true);
}