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 Drawable scaleImage(Drawable paramDrawable, float paramFloat1, float paramFloat2) {
    BitmapDrawable localBitmapDrawable = null;
    if (paramDrawable != null) {
        Bitmap localBitmap = ((BitmapDrawable) paramDrawable).getBitmap();
        int i = localBitmap.getWidth();
        int j = localBitmap.getHeight();
        Matrix localMatrix = new Matrix();
        localMatrix.postScale(paramFloat1, paramFloat2);
        localBitmapDrawable = new BitmapDrawable(
                Bitmap.createBitmap(localBitmap, 0, 0, i, j, localMatrix, true));
    }//w  w  w .j  a  va  2s .  co  m
    return localBitmapDrawable;
}

From source file:Main.java

public static Bitmap setReflection(Bitmap bitmap, int distance, float ratio) {
    final int reflectionGap = distance;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);/*w  w  w. ja v a 2  s  . c  o m*/
    Bitmap reflectionBitmap = Bitmap.createBitmap(bitmap, 0, height / 2, width, (int) (height * ratio), matrix,
            false);
    Bitmap withReflectionBitmap = Bitmap.createBitmap(width, (height + (int) (height * ratio) + reflectionGap),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(withReflectionBitmap);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint defaultPaint = new Paint();
    defaultPaint.setColor(Color.TRANSPARENT);
    canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
    canvas.drawBitmap(reflectionBitmap, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, withReflectionBitmap.getHeight(),
            0x70ffffff, 0x00ffffff, Shader.TileMode.MIRROR);
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

    canvas.drawRect(0, height, width, withReflectionBitmap.getHeight(), paint);
    return withReflectionBitmap;
}

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);//  w ww.  j  a  va2s.  com
        Bitmap bitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        if (needRecycle && bm != null && bm != bitmap) {
            bm.recycle();
        }
        return bitmap;
    } else {
        return bm;
    }

}

From source file:Main.java

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);/*from ww  w .j a  va2 s  .com*/

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, Shader.TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:Main.java

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);//from   w w w .j a v a2s .c  o m

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in   
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient   
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:Main.java

public static Bitmap getReflectionImageWithOrigin(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);/*from   w w  w .  j a va 2 s. c  om*/

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:Main.java

public static Bitmap scaleWithRatio(Context c, int res, int max) {
    Bitmap bmp = imageResourceToBitmap(c, res, max);

    int width = bmp.getWidth();
    int height = bmp.getHeight();

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

    // createa matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);

    // recreate the new Bitmap and return it
    return Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true);
}

From source file:Main.java

public static Bitmap BITMAP_RESIZER(Bitmap bitmap, int newWidth, int newHeight) {
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);

    float ratioX = newWidth / (float) bitmap.getWidth();
    float ratioY = newHeight / (float) bitmap.getHeight();
    float middleX = newWidth / 2.0f;
    float middleY = newHeight / 2.0f;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);/*from  w  w  w.  j av a  2 s.  c  o m*/
    canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2,
            new Paint(Paint.FILTER_BITMAP_FLAG));

    return scaledBitmap;

}

From source file:Main.java

public static Bitmap resizeBitmap(Bitmap bm, int w, int h) {
    Bitmap BitmapOrg = bm;/*  www  . j  a v  a2 s. c o m*/

    int width = BitmapOrg.getWidth();
    int height = BitmapOrg.getHeight();

    float scaleWidth = ((float) w) / width;
    float scaleHeight = ((float) h) / height;

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    return Bitmap.createBitmap(BitmapOrg, 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 .j a v  a 2  s. com*/
    Drawable d = new BitmapDrawable(context.getResources(), temp);
    return d;
}