Example usage for android.graphics RectF height

List of usage examples for android.graphics RectF height

Introduction

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

Prototype

public final float height() 

Source Link

Usage

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

/**
 * Prevent visual artifact when scaling//from  w w  w. j  ava2  s  .c  o  m
 */
private void checkBorderAndCenterWhenScale() {

    RectF rect = getMatrixRectF();
    float deltaX = 0;
    float deltaY = 0;

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

    if (rect.width() >= width) {
        if (rect.left > 0) {
            deltaX = -rect.left;
        }
        if (rect.right < width) {
            deltaX = width - rect.right;
        }
    }
    if (rect.height() >= height) {
        if (rect.top > 0) {
            deltaY = -rect.top;
        }
        if (rect.bottom < height) {
            deltaY = height - rect.bottom;
        }
    }
    // Always center the image when it's smaller than the imageView
    if (rect.width() < width) {
        deltaX = width * 0.5f - rect.right + 0.5f * rect.width();
    }
    if (rect.height() < height) {
        deltaY = height * 0.5f - rect.bottom + 0.5f * rect.height();
    }

    scaleMatrix.postTranslate(deltaX, deltaY);

}

From source file:com.example.gatsu.theevent.HighlightView.java

void growBy(float dx, float dy) {

    if (mMaintainAspectRatio) {
        if (dx != 0) {
            dy = dx / mInitialAspectRatio;
        } else if (dy != 0) {
            dx = dy * mInitialAspectRatio;
        }/* www.  j  av a  2 s . co m*/
    }

    // Don't let the cropping rectangle grow too fast.
    // Grow at most half of the difference between the image rectangle and
    // the cropping rectangle.
    RectF r = new RectF(mCropRect);
    if (dx > 0F && r.width() + 2 * dx > mImageRect.width()) {
        float adjustment = (mImageRect.width() - r.width()) / 2F;
        dx = adjustment;
        if (mMaintainAspectRatio) {
            dy = dx / mInitialAspectRatio;
        }
    }
    if (dy > 0F && r.height() + 2 * dy > mImageRect.height()) {
        float adjustment = (mImageRect.height() - r.height()) / 2F;
        dy = adjustment;
        if (mMaintainAspectRatio) {
            dx = dy * mInitialAspectRatio;
        }
    }

    r.inset(-dx, -dy);

    // Don't let the cropping rectangle shrink too fast.
    final float widthCap = 25F;
    if (r.width() < widthCap) {
        r.inset(-(widthCap - r.width()) / 2F, 0F);
    }
    float heightCap = mMaintainAspectRatio ? (widthCap / mInitialAspectRatio) : widthCap;
    if (r.height() < heightCap) {
        r.inset(0F, -(heightCap - r.height()) / 2F);
    }

    // Put the cropping rectangle inside the image rectangle.
    if (r.left < mImageRect.left) {
        r.offset(mImageRect.left - r.left, 0F);
    } else if (r.right > mImageRect.right) {
        r.offset(-(r.right - mImageRect.right), 0);
    }
    if (r.top < mImageRect.top) {
        r.offset(0F, mImageRect.top - r.top);
    } else if (r.bottom > mImageRect.bottom) {
        r.offset(0F, -(r.bottom - mImageRect.bottom));
    }

    mCropRect.set(r);
    mDrawRect = computeLayout();
    mContext.invalidate();
}

From source file:org.caojun.library.cropimage.HighlightView.java

