Example usage for android.graphics RectF round

List of usage examples for android.graphics RectF round

Introduction

In this page you can find the example usage for android.graphics RectF round.

Prototype

public void round(Rect dst) 

Source Link

Document

Set the dst integer Rect by rounding this rectangle's coordinates to their nearest integer values.

Usage

From source file:Main.java

/**
 * Computes a Camera.Area corresponding to the new focus area to focus the camera on. This is
 * done by deriving a square around the center of a MotionEvent pointer (with side length equal
 * to FOCUS_AREA_MOTION_EVENT_EDGE_LENGTH), then transforming this rectangle's/square's
 * coordinates into the (-1000, 1000) coordinate system used for camera focus areas.
 *
 * Also note that we operate on RectF instances for the most part, to avoid any integer
 * division rounding errors going forward. We only round at the very end for playing into
 * the final focus areas list./*from w ww  .  j a  v a2  s. c  o m*/
 *
 * @throws RuntimeException if unable to compute valid intersection between MotionEvent region
 * and SurfaceTexture region.
 */
protected static Camera.Area computeFocusAreaFromMotionEvent(final MotionEvent event,
        final int surfaceTextureWidth, final int surfaceTextureHeight) {
    // Get position of first touch pointer.
    final int pointerId = event.getPointerId(0);
    final int pointerIndex = event.findPointerIndex(pointerId);
    final float centerX = event.getX(pointerIndex);
    final float centerY = event.getY(pointerIndex);

    // Build event rect. Note that coordinates increase right and down, such that left <= right
    // and top <= bottom.
    final RectF eventRect = new RectF(centerX - FOCUS_AREA_MOTION_EVENT_EDGE_LENGTH, // left
            centerY - FOCUS_AREA_MOTION_EVENT_EDGE_LENGTH, // top
            centerX + FOCUS_AREA_MOTION_EVENT_EDGE_LENGTH, // right
            centerY + FOCUS_AREA_MOTION_EVENT_EDGE_LENGTH // bottom
    );

    // Intersect this rect with the rect corresponding to the full area of the parent surface
    // texture, making sure we are not placing any amount of the eventRect outside the parent
    // surface's area.
    final RectF surfaceTextureRect = new RectF((float) 0, // left
            (float) 0, // top
            (float) surfaceTextureWidth, // right
            (float) surfaceTextureHeight // bottom
    );
    final boolean intersectSuccess = eventRect.intersect(surfaceTextureRect);
    if (!intersectSuccess) {
        throw new RuntimeException("MotionEvent rect does not intersect with SurfaceTexture rect; unable to "
                + "compute focus area");
    }

    // Transform into (-1000, 1000) focus area coordinate system. See
    // https://developer.android.com/reference/android/hardware/Camera.Area.html.
    // Note that if this is ever changed to a Rect instead of RectF, be cautious of integer
    // division rounding!
    final RectF focusAreaRect = new RectF((eventRect.left / surfaceTextureWidth) * 2000 - 1000, // left
            (eventRect.top / surfaceTextureHeight) * 2000 - 1000, // top
            (eventRect.right / surfaceTextureWidth) * 2000 - 1000, // right
            (eventRect.bottom / surfaceTextureHeight) * 2000 - 1000 // bottom
    );
    Rect focusAreaRectRounded = new Rect();
    focusAreaRect.round(focusAreaRectRounded);
    return new Camera.Area(focusAreaRectRounded, FOCUS_AREA_WEIGHT);
}

From source file:uk.co.senab.photoview.PhotoViewAttacher.java

public void renderBitmap() {
    Log.d("RenderBitmap", "Rendering bitmap!!!");
    ImageView iv = getImageView();
    if (iv != null) {
        RectF rect = getDisplayRect();
        if (rect == null || rect.isEmpty()) {
            rect = new RectF(0, 0, originalBitmapSize.x, originalBitmapSize.y);
        }/* w ww .  j a  v  a2  s. co  m*/

        float ratioX = originalBitmapSize.x / (float) iv.getWidth();
        float ratioY = originalBitmapSize.y / (float) iv.getHeight();

        rect.set(rect.left * ratioX, rect.top * ratioY, rect.right * ratioX, rect.bottom * ratioY);
        Rect bounds = new Rect();
        rect.round(bounds);

        Bitmap bitmap = Bitmap.createBitmap(originalBitmapSize.x, originalBitmapSize.y,
                Bitmap.Config.ARGB_8888);
        pdfiumCore.renderPageBitmap(pdfDocument, bitmap, pageIndex, bounds.left, bounds.top, bounds.width(),
                bounds.height());
        iv.setImageBitmap(bitmap);
    }
}

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

private List<Rect> getFaces() {
    List<Rect> found = new ArrayList<Rect>();

    for (Face face : mPreview.faces) {
        if (face.score < 100)
            continue;

        Rect rect = new Rect();
        RectF rectf = new RectF();

        rectf.set(face.rect);/*from   w  w w  .  j  a va2  s.c  o m*/
        mViewFinderView.dumpRect(rectf, "before");
        matrix.mapRect(rectf);
        mViewFinderView.dumpRect(rectf, "after");

        float adj = (rectf.bottom - rectf.top - rectf.right + rectf.left) / 2;
        rectf.set(rectf.left - adj, rectf.top, rectf.right + adj, rectf.bottom);
        mViewFinderView.dumpRect(rectf, "Squared");

        if (rectf.left < 0) {
            rectf.set(0, rectf.top, rectf.right - rectf.left, rectf.bottom);
            Log.d("face Adjust", "Left");
        } else if (rectf.right > pWidth) {
            rectf.set(rectf.left - rectf.right + pWidth, rectf.top, pWidth, rectf.bottom);
            Log.d("face Adjust", "Right");
        }

        if (rectf.top < 0) {
            rectf.set(rectf.left, 0, rectf.right, rectf.bottom - rectf.top);
            Log.d("face Adjust", "Top");
        } else if (rectf.bottom > pHeight) {
            rectf.set(rectf.left, rectf.top - rectf.bottom + pHeight, rectf.right, pHeight);
            Log.d("face Adjust", "Bottom");
        }

        mViewFinderView.dumpRect(rectf, "Shift Adjusted");
        rectf.round(rect);
        found.add(rect);
    }

    return found;
}