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 getScreenFitBitmap(Bitmap src, int dispWidth, int dispHeight) {
    int srcWidth = src.getWidth();
    int srcHeight = src.getHeight();

    float scale = (float) dispWidth / srcWidth;

    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);/* w ww .  j  a v a  2 s  .c  o  m*/

    return Bitmap.createBitmap(src, 0, 0, srcWidth, srcHeight, matrix, true);
}

From source file:Main.java

public static Bitmap getBitmap(Bitmap background, Bitmap contour, float alpha) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);/*from w w w.  j  a v a  2  s  .  c  om*/
    paint.setColor(Color.WHITE);
    paint.setDither(false);
    Bitmap bitmap = Bitmap.createBitmap(contour.getWidth(), contour.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Matrix m = new Matrix();
    m.setScale(contour.getWidth() * 1.0f / background.getWidth(),
            contour.getHeight() * 1.0f / background.getHeight());
    paint.setAlpha((int) (alpha * 0xff));
    canvas.drawBitmap(background, m, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    paint.setAlpha(0xff);
    canvas.drawBitmap(contour, 0, 0, paint);
    return bitmap;
}

From source file:Main.java

public static Bitmap scaleBitmap(int height, int width, String pathImage) {
    Bitmap desBitmap = null;/*from  w ww .  j  a  va2 s .  c  o m*/
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(pathImage, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW / width, photoH / height);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor << 1;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(pathImage, bmOptions);

    Matrix mtx = new Matrix();
    mtx.postRotate(90);
    // Rotating Bitmap
    Bitmap rotatedBMP = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mtx, true);

    if (rotatedBMP != bitmap) {
        bitmap.recycle();
    }

    return rotatedBMP;
}

From source file:Main.java

/**
 * Returns an android.graphics.Matrix resulting from a 9-float matrix array in row-major order.
 * Undefined behavior when the array is not of size 9 or is null.
 *
 * @param m3 9-float matrix array in row-major order
 *//*  w  w  w.  j a v a2  s.com*/

private static Matrix createMatrixFrom3x3(float[] m3) {
    Matrix m = new Matrix();
    m.setValues(m3);
    return m;
}

From source file:Main.java

public static Bitmap extractThumbnail(Bitmap source, int width, int height, int options) {
    if (source == null) {
        return null;
    }//from   w  ww  .jav a 2  s.  c om

    float scale;
    if (source.getWidth() < source.getHeight()) {
        scale = width / (float) source.getWidth();
    } else {
        scale = height / (float) source.getHeight();
    }
    Matrix matrix = new Matrix();
    matrix.setScale(scale, scale);
    Bitmap thumbnail = transform(matrix, source, width, height, OPTIONS_SCALE_UP | options);
    return thumbnail;
}

From source file:Main.java

public static Bitmap zoom(Bitmap bitmap, float wf, float hf) {
    Matrix matrix = new Matrix();
    matrix.postScale(wf, hf);/*from ww  w  .ja  v  a  2s.co  m*/
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

From source file:Main.java

public static Bitmap compressFixBitmap(Bitmap bitMap, int outWidth, int outHeight) {
    int width = bitMap.getWidth();
    int height = bitMap.getHeight();
    float scaleWidth = ((float) outWidth) / width;
    float scaleHeight = ((float) outHeight) / height;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    return Bitmap.createBitmap(bitMap, 0, 0, width, height, matrix, true);
}

From source file:Main.java

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

From source file:Main.java

static void offsetDescendantRect(ViewGroup group, View child, Rect rect) {
    Matrix m = sMatrix.get();/*  w ww  . jav a2  s .c o  m*/
    if (m == null) {
        m = new Matrix();
        sMatrix.set(m);
    } else {
        m.set(IDENTITY);
    }

    offsetDescendantMatrix(group, child, m);

    RectF rectF = sRectF.get();
    if (rectF == null) {
        rectF = new RectF();
    }
    rectF.set(rect);
    m.mapRect(rectF);
    rect.set((int) (rectF.left + 0.5f), (int) (rectF.top + 0.5f), (int) (rectF.right + 0.5f),
            (int) (rectF.bottom + 0.5f));
}

From source file:Main.java

/**
 * Scales the provided bitmap to the height and width provided (using antialiasing).
 * (Alternative method for scaling bitmaps since Bitmap.createScaledBitmap(...) produces low quality bitmaps.)
 *
 * @param bitmap    is the bitmap to scale.
 * @param newWidth  is the desired width of the scaled bitmap.
 * @param newHeight is the desired height of the scaled bitmap.
 * @return the scaled bitmap.//  w ww  . ja va 2  s.c o  m
 */
public static Bitmap scaleBitmap(Bitmap bitmap, int newWidth, int newHeight) {
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);

    float scaleX = newWidth / (float) bitmap.getWidth();
    float scaleY = newHeight / (float) bitmap.getHeight();
    float pivotX = 0;
    float pivotY = 0;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(scaleX, scaleY, pivotX, pivotY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, 0, 0, new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG));

    return scaledBitmap;
}