void growBy(float dx, float dy) {

    if (mMaintainAspectRatio) {
        if (dx != 0) {
            dy = dx / mInitialAspectRatio;
        } else if (dy != 0) {
            dx = dy * mInitialAspectRatio;
        }//w  ww.j  a v  a2s . c  om
    }

    // Don't let the cropping rectangle grow too fast.
    // Grow at most half of the difference between the image rectangle and
    // the cropping rectangle.
    RectF r = new RectF(mCropRect);
    if (dx > 0F && r.width() + 2 * dx > mImageRect.width()) {
        float adjustment = (mImageRect.width() - r.width()) / 2F;
        dx = adjustment;
        if (mMaintainAspectRatio) {
            dy = dx / mInitialAspectRatio;
        }
    }
    if (dy > 0F && r.height() + 2 * dy > mImageRect.height()) {
        float adjustment = (mImageRect.height() - r.height()) / 2F;
        dy = adjustment;
        if (mMaintainAspectRatio) {
            dx = dy * mInitialAspectRatio;
        }
    }

    r.inset(-dx, -dy);

    // Don't let the cropping rectangle shrink too fast.
    final float widthCap = 25F;
    if (r.width() < widthCap) {
        r.inset(-(widthCap - r.width()) / 2F, 0F);
    }
    float heightCap = mMaintainAspectRatio ? (widthCap / mInitialAspectRatio) : widthCap;
    if (r.height() < heightCap) {
        r.inset(0F, -(heightCap - r.height()) / 2F);
    }

    // Put the cropping rectangle inside the image rectangle.
    if (r.left < mImageRect.left) {
        r.offset(mImageRect.left - r.left, 0F);
    } else if (r.right > mImageRect.right) {
        r.offset(-(r.right - mImageRect.right), 0);
    }
    if (r.top < mImageRect.top) {
        r.offset(0F, mImageRect.top - r.top);
    } else if (r.bottom > mImageRect.bottom) {
        r.offset(0F, -(r.bottom - mImageRect.bottom));
    }

    mCropRect.set(r);
    mDrawRect = computeLayout();
    mView.invalidate();
}

From source file:com.ferdi2005.secondgram.AndroidUtilities.java

public static void setRectToRect(Matrix matrix, RectF src, RectF dst, int rotation, Matrix.ScaleToFit align) {
    float tx, sx;
    float ty, sy;
    if (rotation == 90 || rotation == 270) {
        sx = dst.height() / src.width();
        sy = dst.width() / src.height();
    } else {/*w ww.j  a  va2  s.  c  o m*/
        sx = dst.width() / src.width();
        sy = dst.height() / src.height();
    }
    if (align != Matrix.ScaleToFit.FILL) {
        if (sx > sy) {
            sx = sy;
        } else {
            sy = sx;
        }
    }
    tx = -src.left * sx;
    ty = -src.top * sy;

    matrix.setTranslate(dst.left, dst.top);
    if (rotation == 90) {
        matrix.preRotate(90);
        matrix.preTranslate(0, -dst.width());
    } else if (rotation == 180) {
        matrix.preRotate(180);
        matrix.preTranslate(-dst.width(), -dst.height());
    } else if (rotation == 270) {
        matrix.preRotate(270);
        matrix.preTranslate(-dst.height(), 0);
    }

    matrix.preScale(sx, sy);
    matrix.preTranslate(tx, ty);
}

From source file:org.stockchart.core.Area.java

public RectF getSideMargins() {
    RectF r = new RectF();

    r.left = getLeftAxis().getSize(0f);/*from w  ww.j a v  a 2s. c  om*/
    r.right = getRightAxis().getSize(0f);
    r.top = getTopAxis().getSize(0f);
    r.bottom = getBottomAxis().getSize(0f);

    if (fLegend.isVisible()) {
        RectF size = fLegend.getSize();

        switch (fLegend.getSide()) {
        case LEFT:
            r.left += size.width();
            break;
        case RIGHT:
            r.right += size.width();
            break;
        case BOTTOM:
            r.bottom += size.height();
            break;
        case TOP:
            r.top += size.height();
            break;

        }
    }

    return r;
}

From source file:com.huyn.demogroup.freechild.FixedViewAttacher.java

