Example usage for android.graphics Matrix postTranslate

List of usage examples for android.graphics Matrix postTranslate

Introduction

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

Prototype

public boolean postTranslate(float dx, float dy) 

Source Link

Document

Postconcats the matrix with the specified translation.

Usage

From source file:Main.java

/**
 * Post translates the matrix to center the given bounds inside the view.
 *//*from   w w  w  . j a va2  s  .  c  o m*/
public static void postCenterMatrix(RectF contentBounds, View view, Matrix matrix) {
    matrix.postTranslate((view.getWidth() - contentBounds.width()) / 2,
            (view.getHeight() - contentBounds.height()) / 2);
}

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;//from w ww  .  j  ava2s. 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

public static void drawBitmapCenter(Canvas canvas, float f, float f1, float f2, boolean flag, boolean flag1,
        Bitmap bitmap, Paint paint) {
    if (flag) {/*from w w  w .  j  a  v a  2  s. co  m*/
        f -= (f2 * (float) bitmap.getWidth()) / 2.0F;
    }
    if (flag1) {
        f1 -= (f2 * (float) bitmap.getHeight()) / 2.0F;
    }
    Matrix matrix = new Matrix();
    matrix.setScale(f2, f2);
    matrix.postTranslate(f, f1);
    canvas.drawBitmap(bitmap, matrix, paint);
}

From source file:Main.java

/**
 * Post rotates the matrix and bounds for the given bounds and degrees.
 *//*from ww  w. j a  va 2s . c  o m*/
public static void postRotateMatrix(float degrees, RectF bounds, Matrix matrix) {
    matrix.postRotate(degrees);
    matrix.mapRect(bounds);
    matrix.postTranslate(-bounds.left, -bounds.top);
}

From source file:Main.java

public static Bitmap getFlippedBitmap(Resources res, int resId) {

    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inJustDecodeBounds = true;/* ww w  .j  av a2s .co  m*/
    //Below line is necessary to fill in opt.outWidth, opt.outHeight
    Bitmap b = BitmapFactory.decodeResource(res, resId, opt);

    b = Bitmap.createBitmap(opt.outWidth, opt.outHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(b);

    Matrix flipHorizontalMatrix = new Matrix();
    flipHorizontalMatrix.setScale(-1, 1);
    flipHorizontalMatrix.postTranslate(b.getWidth(), 0);

    Bitmap bb = BitmapFactory.decodeResource(res, resId);
    canvas.drawBitmap(bb, flipHorizontalMatrix, null);

    return b;
}

From source file:Main.java

private static Matrix createTransform(Matrix transform, float scaleX, float scaleY, float translateX,
        float translateY) {
    transform.setScale(scaleX, scaleY);//from w ww .j  a  v  a 2s.  c  o  m
    transform.postTranslate(translateX, translateY);
    return transform;
}

From source file:Main.java

public static int loadTexture(Resources resources, int resource, int internalFormat, boolean flip) {
    int[] textures = s_LOAD_TEXTURE_ID.get();
    if (textures == null) {
        textures = new int[1];
        s_LOAD_TEXTURE_ID.set(textures);
    }/*  w w  w  .ja  va 2 s  . c  om*/

    glActiveTexture(GL_TEXTURE0);
    glGenTextures(1, textures, 0);

    int texture = textures[0];
    glBindTexture(GL_TEXTURE_2D, texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    Bitmap bitmap = BitmapFactory.decodeResource(resources, resource);

    final int width = bitmap.getWidth();
    final int height = bitmap.getHeight();

    if (flip) {
        Matrix matrix = new Matrix();
        matrix.setScale(1, -1);
        matrix.postTranslate(0, height);
        Bitmap flipBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

        bitmap.recycle();
        bitmap = flipBitmap;
    }

    GLUtils.texImage2D(GL_TEXTURE_2D, 0, internalFormat, bitmap, GL_UNSIGNED_BYTE, 0);

    bitmap.recycle();

    glBindTexture(GL_TEXTURE_2D, 0);

    return texture;
}

From source file:Main.java

/**
 * Returns a transformation matrix from one reference frame into another.
 * Handles cropping (if maintaining aspect ratio is desired) and rotation.
 *
 * @param srcWidth Width of source frame.
 * @param srcHeight Height of source frame.
 * @param dstWidth Width of destination frame.
 * @param dstHeight Height of destination frame.
 * @param applyRotation Amount of rotation to apply from one frame to another.
 *  Must be a multiple of 90.//w  w w  .j a va  2  s  .c  om
 * @param maintainAspectRatio If true, will ensure that scaling in x and y remains constant,
 * cropping the image if necessary.
 * @return The transformation fulfilling the desired requirements.
 */
public static Matrix getTransformationMatrix(final int srcWidth, final int srcHeight, final int dstWidth,
        final int dstHeight, final int applyRotation, final boolean maintainAspectRatio) {
    final Matrix matrix = new Matrix();

    if (applyRotation != 0) {
        // Translate so center of image is at origin.
        matrix.postTranslate(-srcWidth / 2.0f, -srcHeight / 2.0f);

        // Rotate around origin.
        matrix.postRotate(applyRotation);
    }

    // Account for the already applied rotation, if any, and then determine how
    // much scaling is needed for each axis.
    final boolean transpose = (Math.abs(applyRotation) + 90) % 180 == 0;

    final int inWidth = transpose ? srcHeight : srcWidth;
    final int inHeight = transpose ? srcWidth : srcHeight;

    // Apply scaling if necessary.
    if (inWidth != dstWidth || inHeight != dstHeight) {
        final float scaleFactorX = dstWidth / (float) inWidth;
        final float scaleFactorY = dstHeight / (float) inHeight;

        if (maintainAspectRatio) {
            // Scale by minimum factor so that dst is filled completely while
            // maintaining the aspect ratio. Some image may fall off the edge.
            final float scaleFactor = Math.max(scaleFactorX, scaleFactorY);
            matrix.postScale(scaleFactor, scaleFactor);
        } else {
            // Scale exactly to fill dst from src.
            matrix.postScale(scaleFactorX, scaleFactorY);
        }
    }

    if (applyRotation != 0) {
        // Translate back from origin centered reference to destination frame.
        matrix.postTranslate(dstWidth / 2.0f, dstHeight / 2.0f);
    }

    return matrix;
}

From source file:Main.java

public static Matrix getRotateMatrix(Bitmap bitmap, int rotation) {
    Matrix matrix = new Matrix();
    if (bitmap != null && rotation != 0) {
        int cx = bitmap.getWidth() / 2;
        int cy = bitmap.getHeight() / 2;
        matrix.preTranslate(-cx, -cy);/*from w  w w .j  av  a 2s.  c o m*/
        matrix.postRotate(rotation);
        matrix.postTranslate(cx, cy);
    }
    return matrix;
}

From source file:Main.java

public static void prepareMatrix(Matrix matrix, boolean mirror, int displayOrientation, int viewWidth,
        int viewHeight) {
    matrix.setScale((float) (mirror ? -1 : 1), 1.0F);
    matrix.postRotate((float) displayOrientation);
    matrix.postScale((float) viewWidth / 2000.0F, (float) viewHeight / 2000.0F);
    matrix.postTranslate((float) viewWidth / 2.0F, (float) viewHeight / 2.0F);
}