Java Code Examples for android.graphics.Matrix.mapRect()

The following are Jave code examples for showing how to use mapRect() of the android.graphics.Matrix class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: PicShow-zhaipin   File: PhotoViewAttacher.java   Source Code and License Vote up 6 votes
/**
 * Helper method that maps the supplied Matrix to the current Drawable
 *
 * @param matrix - Matrix to map Drawable against
 * @return RectF - Displayed Rectangle
 */
private RectF getDisplayRect(Matrix matrix) {
    ImageView imageView = getImageView();

    if (null != imageView) {
        Drawable d = imageView.getDrawable();
        if (null != d) {
            mDisplayRect.set(0, 0, d.getIntrinsicWidth(),
                    d.getIntrinsicHeight());
            matrix.mapRect(mDisplayRect);
            return mDisplayRect;
        }
    }
    return null;
}
 
Example 2
Project: MontageCam   File: CropFragment.java   Source Code and License Vote up 6 votes
@SuppressWarnings("WrongThread")
   @Override
   protected Bitmap doInBackground(Bitmap... params) {
       RectF cropRect = mCropPanel.getCropRect();// 剪切区域矩形
       Matrix touchMatrix = activity.mainImage.getImageViewMatrix();
       // Canvas canvas = new Canvas(resultBit);
       float[] data = new float[9];
       touchMatrix.getValues(data);// 底部图片变化记录矩阵原始数据
       Matrix3 cal = new Matrix3(data);// 辅助矩阵计算类
       Matrix3 inverseMatrix = cal.inverseMatrix();// 计算逆矩阵
       Matrix m = new Matrix();
       m.setValues(inverseMatrix.getValues());
       m.mapRect(cropRect);// 变化剪切矩形

       // Paint paint = new Paint();
       // paint.setColor(Color.RED);
       // paint.setStrokeWidth(10);
       // canvas.drawRect(cropRect, paint);
       // Bitmap resultBit = Bitmap.createBitmap(params[0]).copy(
       // Bitmap.Config.ARGB_8888, true);
       Bitmap resultBit = Bitmap.createBitmap(params[0], (int) cropRect.left, (int) cropRect
.top, (int) cropRect.width(), (int) cropRect.height());

       //saveBitmap(resultBit, activity.saveFilePath);
       return resultBit;
   }
 
Example 3
Project: CommentGallery   File: DefaultZoomableController.java   Source Code and License Vote up 6 votes
/**
 * Limits the translation so that there are no empty spaces on the sides if possible.
 * <p>
 * <p> The image is attempted to be centered within the view bounds if the transformed image is
 * smaller. There will be no empty spaces within the view bounds if the transformed image is
 * bigger. This applies to each dimension (horizontal and vertical) independently.
 *
 * @param limitTypes whether to limit translation along the specific axis.
 * @return whether limiting has been applied or not
 */
private boolean limitTranslation(Matrix transform, @LimitFlag int limitTypes) {
    if (!shouldLimit(limitTypes, LIMIT_TRANSLATION_X | LIMIT_TRANSLATION_Y)) {
        return false;
    }
    RectF b = mTempRect;
    b.set(mImageBounds);
    transform.mapRect(b);
    float offsetLeft = shouldLimit(limitTypes, LIMIT_TRANSLATION_X) ?
            getOffset(b.left, b.right, mViewBounds.left, mViewBounds.right, mImageBounds.centerX()) : 0;
    float offsetTop = shouldLimit(limitTypes, LIMIT_TRANSLATION_Y) ?
            getOffset(b.top, b.bottom, mViewBounds.top, mViewBounds.bottom, mImageBounds.centerY()) : 0;
    if (offsetLeft != 0 || offsetTop != 0) {
        transform.postTranslate(offsetLeft, offsetTop);
        return true;
    }
    return false;
}
 
Example 4
Project: GitHub   File: DefaultZoomableController.java   Source Code and License Vote up 6 votes
/**
 * Limits the translation so that there are no empty spaces on the sides if possible.
 *
 * <p> The image is attempted to be centered within the view bounds if the transformed image is
 * smaller. There will be no empty spaces within the view bounds if the transformed image is
 * bigger. This applies to each dimension (horizontal and vertical) independently.
 *
 * @param limitTypes whether to limit translation along the specific axis.
 * @return whether limiting has been applied or not
 */