private boolean checkMatrixBounds() {
    final RectF rect = getVisibleRect();
    if (rect == null) {
        return false;
    }//from ww w .j a  v  a  2  s  . c om

    RectF src = new RectF();
    src.set(rect);
    final float h = rect.height(), w = rect.width();
    float deltaX = 0, deltaY = 0;
    View parent = (View) mImageView.getParent();

    final int viewHeight = parent.getHeight();//getImageViewHeight(mImageView);
    final int viewWidth = parent.getWidth();//getImageViewWidth(mImageView);
    System.out.println("===============================================width:" + viewWidth + "/" + viewHeight);
    System.out
            .println("+++++++++rect.left:" + rect.left + "/" + rect.top + "/" + rect.right + "/" + rect.bottom);

    float scale = getScale();
    float centerX = rect.centerX();
    float centerY = rect.centerY();
    float targetW = w * scale;
    float targetH = h * scale;
    RectF targetRect = new RectF(centerX - targetW / 2, centerY - targetH / 2, centerX + targetW / 2,
            centerY + targetH / 2);

    rect.set(targetRect);
    final float height = rect.height(), width = rect.width();

    if (height <= viewHeight) {
        deltaY = (viewHeight - height) / 2 - rect.top;
    } else if (rect.top > 0) {
        deltaY = -rect.top;
    } else if (rect.bottom < viewHeight) {
        deltaY = viewHeight - rect.bottom;
    }

    if (width <= viewWidth) {
        deltaX = (viewWidth - width) / 2 - rect.left;
        mScrollEdge = EDGE_BOTH;
    } else if (rect.left > 0) {
        mScrollEdge = EDGE_LEFT;
        deltaX = -rect.left;
    } else if (rect.right < viewWidth) {
        deltaX = viewWidth - rect.right;
        mScrollEdge = EDGE_RIGHT;
    } else {
        mScrollEdge = EDGE_NONE;
    }

    // Finally actually translate the matrix
    if (!mStable) {
        System.out.println("+++++++++++deltaX:" + deltaX + "/deltaY:" + deltaY + "---" + getScale());
        postTranslate(deltaX, deltaY);
    }
    return true;
}

From source file:com.undatech.opaque.RemoteCanvas.java

/**
 * Redraws the location of the remote pointer.
 *///from w ww .  j a  va 2  s  . com
public void reDrawRemotePointer() {
    if (myDrawable != null) {
        myDrawable.moveCursorRect(pointer.getX(), pointer.getY());
        RectF r = myDrawable.getCursorRect();
        reDraw(r.left, r.top, r.width(), r.height());
    }
}

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

@Override
public boolean onTouch(View v, MotionEvent event) {
    gestureDetector.onTouchEvent(event);
    scaleGestureDetector.onTouchEvent(event);

    float x = 0, y = 0;
    // Get multiple touch points
    final int pointerCount = event.getPointerCount();
    // Calculate average x and y
    for (int i = 0; i < pointerCount; i++) {
        x += event.getX(i);//from ww w  .  j  a v a2 s .  co  m
        y += event.getY(i);
    }
    x = x / pointerCount;
    y = y / pointerCount;

    /**
     * Reset lastX and lastY
     */
    if (pointerCount != lastPointerCount) {
        isCanDrag = false;
        lastX = x;
        lastY = y;
    }

    lastPointerCount = pointerCount;

    switch (event.getAction()) {
    case MotionEvent.ACTION_MOVE:
        float dx = x - lastX;
        float dy = y - lastY;

        if (!isCanDrag) {
            isCanDrag = isCanDrag(dx, dy);
        }
        if (isCanDrag) {
            RectF rectF = getMatrixRectF();
            if (getDrawable() != null) {
                isCheckLeftAndRight = isCheckTopAndBottom = true;
                // No left/right translation if image width is less than screen width
                if (rectF.width() < getWidth()) {
                    dx = 0;
                    isCheckLeftAndRight = false;
                }
                // No Up/Down translation if image height is less than screen height
                if (rectF.height() < getHeight()) {
                    dy = 0;
                    isCheckTopAndBottom = false;
                }
                scaleMatrix.postTranslate(dx, dy);
                checkMatrixBounds();
                setImageMatrix(scaleMatrix);
            }
        }
        lastX = x;
        lastY = y;
        break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        lastPointerCount = 0;
        break;
    }
    return true;
}

