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:com.example.linhdq.test.documents.creation.crop.CropImageActivity.java

@OnClick(R.id.item_save)
void onSaveClicked() {
    if (!mCropData.isPresent() || mSaving || (mCrop == null)) {
        return;/*w w  w.jav a  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);

}

From source file:com.mediatek.galleryfeature.stereo.segment.ImageShow.java

protected void constrainTranslation(Point translation, float scale) {
    int currentEdgeEffect = 0;
    if (scale <= 1) {
        finishEdgeEffect();/*from  w ww.jav  a2  s.com*/
        return;
    }
    Rect originalBounds = mMasterImage.getOriginalBounds();
    Matrix originalToScreen = mMasterImage.getImageToScreenMatrix(originalBounds.width(),
            originalBounds.height(), getWidth(), getHeight());
    if (originalToScreen == null) {
        finishEdgeEffect();
        return;
    }
    RectF screenPos = new RectF(originalBounds);
    originalToScreen.mapRect(screenPos);
    boolean rightConstraint = screenPos.right < getWidth();
    boolean leftConstraint = screenPos.left > 0;
    boolean topConstraint = screenPos.top > 0;
    boolean bottomConstraint = screenPos.bottom < getHeight();
    if (screenPos.width() > getWidth()) {
        if (rightConstraint && !leftConstraint) {
            float tx = screenPos.right - translation.x * scale;
            translation.x = (int) ((getWidth() - tx) / scale);
            currentEdgeEffect = EDGE_RIGHT;
        } else if (leftConstraint && !rightConstraint) {
            float tx = screenPos.left - translation.x * scale;
            translation.x = (int) ((-tx) / scale);
            currentEdgeEffect = EDGE_LEFT;
        }
    } else {
        float tx = screenPos.right - translation.x * scale;
        float dx = (getWidth() - screenPos.width()) / 2f;
        translation.x = (int) ((getWidth() - tx - dx) / scale);
    }
    if (screenPos.height() > getHeight()) {
        if (bottomConstraint && !topConstraint) {
            float ty = screenPos.bottom - translation.y * scale;
            translation.y = (int) ((getHeight() - ty) / scale);
            currentEdgeEffect = EDGE_BOTTOM;
        } else if (topConstraint && !bottomConstraint) {
            float ty = screenPos.top - translation.y * scale;
            translation.y = (int) ((-ty) / scale);
            currentEdgeEffect = EDGE_TOP;
        }
    } else {
        float ty = screenPos.bottom - translation.y * scale;
        float dy = (getHeight() - screenPos.height()) / 2f;
        translation.y = (int) ((getHeight() - ty - dy) / scale);
    }
    if (mCurrentEdgeEffect != currentEdgeEffect) {
        if (mCurrentEdgeEffect == 0 || currentEdgeEffect != 0) {
            mCurrentEdgeEffect = currentEdgeEffect;
            mEdgeEffect.finish();
        }
        mEdgeEffect.setSize(getWidth(), mEdgeSize);
    }
    if (currentEdgeEffect != 0) {
        mEdgeEffect.onPull(mEdgeSize);
    }
}

From source file:com.frank.protean.photoview.PhotoViewAttacher.java

/**
 * Helper method that maps the supplied Matrix to the current Drawable
 *
 * @param matrix - Matrix to map Drawable against
 * @return RectF - Displayed Rectangle/*w  ww. j  av  a2s. c o m*/
 */
private RectF getDisplayRect(Matrix matrix) {
    Drawable d = mImageView.getDrawable();
    if (d != null) {
        mDisplayRect.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        matrix.mapRect(mDisplayRect);
        return mDisplayRect;
    }
    return null;
}

From source file:com.guodong.sun.guodong.widget.ZoomImageView.java

/**
 * ???l,r,t,b//  w ww .  j  av  a 2s .  c om
 *
 * @return
 */
private RectF getMatrixRectF() {

    Matrix matrix = mScaleMatrix;
    RectF rectF = new RectF();
    Drawable d = getDrawable();

    if (d != null) {

        rectF.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        matrix.mapRect(rectF);
    }
    return rectF;
}

From source file:com.huyn.demogroup.zoomageview.view.PhotoViewAttacher.java