private boolean limitTranslation(Matrix transform, @LimitFlag int limitTypes) {
  if (!shouldLimit(limitTypes, LIMIT_TRANSLATION_X | LIMIT_TRANSLATION_Y)) {
    return false;
  }
  RectF b = mTempRect;
  b.set(mImageBounds);
  transform.mapRect(b);
  float offsetLeft = shouldLimit(limitTypes, LIMIT_TRANSLATION_X) ?
      getOffset(b.left, b.right, mViewBounds.left, mViewBounds.right, mImageBounds.centerX()) : 0;
  float offsetTop = shouldLimit(limitTypes, LIMIT_TRANSLATION_Y) ?
      getOffset(b.top, b.bottom, mViewBounds.top, mViewBounds.bottom, mImageBounds.centerY()) : 0;
  if (offsetLeft != 0 || offsetTop != 0) {
    transform.postTranslate(offsetLeft, offsetTop);
    return true;
  }
  return false;
}
 
Example 5
Project: KTalk   File: PhotoViewAttacher.java   Source Code and License Vote up 6 votes
/**
 * Helper method that maps the supplied Matrix to the current Drawable
 * 
 * @param matrix
 *            - Matrix to map Drawable against
 * @return RectF - Displayed Rectangle
 */
private RectF getDisplayRect(Matrix matrix) {
	ImageView imageView = getImageView();

	if (null != imageView) {
		Drawable d = imageView.getDrawable();
		if (null != d) {
			mDisplayRect.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
			matrix.mapRect(mDisplayRect);
			return mDisplayRect;
		}
	}
	return null;
}
 
Example 6
Project: Zoomable   File: DefaultZoomableController.java   Source Code and License Vote up 6 votes
/**
 * Limits the translation so that there are no empty spaces on the sides if possible.
 *
 * <p> The image is attempted to be centered within the view bounds if the transformed image is
 * smaller. There will be no empty spaces within the view bounds if the transformed image is
 * bigger. This applies to each dimension (horizontal and vertical) independently.
 *
 * @param limitTypes whether to limit translation along the specific axis.
 * @return whether limiting has been applied or not
 */
private boolean limitTranslation(Matrix transform, @LimitFlag int limitTypes) {
  if (!shouldLimit(limitTypes, LIMIT_TRANSLATION_X | LIMIT_TRANSLATION_Y)) {
    return false;
  }
  RectF b = mTempRect;
  b.set(mImageBounds);
  transform.mapRect(b);
  float offsetLeft = shouldLimit(limitTypes, LIMIT_TRANSLATION_X) ?
      getOffset(b.left, b.right, mViewBounds.left, mViewBounds.right, mImageBounds.centerX()) : 0;
  float offsetTop = shouldLimit(limitTypes, LIMIT_TRANSLATION_Y) ?
      getOffset(b.top, b.bottom, mViewBounds.top, mViewBounds.bottom, mImageBounds.centerY()) : 0;
  if (offsetLeft != 0 || offsetTop != 0) {
    transform.postTranslate(offsetLeft, offsetTop);
    return true;
  }
  return false;
}
 
Example 7
Project: Tribe   File: PhotoViewAttacher.java   Source Code and License Vote up 6 votes
/**
 * Helper method that maps the supplied Matrix to the current Drawable
 * 
 * @param matrix
 *            - Matrix to map Drawable against
 * @return RectF - Displayed Rectangle
 */
private RectF getDisplayRect(Matrix matrix) {
	ImageView imageView = getImageView();

	if (null != imageView) {
		Drawable d = imageView.getDrawable();
		if (null != d) {
			mDisplayRect.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
			matrix.mapRect(mDisplayRect);
			return mDisplayRect;
		}
	}
	return null;
}
 
