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 mirrorBitmap(Bitmap bitmap) {
    Matrix matrix = new Matrix();
    matrix.preScale(-1.0f, 1.0f);//from   w  w  w .j  a  va  2 s.co m
    Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
    return newBitmap;
}

From source file:Main.java

public static Bitmap rotateBitmapNoRecycle(Bitmap b, int degrees) {
    if (degrees != 0 && b != null && !b.isRecycled()) {
        Matrix m = new Matrix();
        m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
        try {// w  ww .  ja  v  a 2 s.co  m
            Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
            return b2;
        } catch (OutOfMemoryError ex) {
        }
    }
    return b;
}

From source file:Main.java

public static Bitmap bigImage(Bitmap bmp, float big) {
    int bmpWidth = bmp.getWidth();
    int bmpHeight = bmp.getHeight();
    Matrix matrix = new Matrix();
    matrix.postScale(big, big);/* ww  w  .j ava 2 s  . c o  m*/
    return Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true);
}

From source file:Main.java

public static Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {
    Matrix m = new Matrix();
    m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
    float targetX, targetY;
    if (orientationDegree == 90) {
        targetX = bm.getHeight();/*from w ww.  ja v  a  2s.  c om*/
        targetY = 0;
    } else {
        targetX = bm.getHeight();
        targetY = bm.getWidth();
    }
    final float[] values = new float[9];
    m.getValues(values);
    float x1 = values[Matrix.MTRANS_X];
    float y1 = values[Matrix.MTRANS_Y];
    m.postTranslate(targetX - x1, targetY - y1);
    Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888);
    Paint paint = new Paint();
    Canvas canvas = new Canvas(bm1);
    canvas.drawBitmap(bm, m, paint);
    bm.recycle();
    bm = null;
    return bm1;
}

From source file:Main.java

public static Bitmap rotateBitmapTranslate(Bitmap bitmap, float degrees) {
    Bitmap mBitmap = null;/*w  w w . j a  va  2s .co m*/
    int width;
    int height;
    try {
        Matrix matrix = new Matrix();
        if ((degrees / 90) % 2 != 0) {
            width = bitmap.getWidth();
            height = bitmap.getHeight();
        } else {
            width = bitmap.getHeight();
            height = bitmap.getWidth();
        }
        int cx = width / 2;
        int cy = height / 2;
        matrix.preTranslate(-cx, -cy);
        matrix.postRotate(degrees);
        matrix.postTranslate(cx, cy);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mBitmap;
}

From source file:Main.java

public static Bitmap rotateAndMirror(Bitmap b, int degrees, boolean mirror) {
    if ((degrees != 0 || mirror) && b != null) {
        Matrix m = new Matrix();
        // Mirror first.
        // horizontal flip + rotation = -rotation + horizontal flip
        if (mirror) {
            m.postScale(-1, 1);//from  w  w  w  .  j  ava  2  s. co m
            degrees = (degrees + 360) % 360;
            if (degrees == 0 || degrees == 180) {
                m.postTranslate((float) b.getWidth(), 0);
            } else if (degrees == 90 || degrees == 270) {
                m.postTranslate((float) b.getHeight(), 0);
            } else {
                throw new IllegalArgumentException("Invalid degrees=" + degrees);
            }
        }
        if (degrees != 0) {
            // clockwise
            m.postRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);
        }

        try {
            Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);
            if (b != b2) {
                b.recycle();
                b = b2;
            }
        } catch (OutOfMemoryError ex) {
            // We have no memory to rotate. Return the original bitmap.
        }
    }
    return b;
}

From source file:Main.java

public static Bitmap rotateBitmap(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);/*w w  w .j av  a  2  s  .  c  o  m*/

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

From source file:Main.java

public static Bitmap createScaledBitmap(Bitmap bitmap, int width, int height) {
    Bitmap background = Bitmap.createBitmap((int) width, (int) height, Bitmap.Config.ARGB_8888);
    float originalWidth = bitmap.getWidth(), originalHeight = bitmap.getHeight();
    Canvas canvas = new Canvas(background);
    float scale = Math.max(width / originalWidth, height / originalHeight);
    float xTranslation = 0.0f, yTranslation = (height - originalHeight * scale) / 2.0f;
    if (originalWidth < originalHeight) {
        // scale = height / originalHeight;
        xTranslation = (width - originalWidth * scale) / 2.0f;
        yTranslation = 0.0f;/* w  ww.jav  a2  s  .  c o m*/
    }
    Matrix transformation = new Matrix();
    transformation.postTranslate(xTranslation, yTranslation);
    transformation.preScale(scale, scale);
    Paint paint = new Paint();
    paint.setFilterBitmap(true);
    canvas.drawBitmap(bitmap, transformation, paint);
    return background;
}

From source file:Main.java

private static Bitmap pictureDrawable2Bitmap(PictureDrawable pictureDrawable) {
    float intrinsicWidth = (float) pictureDrawable.getIntrinsicWidth();
    float intrinsicHeight = (float) pictureDrawable.getIntrinsicHeight();
    float f = 1.0f;
    if (intrinsicWidth < 600.0f || intrinsicHeight < 600.0f) {
        f = Math.min(600.0f / intrinsicWidth, 600.0f / intrinsicHeight);
        intrinsicWidth = (intrinsicWidth * f) + 0.5f;
        intrinsicHeight = (intrinsicHeight * f) + 0.5f;
    }/*  w w  w  .j a va2  s .c o m*/
    Bitmap createBitmap = Bitmap.createBitmap((int) intrinsicWidth, (int) intrinsicHeight, Config.ARGB_8888);
    Canvas canvas = new Canvas(createBitmap);
    Matrix matrix = new Matrix();
    matrix.preScale(f, f);
    canvas.concat(matrix);
    canvas.drawPicture(pictureDrawable.getPicture());
    return createBitmap;
}

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;
    }//w w w .  j  a v  a  2s. c  o m
    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);
}