/**
 * Helper method that maps the supplied Matrix to the current Drawable
 *
 * @param matrix - Matrix to map Drawable against
 * @return RectF - Displayed Rectangle/*from   w  w w .ja v  a2 s  .c o  m*/
 */
private RectF getDisplayRect(Matrix matrix) {
    Drawable d = mImageView.getDrawable();
    if (d != null) {
        mDisplayRect.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        matrix.mapRect(mDisplayRect);
        //System.out.println("++++++mDisplayRect result:" + mDisplayRect.width() + "/" + mDisplayRect.height() + "/" + mDisplayRect.left + "/" + mDisplayRect.top);
        return mDisplayRect;
    }
    return null;
}

From source file:com.android.gallery3d.filtershow.imageshow.ImageShow.java

private void constrainTranslation(Point translation, float scale) {
    int currentEdgeEffect = 0;
    if (scale <= 1) {
        mCurrentEdgeEffect = 0;/*from   w ww .j  a v a2 s . c o m*/
        mEdgeEffect.finish();
        return;
    }

    Matrix originalToScreen = MasterImage.getImage().originalImageToScreen();
    Rect originalBounds = MasterImage.getImage().getOriginalBounds();
    RectF screenPos = new RectF(originalBounds);
    originalToScreen.mapRect(screenPos);

    boolean rightConstraint = screenPos.right < getWidth() - mShadowMargin;
    boolean leftConstraint = screenPos.left > mShadowMargin;
    boolean topConstraint = screenPos.top > mShadowMargin;
    boolean bottomConstraint = screenPos.bottom < getHeight() - mShadowMargin;

    if (screenPos.width() > getWidth()) {
        if (rightConstraint && !leftConstraint) {
            float tx = screenPos.right - translation.x * scale;
            translation.x = (int) ((getWidth() - mShadowMargin - tx) / scale);
            currentEdgeEffect = EDGE_RIGHT;
        } else if (leftConstraint && !rightConstraint) {
            float tx = screenPos.left - translation.x * scale;
            translation.x = (int) ((mShadowMargin - tx) / scale);
            currentEdgeEffect = EDGE_LEFT;
        }
    } else {
        float tx = screenPos.right - translation.x * scale;
        float dx = (getWidth() - 2 * mShadowMargin - screenPos.width()) / 2f;
        translation.x = (int) ((getWidth() - mShadowMargin - tx - dx) / scale);
    }

    if (screenPos.height() > getHeight()) {
        if (bottomConstraint && !topConstraint) {
            float ty = screenPos.bottom - translation.y * scale;
            translation.y = (int) ((getHeight() - mShadowMargin - ty) / scale);
            currentEdgeEffect = EDGE_BOTTOM;
        } else if (topConstraint && !bottomConstraint) {
            float ty = screenPos.top - translation.y * scale;
            translation.y = (int) ((mShadowMargin - ty) / scale);
            currentEdgeEffect = EDGE_TOP;
        }
    } else {
        float ty = screenPos.bottom - translation.y * scale;
        float dy = (getHeight() - 2 * mShadowMargin - screenPos.height()) / 2f;
        translation.y = (int) ((getHeight() - mShadowMargin - ty - dy) / scale);
    }

    if (mCurrentEdgeEffect != currentEdgeEffect) {
        if (mCurrentEdgeEffect == 0 || currentEdgeEffect != 0) {
            mCurrentEdgeEffect = currentEdgeEffect;
            mEdgeEffect.finish();
        }
        mEdgeEffect.setSize(getWidth(), mEdgeSize);
    }
    if (currentEdgeEffect != 0) {
        mEdgeEffect.onPull(mEdgeSize);
    }
}

From source file:com.codegarden.nativenavigation.JuceActivity.java

public final int[] renderGlyph(char glyph, Paint paint, android.graphics.Matrix matrix, Rect bounds) {
    Path p = new Path();
    paint.getTextPath(String.valueOf(glyph), 0, 1, 0.0f, 0.0f, p);

    RectF boundsF = new RectF();
    p.computeBounds(boundsF, true);//from w  w  w .j  a  v a 2 s. co m
    matrix.mapRect(boundsF);

    boundsF.roundOut(bounds);
    bounds.left--;
    bounds.right++;

    final int w = bounds.width();
    final int h = Math.max(1, bounds.height());

    Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

    Canvas c = new Canvas(bm);
    matrix.postTranslate(-bounds.left, -bounds.top);
    c.setMatrix(matrix);
    c.drawPath(p, paint);

    final int sizeNeeded = w * h;
    if (cachedRenderArray.length < sizeNeeded)
        cachedRenderArray = new int[sizeNeeded];

    bm.getPixels(cachedRenderArray, 0, w, 0, 0, w, h);
    bm.recycle();
    return cachedRenderArray;
}

