Example usage for android.graphics Matrix mapRect

List of usage examples for android.graphics Matrix mapRect

Introduction

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

Prototype

public boolean mapRect(RectF rect) 

Source Link

Document

Apply this matrix to the rectangle, and write the transformed rectangle back into it.

Usage

From source file:Main.java

/**
 * Post rotates the matrix and bounds for the given bounds and degrees.
 *///  w  w  w .  jav  a2s  .com
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

/** Convert coordination of face rect to screen rect that will be draw on canvas<br>
 * *//*  w w w . j a v  a  2 s . c  o  m*/
public static Rect convertFaceRect(Rect faceRect, boolean frontCamera, float displayOrientation,
        float viewWidth, float viewHeight) {

    RectF tmp = new RectF(faceRect);
    Matrix matrix = createConvertMatrix(frontCamera, displayOrientation, viewWidth, viewHeight);
    matrix.mapRect(tmp);
    return new Rect((int) tmp.left, (int) tmp.top, (int) tmp.right, (int) tmp.bottom);
}

From source file:Main.java

static void offsetDescendantRect(ViewGroup group, View child, Rect rect) {
    Matrix m = sMatrix.get();
    if (m == null) {
        m = new Matrix();
        sMatrix.set(m);//  w  w  w . j  a v a  2  s  . com
    } 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

public static void offsetDescendantRect(ViewGroup group, View child, Rect rect) {
    Matrix m = sMatrix.get();
    if (m == null) {
        m = new Matrix();
        sMatrix.set(m);//from w  w  w  .j  a v  a2  s  .c  o  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

public static void offsetDescendantRect(ViewGroup group, View child, Rect rect) {
    Matrix m = sMatrix.get();
    if (m == null) {
        m = new Matrix();
        sMatrix.set(m);/*w w w  .ja  va 2 s  .  c om*/
    } else {
        m.reset();
    }

    offsetDescendantMatrix(group, child, m);

    RectF rectF = sRectF.get();
    if (rectF == null) {
        rectF = new RectF();
        sRectF.set(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

/**
 * Creates a mutable bitmap from subset of source bitmap, transformed by the optional matrix.
 *//*from  w ww .j  a  v  a 2s.  co m*/
private static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m) {
    // Re-implement Bitmap createBitmap() to always return a mutable bitmap.
    Canvas canvas = new Canvas();

    Bitmap bitmap;
    Paint paint;
    if ((m == null) || m.isIdentity()) {
        bitmap = Bitmap.createBitmap(width, height, source.getConfig());
        paint = null;
    } else {
        RectF rect = new RectF(0, 0, width, height);
        m.mapRect(rect);
        bitmap = Bitmap.createBitmap(Math.round(rect.width()), Math.round(rect.height()), source.getConfig());

        canvas.translate(-rect.left, -rect.top);
        canvas.concat(m);

        paint = new Paint(Paint.FILTER_BITMAP_FLAG);
        if (!m.rectStaysRect()) {
            paint.setAntiAlias(true);
        }
    }
    bitmap.setDensity(source.getDensity());
    canvas.setBitmap(bitmap);

    Rect srcBounds = new Rect(x, y, x + width, y + height);
    RectF dstBounds = new RectF(0, 0, width, height);
    canvas.drawBitmap(source, srcBounds, dstBounds, paint);
    return bitmap;
}

From source file:com.iped.ipcam.bitmapfun.ImageDetailFragment.java

protected void center(boolean horizontal, boolean vertical) {

    Matrix m = new Matrix();
    m.set(matrix);/*from   w  w w.  j a va2s. c  o m*/
    RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
    m.mapRect(rect);
    float height = rect.height();
    float width = rect.width();
    float deltaX = 0, deltaY = 0;

    if (vertical) {
        int screenHeight = dm.heightPixels;
        if (height <= screenHeight) {
            deltaY = (screenHeight - height) / 2 - rect.top;
        } else if (rect.top > 0) {
            deltaY = -rect.top;
        } else if (rect.bottom < screenHeight) {
            deltaY = screenHeight - rect.bottom;
        }
    }

    if (horizontal) {
        int screenWidth = dm.widthPixels;
        if (width <= screenWidth) {
            deltaX = (screenWidth - width) / 2 - rect.left;
        } else if (rect.left > 0) {
            deltaX = -rect.left;
        } else if (rect.right < screenWidth) {
            deltaX = screenWidth - rect.right;
        }
    }
    matrix.postTranslate(deltaX, deltaY);
}

From source file:com.apptentive.android.sdk.util.image.PreviewImageView.java

/**
 * Get image boundary from matrix/*from  w ww.  ja  va 2 s .c  o m*/
 *
 * @return
 */
private RectF getMatrixRectF() {
    Matrix matrix = scaleMatrix;
    RectF rect = new RectF();
    Drawable d = getDrawable();
    if (null != d) {
        rect.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        matrix.mapRect(rect);
    }
    return rect;
}

From source file:com.example.android.animationsdemo.CameraActivity.java

/**
 * Convert touch position x:y to {@link android.hardware.Camera.Area} position -1000:-1000 to 1000:1000.
 *///from www. j a v  a 2s . c o  m
private Rect calculateTapArea(float x, float y) {

    // define focus area (36dp)
    Display display = ((WindowManager) getSystemService(Activity.WINDOW_SERVICE)).getDefaultDisplay();
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    int areaSize = Float.valueOf(36 * metrics.density).intValue();
    DKLog.d(TAG, String.format("x=%f, y=%f, areaSize=%d", x, y, areaSize));

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

    // Convert touch area to new area -1000:-1000 to 1000:1000.
    float left = ((x - areaSize) / width) * 2000 - 1000;
    float top = ((y - areaSize) / height) * 2000 - 1000;
    float right = ((x + areaSize) / width) * 2000 - 1000;
    float bottom = ((y + areaSize) / height) * 2000 - 1000;

    // adjust boundary
    if (left < -1000) {
        right += ((-1000) - left);
        left = (-1000);
    }
    if (top < -1000) {
        bottom += ((-1000) - top);
        top = (-1000);
    }
    if (right > 1000) {
        left -= (right - 1000);
        right = 1000;
    }
    if (bottom > 1000) {
        top -= (bottom - 1000);
        bottom = 1000;
    }

    // rotate matrix if portrait
    RectF rectF = new RectF(left, top, right, bottom);
    Matrix matrix = new Matrix();
    int degree = (display.getRotation() == Surface.ROTATION_0) ? (-90) : (0);
    matrix.setRotate(degree);
    matrix.mapRect(rectF);
    return new Rect(Math.round(rectF.left), Math.round(rectF.top), Math.round(rectF.right),
            Math.round(rectF.bottom));
}

From source file:org.uoyabause.android.PadButton.java

public void updateRect(Matrix matrix, int x1, int y1, int x2, int y2) {
    rect.set(x1, y1, x2, y2);//from   w ww .ja  v a2 s  .c  om
    matrix.mapRect(rect);
}