Example 8
Project: ucar-weex-core   File: ImageDrawable.java   Source Code and License Vote up 6 votes
private static void updateShaderAndSize(@NonNull ImageView.ScaleType scaleType, int vWidth, int vHeight, ImageDrawable imageDrawable, BitmapShader bitmapShader) {
  Matrix matrix = createShaderMatrix(scaleType, vWidth, vHeight,
                                     imageDrawable.bitmapWidth,
                                     imageDrawable.bitmapHeight);
  int intrinsicWidth = vWidth, intrinsicHeight = vHeight;
  if (scaleType == ImageView.ScaleType.FIT_CENTER) {
    RectF bitmapRect = new RectF(0, 0, imageDrawable.bitmapWidth, imageDrawable.bitmapHeight), contentRect = new RectF();
    matrix.mapRect(contentRect, bitmapRect);
    intrinsicWidth = (int) contentRect.width();
    intrinsicHeight = (int) contentRect.height();
    matrix = createShaderMatrix(scaleType, intrinsicWidth, intrinsicHeight, imageDrawable
        .bitmapWidth, imageDrawable.bitmapHeight);
  }
  imageDrawable.setIntrinsicWidth(intrinsicWidth);
  imageDrawable.setIntrinsicHeight(intrinsicHeight);
  bitmapShader.setLocalMatrix(matrix);
}
 
Example 9
Project: XERUNG   File: ImageViewTouchBase.java   Source Code and License Vote up 6 votes
protected void center() {
    final Bitmap bitmap = bitmapDisplayed.getBitmap();
    if (bitmap == null) {
        return;
    }
    Matrix m = getImageViewMatrix();

    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;

    deltaY = centerVertical(rect, height, deltaY);
    deltaX = centerHorizontal(rect, width, deltaX);

    postTranslate(deltaX, deltaY);
    setImageMatrix(getImageViewMatrix());
}
 
Example 10
Project: FanChat   File: PhotoViewAttacher.java   Source Code and License Vote up 5 votes
/**
 * Helper method that maps the supplied Matrix to the current Drawable
 * 
 * @param matrix
 *            - Matrix to map Drawable against
 * @return RectF - Displayed Rectangle
 */
private RectF getDisplayRect(Matrix matrix) {
	ImageView imageView = getImageView();

	if (null != imageView) {
		Drawable d = imageView.getDrawable();
		if (null != d) {
			mDisplayRect.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
			matrix.mapRect(mDisplayRect);
			return mDisplayRect;
		}
	}
	return null;
}
 
Example 11
Project: AndroidPDF   File: PageTreeNode.java   Source Code and License Vote up 5 votes
private RectF evaluatePageSliceBounds(RectF localPageSliceBounds, PageTreeNode parent) {
    if (parent == null) {
        return localPageSliceBounds;
    }
    final Matrix matrix = new Matrix();
    matrix.postScale(parent.pageSliceBounds.width(), parent.pageSliceBounds.height());
    matrix.postTranslate(parent.pageSliceBounds.left, parent.pageSliceBounds.top);
    final RectF sliceBounds = new RectF();
    matrix.mapRect(sliceBounds, localPageSliceBounds);
    return sliceBounds;
}
 
Example 12
Project: FlickLauncher   File: PagedView.java   Source Code and License Vote up 5 votes
protected void getVisiblePages(int[] range) {
    final int count = getChildCount();
    range[0] = -1;
    range[1] = -1;

    if (count > 0) {
        final int visibleLeft = -getLeft();
        final int visibleRight = visibleLeft + getViewportWidth();
        final Matrix pageShiftMatrix = getPageShiftMatrix();
        int curScreen = 0;

        for (int i = 0; i < count; i++) {
            View currPage = getPageAt(i);

            // Verify if the page bounds are within the visible range.
            sTmpRectF.left = 0;
            sTmpRectF.right = currPage.getMeasuredWidth();
            currPage.getMatrix().mapRect(sTmpRectF);
            sTmpRectF.offset(currPage.getLeft() - getScrollX(), 0);
            pageShiftMatrix.mapRect(sTmpRectF);

            if (sTmpRectF.left > visibleRight || sTmpRectF.right < visibleLeft) {
                if (range[0] == -1) {
                    continue;
                } else {
                    break;
                }
            }
            curScreen = i;
            if (range[0] < 0) {
                range[0] = curScreen;
            }
        }

        range[1] = curScreen;
    } else {
        range[0] = -1;
        range[1] = -1;
    }
}
 
Example 13
Project: filepicker   File: PhotoViewAttacher.java   Source Code and License Vote up 5 votes
/**
 * Helper method that maps the supplied Matrix to the current Drawable
 *
 * @param matrix - Matrix to map Drawable against
 * @return RectF - Displayed Rectangle
 */