From source file:com.raibow.yamahaspk.filtershow.imageshow.ImageShow.java

public void drawImageAndAnimate(Canvas canvas, Bitmap image) {
    if (image == null) {
        return;/*from   w ww.  j  av  a 2  s. c  o  m*/
    }
    MasterImage master = MasterImage.getImage();
    Matrix m = master.computeImageToScreen(image, 0, false);
    if (m == null) {
        return;
    }

    canvas.save();

    RectF d = new RectF(0, 0, image.getWidth(), image.getHeight());
    m.mapRect(d);
    d.roundOut(mImageBounds);

    boolean showAnimatedImage = master.onGoingNewLookAnimation();
    if (!showAnimatedImage && mDidStartAnimation) {
        // animation ended, but do we have the correct image to show?
        if (master.getPreset().equals(master.getCurrentPreset())) {
            // we do, let's stop showing the animated image
            mDidStartAnimation = false;
            MasterImage.getImage().resetAnimBitmap();
        } else {
            showAnimatedImage = true;
        }
    } else if (showAnimatedImage) {
        mDidStartAnimation = true;
    }

    if (showAnimatedImage) {
        canvas.save();

        // Animation uses the image before the change
        Bitmap previousImage = master.getPreviousImage();
        Matrix mp = master.computeImageToScreen(previousImage, 0, false);
        RectF dp = new RectF(0, 0, previousImage.getWidth(), previousImage.getHeight());
        mp.mapRect(dp);
        Rect previousBounds = new Rect();
        dp.roundOut(previousBounds);
        float centerX = dp.centerX();
        float centerY = dp.centerY();
        boolean needsToDrawImage = true;

        if (master.getCurrentLookAnimation() == MasterImage.CIRCLE_ANIMATION) {
            float maskScale = MasterImage.getImage().getMaskScale();
            if (maskScale >= 0.0f) {
                float maskW = sMask.getWidth() / 2.0f;
                float maskH = sMask.getHeight() / 2.0f;
                Point point = mActivity.hintTouchPoint(this);
                float maxMaskScale = 2 * Math.max(getWidth(), getHeight()) / Math.min(maskW, maskH);
                maskScale = maskScale * maxMaskScale;
                float x = point.x - maskW * maskScale;
                float y = point.y - maskH * maskScale;

                // Prepare the shader
                mShaderMatrix.reset();
                mShaderMatrix.setScale(1.0f / maskScale, 1.0f / maskScale);
                mShaderMatrix.preTranslate(-x + mImageBounds.left, -y + mImageBounds.top);
                float scaleImageX = mImageBounds.width() / (float) image.getWidth();
                float scaleImageY = mImageBounds.height() / (float) image.getHeight();
                mShaderMatrix.preScale(scaleImageX, scaleImageY);
                mMaskPaint.reset();
                mMaskPaint.setShader(createShader(image));
                mMaskPaint.getShader().setLocalMatrix(mShaderMatrix);

                drawShadow(canvas, mImageBounds); // as needed
                canvas.drawBitmap(previousImage, m, mPaint);
                canvas.clipRect(mImageBounds);
                canvas.translate(x, y);
                canvas.scale(maskScale, maskScale);
                canvas.drawBitmap(sMask, 0, 0, mMaskPaint);
                needsToDrawImage = false;
            }
        } else if (master.getCurrentLookAnimation() == MasterImage.ROTATE_ANIMATION) {
            Rect d1 = computeImageBounds(master.getPreviousImage().getHeight(),
                    master.getPreviousImage().getWidth());
            Rect d2 = computeImageBounds(master.getPreviousImage().getWidth(),
                    master.getPreviousImage().getHeight());
            float finalScale = d1.width() / (float) d2.height();
            finalScale = (1.0f * (1.0f - master.getAnimFraction())) + (finalScale * master.getAnimFraction());
            canvas.rotate(master.getAnimRotationValue(), centerX, centerY);
            canvas.scale(finalScale, finalScale, centerX, centerY);
        } else if (master.getCurrentLookAnimation() == MasterImage.MIRROR_ANIMATION) {
            if (master.getCurrentFilterRepresentation() instanceof FilterMirrorRepresentation) {
                FilterMirrorRepresentation rep = (FilterMirrorRepresentation) master
                        .getCurrentFilterRepresentation();

                ImagePreset preset = master.getPreset();
                ArrayList<FilterRepresentation> geometry = (ArrayList<FilterRepresentation>) preset
                        .getGeometryFilters();
                GeometryMathUtils.GeometryHolder holder = null;
                holder = GeometryMathUtils.unpackGeometry(geometry);

                if (holder.rotation.value() == 90 || holder.rotation.value() == 270) {
                    if (rep.isHorizontal() && !rep.isVertical()) {
                        canvas.scale(1, master.getAnimRotationValue(), centerX, centerY);
                    } else if (rep.isVertical() && !rep.isHorizontal()) {
                        canvas.scale(1, master.getAnimRotationValue(), centerX, centerY);
                    } else if (rep.isHorizontal() && rep.isVertical()) {
                        canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY);
                    } else {
                        canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY);
                    }
                } else {
                    if (rep.isHorizontal() && !rep.isVertical()) {
                        canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY);
                    } else if (rep.isVertical() && !rep.isHorizontal()) {
                        canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY);
                    } else if (rep.isHorizontal() && rep.isVertical()) {
                        canvas.scale(1, master.getAnimRotationValue(), centerX, centerY);
                    } else {
                        canvas.scale(1, master.getAnimRotationValue(), centerX, centerY);
                    }
                }
            }
        }

        if (needsToDrawImage) {
            drawShadow(canvas, previousBounds); // as needed
            canvas.drawBitmap(previousImage, mp, mPaint);
        }

        canvas.restore();
    } else {
        drawShadow(canvas, mImageBounds); // as needed
        canvas.drawBitmap(image, m, mPaint);
    }

    canvas.restore();
}