From source file:com.undatech.opaque.RemoteCanvas.java

/**
 * Moves soft cursor into a particular location.
 * @param x//ww w  . j av  a2s . c o  m
 * @param y
 */

synchronized void softCursorMove(int x, int y) {
    if (myDrawable.isNotInitSoftCursor()) {
        initializeSoftCursor();
    }

    if (!cursorBeingMoved) {
        pointer.setX(x);
        pointer.setY(y);
        RectF prevR = new RectF(myDrawable.getCursorRect());
        // Move the cursor.
        myDrawable.moveCursorRect(x, y);
        // Show the cursor.
        RectF r = myDrawable.getCursorRect();
        reDraw(r.left, r.top, r.width(), r.height());
        reDraw(prevR.left, prevR.top, prevR.width(), prevR.height());
    }
}

From source file:com.example.linhdq.test.documents.creation.crop.CropImageActivity.java

@OnClick(R.id.item_save)
void onSaveClicked() {
    if (!mCropData.isPresent() || mSaving || (mCrop == null)) {
        return;/*from w ww.java 2 s .c o  m*/
    }
    mSaving = true;

    Util.startBackgroundJob(this, null, getText(R.string.cropping_image).toString(), new Runnable() {
        public void run() {
            try {
                float scale = 1f / mCropData.get().getScaleResult().getScaleFactor();
                Matrix scaleMatrix = new Matrix();
                scaleMatrix.setScale(scale, scale);

                final float[] trapezoid = mCrop.getTrapezoid();
                final RectF perspectiveCorrectedBoundingRect = new RectF(
                        mCrop.getPerspectiveCorrectedBoundingRect());
                scaleMatrix.mapRect(perspectiveCorrectedBoundingRect);
                Box bb = new Box((int) perspectiveCorrectedBoundingRect.left,
                        (int) perspectiveCorrectedBoundingRect.top,
                        (int) perspectiveCorrectedBoundingRect.width(),
                        (int) perspectiveCorrectedBoundingRect.height());

                Pix pix8 = Convert.convertTo8(mPix);
                mPix.recycle();

                Pix croppedPix = Clip.clipRectangle2(pix8, bb);
                if (croppedPix == null) {
                    throw new IllegalStateException();
                }
                pix8.recycle();

                scaleMatrix.postTranslate(-bb.getX(), -bb.getY());
                scaleMatrix.mapPoints(trapezoid);

                final float[] dest = new float[] { 0, 0, bb.getWidth(), 0, bb.getWidth(), bb.getHeight(), 0,
                        bb.getHeight() };
                Pix bilinear = Projective.projectiveTransform(croppedPix, dest, trapezoid);
                if (bilinear == null) {
                    bilinear = croppedPix;
                } else {
                    croppedPix.recycle();
                }

                if (mRotation != 0 && mRotation != 4) {
                    Pix rotatedPix = Rotate.rotateOrth(bilinear, mRotation);
                    bilinear.recycle();
                    bilinear = rotatedPix;
                }
                if (bilinear == null) {
                    throw new IllegalStateException();
                }
                Intent result = new Intent();
                OCR.savePixToCacheDir(CropImageActivity.this, bilinear.copy());
                result.putExtra(DocumentGridActivity.EXTRA_NATIVE_PIX, bilinear.getNativePix());
                setResult(RESULT_OK, result);
            } catch (IllegalStateException e) {
                setResult(RESULT_CANCELED);
            } finally {
                finish();
            }
        }
    }, mHandler);

}