private RectF getDisplayRect(Matrix matrix) {
    ImageView imageView = getImageView();

    if (null != imageView) {
        Drawable d = imageView.getDrawable();
        if (null != d) {
            mDisplayRect.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
            matrix.mapRect(mDisplayRect);
            return mDisplayRect;
        }
    }
    return null;
}
 
Example 14
Project: FaceRecognition   File: FaceUtil.java   Source Code and License Vote up 5 votes
private static RectF transform(Rect rect, int width, int height){
    Matrix mMatrix = new Matrix();
    RectF mRect = new RectF();
    boolean isMirror = false;
    //int Id = CameraInterface.getInstance().getCameraId();
    //if(Id == Camera.CameraInfo.CAMERA_FACING_BACK)isMirror = false;
    //else if(Id == Camera.CameraInfo.CAMERA_FACING_FRONT)isMirror = true;
    Util.prepareMatrix(mMatrix, isMirror, 0, width, height);
    mRect.set(rect);
    mMatrix.mapRect(mRect);

    return mRect;
}
 
Example 15
Project: yyox   File: PhotoViewAttacher.java   Source Code and License Vote up 5 votes
/**
 * Helper method that maps the supplied Matrix to the current Drawable
 *
 * @param matrix - Matrix to map Drawable against
 * @return RectF - Displayed Rectangle
 */
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;
}
 
Example 16
Project: GCSApp   File: PhotoViewAttacher.java   Source Code and License Vote up 5 votes
/**
 * Helper method that maps the supplied Matrix to the current Drawable
 * 
 * @param matrix
 *            - Matrix to map Drawable against
 * @return RectF - Displayed Rectangle
 */
private RectF getDisplayRect(Matrix matrix) {
	ImageView imageView = getImageView();

	if (null != imageView) {
		Drawable d = imageView.getDrawable();
		if (null != d) {
			mDisplayRect.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
			matrix.mapRect(mDisplayRect);
			return mDisplayRect;
		}
	}
	return null;
}
 
Example 17
Project: Quran   File: HighlightingImageView.java   Source Code and License Vote up 5 votes
private boolean initOverlayParams(Matrix matrix) {
  if (overlayParams == null || pageBounds == null) {
    return false;
  }

  // Overlay params previously initiated; skip
  if (overlayParams.init) {
    return true;
  }

  int overlayColor = overlayTextColor;
  if (isNightMode) {
    overlayColor = Color.rgb(nightModeTextBrightness,
        nightModeTextBrightness, nightModeTextBrightness);
  }
  overlayParams.paint.setColor(overlayColor);

  // Use font metrics to calculate the maximum possible height of the text
  FontMetrics fm = overlayParams.paint.getFontMetrics();

  final RectF mappedRect = new RectF();
  matrix.mapRect(mappedRect, pageBounds);

  // Calculate where the text's baseline should be
  // (for top text and bottom text)
  // (p.s. parts of the glyphs will be below the baseline such as a
  // 'y' or 'ي')
  overlayParams.topBaseline = -fm.top;
  overlayParams.bottomBaseline = getHeight() - fm.bottom;

  // Calculate the horizontal margins off the edge of screen
  overlayParams.offsetX = Math.min(
      mappedRect.left, getWidth() - mappedRect.right);

  overlayParams.init = true;
  return true;
}
 
Example 18
Project: EasyTodo   File: PinchImageView.java   Source Code and License Vote up 4 votes
/**
 * 双击后放大或者缩小
 *
 * 将图片缩放比例缩放到nextScale指定的值.
 * 但nextScale值不能大于最大缩放值不能小于fit center情况下的缩放值.
 * 将双击的点尽量移动到控件中心.
 *
 * @param x 双击的点
 * @param y 双击的点
 *
 * @see #calculateNextScale(float, float)
 * @see #getMaxScale()
 */