From source file:com.android.gallery3d.filtershow.imageshow.ImageShow.java

public void drawImageAndAnimate(Canvas canvas, Bitmap image) {
    if (image == null) {
        return;// w w  w  .  j  a v a2s .com
    }
    MasterImage master = MasterImage.getImage();
    Matrix m = master.computeImageToScreen(image, 0, false);
    if (m == null) {
        return;
    }

    canvas.save();

    RectF d = new RectF(0, 0, image.getWidth(), image.getHeight());
    m.mapRect(d);
    d.roundOut(mImageBounds);

    boolean showAnimatedImage = master.onGoingNewLookAnimation();
    if (!showAnimatedImage && mDidStartAnimation) {
        // animation ended, but do we have the correct image to show?
        if (master.getPreset().equals(master.getCurrentPreset())) {
            // we do, let's stop showing the animated image
            mDidStartAnimation = false;
            MasterImage.getImage().resetAnimBitmap();
        } else {
            showAnimatedImage = true;
        }
    } else if (showAnimatedImage) {
        mDidStartAnimation = true;
    }

    if (showAnimatedImage) {
        canvas.save();

        // Animation uses the image before the change
        Bitmap previousImage = master.getPreviousImage();
        Matrix mp = master.computeImageToScreen(previousImage, 0, false);
        RectF dp = new RectF(0, 0, previousImage.getWidth(), previousImage.getHeight());
        mp.mapRect(dp);
        Rect previousBounds = new Rect();
        dp.roundOut(previousBounds);
        float centerX = dp.centerX();
        float centerY = dp.centerY();
        boolean needsToDrawImage = true;

        if (master.getCurrentLookAnimation() == MasterImage.CIRCLE_ANIMATION) {
            float maskScale = MasterImage.getImage().getMaskScale();
            if (maskScale >= 0.0f) {
                float maskW = sMask.getWidth() / 2.0f;
                float maskH = sMask.getHeight() / 2.0f;
                Point point = mActivity.hintTouchPoint(this);
                float maxMaskScale = 2 * Math.max(getWidth(), getHeight()) / Math.min(maskW, maskH);
                maskScale = maskScale * maxMaskScale;
                float x = point.x - maskW * maskScale;
                float y = point.y - maskH * maskScale;

                // Prepare the shader
                mShaderMatrix.reset();
                mShaderMatrix.setScale(1.0f / maskScale, 1.0f / maskScale);
                mShaderMatrix.preTranslate(-x + mImageBounds.left, -y + mImageBounds.top);
                float scaleImageX = mImageBounds.width() / (float) image.getWidth();
                float scaleImageY = mImageBounds.height() / (float) image.getHeight();
                mShaderMatrix.preScale(scaleImageX, scaleImageY);
                mMaskPaint.reset();
                Shader maskShader = createShader(image);
                maskShader.setLocalMatrix(mShaderMatrix);
                mMaskPaint.setShader(maskShader);

                drawShadow(canvas, mImageBounds); // as needed
                canvas.drawBitmap(previousImage, m, mPaint);
                canvas.clipRect(mImageBounds);
                canvas.translate(x, y);
                canvas.scale(maskScale, maskScale);
                canvas.drawBitmap(sMask, 0, 0, mMaskPaint);
                needsToDrawImage = false;
            }
        } else if (master.getCurrentLookAnimation() == MasterImage.ROTATE_ANIMATION) {
            Rect d1 = computeImageBounds(master.getPreviousImage().getHeight(),
                    master.getPreviousImage().getWidth());
            Rect d2 = computeImageBounds(master.getPreviousImage().getWidth(),
                    master.getPreviousImage().getHeight());
            float finalScale = d1.width() / (float) d2.height();
            finalScale = (1.0f * (1.0f - master.getAnimFraction())) + (finalScale * master.getAnimFraction());
            canvas.rotate(master.getAnimRotationValue(), centerX, centerY);
            canvas.scale(finalScale, finalScale, centerX, centerY);
        } else if (master.getCurrentLookAnimation() == MasterImage.MIRROR_ANIMATION) {
            if (master.getCurrentFilterRepresentation() instanceof FilterMirrorRepresentation) {
                FilterMirrorRepresentation rep = (FilterMirrorRepresentation) master
                        .getCurrentFilterRepresentation();

                ImagePreset preset = master.getPreset();
                ArrayList<FilterRepresentation> geometry = (ArrayList<FilterRepresentation>) preset
                        .getGeometryFilters();
                GeometryMathUtils.GeometryHolder holder = null;
                holder = GeometryMathUtils.unpackGeometry(geometry);

                if (holder.rotation.value() == 90 || holder.rotation.value() == 270) {
                    if (rep.isHorizontal() && !rep.isVertical()) {
                        canvas.scale(1, master.getAnimRotationValue(), centerX, centerY);
                    } else if (rep.isVertical() && !rep.isHorizontal()) {
                        canvas.scale(1, master.getAnimRotationValue(), centerX, centerY);
                    } else if (rep.isHorizontal() && rep.isVertical()) {
                        canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY);
                    } else {
                        canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY);
                    }
                } else {
                    if (rep.isHorizontal() && !rep.isVertical()) {
                        canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY);
                    } else if (rep.isVertical() && !rep.isHorizontal()) {
                        canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY);
                    } else if (rep.isHorizontal() && rep.isVertical()) {
                        canvas.scale(1, master.getAnimRotationValue(), centerX, centerY);
                    } else {
                        canvas.scale(1, master.getAnimRotationValue(), centerX, centerY);
                    }
                }
            }
        }

        if (needsToDrawImage) {
            drawShadow(canvas, previousBounds); // as needed
            canvas.drawBitmap(previousImage, mp, mPaint);
        }

        canvas.restore();
    } else {
        drawShadow(canvas, mImageBounds); // as needed
        canvas.drawBitmap(image, m, mPaint);
    }

    canvas.restore();
}

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

/**
 * Helper method that maps the supplied Matrix to the current Drawable
 *
 * @param matrix - Matrix to map Drawable against
 * @return RectF - Displayed Rectangle/*  ww  w  . j ava2 s  . c o  m*/
 */
private RectF getDisplayRect(Matrix matrix) {
    ImageView imageView = getImageView();

    if (null != imageView) {
        mDisplayRect.set(0, 0, originalBitmapSize.x, originalBitmapSize.y);
        matrix.mapRect(mDisplayRect);
        return mDisplayRect;
    }
    return null;
}