private void doubleTap(float x, float y) {
    if (!isReady()) {
        return;
    }
    //获取第一层变换矩阵
    Matrix innerMatrix = MathUtils.matrixTake();
    getInnerMatrix(innerMatrix);
    //当前总的缩放比例
    float innerScale = MathUtils.getMatrixScale(innerMatrix)[0];
    float outerScale = MathUtils.getMatrixScale(mOuterMatrix)[0];
    float currentScale = innerScale * outerScale;
    //控件大小
    float displayWidth = getWidth();
    float displayHeight = getHeight();
    //最大放大大小
    float maxScale = getMaxScale();
    //接下来要放大的大小
    float nextScale = calculateNextScale(innerScale, outerScale);
    //如果接下来放大大于最大值或者小于fit center值,则取边界
    if (nextScale > maxScale) {
        nextScale = maxScale;
    }
    if (nextScale < innerScale) {
        nextScale = innerScale;
    }
    //开始计算缩放动画的结果矩阵
    Matrix animEnd = MathUtils.matrixTake(mOuterMatrix);
    //计算还需缩放的倍数
    animEnd.postScale(nextScale / currentScale, nextScale / currentScale, x, y);
    //将放大点移动到控件中心
    animEnd.postTranslate(displayWidth / 2f - x, displayHeight / 2f - y);
    //得到放大之后的图片方框
    Matrix testMatrix = MathUtils.matrixTake(innerMatrix);
    testMatrix.postConcat(animEnd);
    RectF testBound = MathUtils.rectFTake(0, 0, getDrawable().getIntrinsicWidth(), getDrawable().getIntrinsicHeight());
    testMatrix.mapRect(testBound);
    //修正位置
    float postX = 0;
    float postY = 0;
    if (testBound.right - testBound.left < displayWidth) {
        postX = displayWidth / 2f - (testBound.right + testBound.left) / 2f;
    } else if (testBound.left > 0) {
        postX = -testBound.left;
    } else if (testBound.right < displayWidth) {
        postX = displayWidth - testBound.right;
    }
    if (testBound.bottom - testBound.top < displayHeight) {
        postY = displayHeight / 2f - (testBound.bottom + testBound.top) / 2f;
    } else if (testBound.top > 0) {
        postY = -testBound.top;
    } else if (testBound.bottom < displayHeight) {
        postY = displayHeight - testBound.bottom;
    }
    //应用修正位置
    animEnd.postTranslate(postX, postY);
    //清理当前可能正在执行的动画
    cancelAllAnimator();
    //启动矩阵动画
    mScaleAnimator = new ScaleAnimator(mOuterMatrix, animEnd);
    mScaleAnimator.start();
    //清理临时变量
    MathUtils.rectFGiven(testBound);
    MathUtils.matrixGiven(testMatrix);
    MathUtils.matrixGiven(animEnd);
    MathUtils.matrixGiven(innerMatrix);
}
 
Example 19
Project: TensorflowAndroidDemo   File: MultiBoxTracker.java   Source Code and License Vote up 4 votes
private void processResults(
    final long timestamp, final List<Recognition> results, final byte[] originalFrame) {
  final List<Pair<Float, Recognition>> rectsToTrack = new LinkedList<Pair<Float, Recognition>>();

  screenRects.clear();
  final Matrix rgbFrameToScreen = new Matrix(getFrameToCanvasMatrix());

  for (final Recognition result : results) {
    if (result.getLocation() == null) {
      continue;
    }
    final RectF detectionFrameRect = new RectF(result.getLocation());

    final RectF detectionScreenRect = new RectF();
    rgbFrameToScreen.mapRect(detectionScreenRect, detectionFrameRect);

    logger.v(
        "Result! Frame: " + result.getLocation() + " mapped to screen:" + detectionScreenRect);

    screenRects.add(new Pair<Float, RectF>(result.getConfidence(), detectionScreenRect));

    if (detectionFrameRect.width() < MIN_SIZE || detectionFrameRect.height() < MIN_SIZE) {
      logger.w("Degenerate rectangle! " + detectionFrameRect);
      continue;
    }

    rectsToTrack.add(new Pair<Float, Recognition>(result.getConfidence(), result));
  }

  if (rectsToTrack.isEmpty()) {
    logger.v("Nothing to track, aborting.");
    return;
  }

  if (objectTracker == null) {
    logger.w("No ObjectTracker, can't track anything!");
    return;
  }

  logger.i("%d rects to track", rectsToTrack.size());
  for (final Pair<Float, Recognition> potential : rectsToTrack) {
    handleDetection(originalFrame, timestamp, potential);
  }
}
 
Example 20
Project: AnimationsDemo   File: MyHighlightView.java   Source Code and License Vote up 4 votes
public RectF getDisplayRect(final Matrix m, final RectF supportRect) {
    final RectF r = new RectF(supportRect);
    m.mapRect(r);
    return r;
}