Code example for Rect

Methods: contains

0
		if ((Math.abs(r.bottom - y) < hysteresis) && horizCheck) {
			retval |= GROW_BOTTOM_EDGE;
		} 
 
		// Not near any edge but inside the rectangle: move. 
		if (retval == GROW_NONE && r.contains((int) x, (int) y)) {
			retval = MOVE;
		} 
 
		return retval;
	} 
 
	public ModifyMode getMode() { 
		return mMode;
	} 
 
	public void invalidate() { 
		mDrawRect = computeLayout();
	} 
 
	public void setMode(ModifyMode mode) {
		if (mode != mMode) {
			mMode = mode;
			mContext.invalidate();
		} 
	} 
 
	public void setup(Matrix m, Rect imageRect, RectF cropRect, boolean maintainAspectRatio) {
		mMatrix = new Matrix(m);
 
		mCropRect = cropRect;
		mImageRect = new RectF(imageRect);
		mMaintainAspectRatio = maintainAspectRatio;
 
		mInitialAspectRatio = mCropRect.width() / mCropRect.height();
		mDrawRect = computeLayout();
 
		mFocusPaint.setColor(mContext.getResources().getColor(R.color.crop_rectangle_area));
 
		mOutlinePaint.setColor(mContext.getResources().getColor(R.color.crop_rectangle_line));
		mOutlinePaint.setStrokeWidth(3F);
		mOutlinePaint.setStyle(Paint.Style.STROKE);
		mOutlinePaint.setAntiAlias(true);
 
		mMode = ModifyMode.None;
		init(); 
	} 
 
	// Grows the cropping rectange by (dx, dy) in image space. 
	void growBy(float dx, float dy) {
		if (mMaintainAspectRatio) {
			if (dx != 0) {
				dy = dx / mInitialAspectRatio;
			} else if (dy != 0) {
				dx = dy * mInitialAspectRatio;
			} 
		} 
 
		// 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();
	} 
 
	// Handles motion (dx, dy) in screen space. 
	// The "edge" parameter specifies which edges the user is dragging. 
	void handleMotion(int edge, float dx, float dy) {
		Rect r = computeLayout();
		if (edge == GROW_NONE) {
			return; 
		} else if (edge == MOVE) {
			// Convert to image space before sending to moveBy(). 
			moveBy(dx * (mCropRect.width() / r.width()), dy * (mCropRect.height() / r.height()));
		} else { 
			if (((GROW_LEFT_EDGE | GROW_RIGHT_EDGE) & edge) == 0) {
				dx = 0;
			} 
 
			if (((GROW_TOP_EDGE | GROW_BOTTOM_EDGE) & edge) == 0) {
				dy = 0;
			} 
 
			// Convert to image space before sending to growBy(). 
			float xDelta = dx * (mCropRect.width() / r.width());
			float yDelta = dy * (mCropRect.height() / r.height());
			growBy((((edge & GROW_LEFT_EDGE) != 0) ? -1 : 1) * xDelta, (((edge & GROW_TOP_EDGE) != 0) ? -1 : 1)
					* yDelta);
		} 
	} 
 
	// Grows the cropping rectange by (dx, dy) in image space. 
	void moveBy(float dx, float dy) {
		Rect invalRect = new Rect(mDrawRect);
 
		mCropRect.offset(dx, dy);
 
		// Put the cropping rectangle inside image rectangle. 
		mCropRect.offset(Math.max(0, mImageRect.left - mCropRect.left), Math.max(0, mImageRect.top - mCropRect.top));
 
		mCropRect.offset(Math.min(0, mImageRect.right - mCropRect.right),
				Math.min(0, mImageRect.bottom - mCropRect.bottom));
 
		mDrawRect = computeLayout();
		invalRect.union(mDrawRect);
		invalRect.inset(-10, -10);
		mContext.invalidate(invalRect);
	} 
 
	protected void draw(Canvas canvas) {
		canvas.save();
 
		mOutlinePath.reset();
		mOutlinePath.addRect(mDrawRect.left, mDrawRect.top, mDrawRect.right, mDrawRect.bottom, Path.Direction.CW);
 
		if (!Platform.isCanvasHardwareAccelerated(canvas)) {
			canvas.clipPath(mOutlinePath, Region.Op.DIFFERENCE);
			canvas.drawPaint(mFocusPaint);
		} 
 
		canvas.restore();
 
		canvas.drawPath(mOutlinePath, mOutlinePaint);
 
		/** 
		 * Draw Handle Drawables 
		 */ 
		int left = mDrawRect.left + 1;
		int right = mDrawRect.right + 1;
		int top = mDrawRect.top + 4;
		int bottom = mDrawRect.bottom + 3;
 
		int widthWidth = mResizeDrawableWidth.getIntrinsicWidth() / 2;
		int widthHeight = mResizeDrawableWidth.getIntrinsicHeight() / 2;
		int heightHeight = mResizeDrawableHeight.getIntrinsicHeight() / 2;
		int heightWidth = mResizeDrawableHeight.getIntrinsicWidth() / 2;
 
		int xMiddle = mDrawRect.left + ((mDrawRect.right - mDrawRect.left) / 2);
		int yMiddle = mDrawRect.top + ((mDrawRect.bottom - mDrawRect.top) / 2);
 
		mResizeDrawableWidth.setBounds(left - widthWidth, yMiddle - widthHeight, left + widthWidth, yMiddle
				+ widthHeight);
		mResizeDrawableWidth.draw(canvas);
 
		mResizeDrawableWidth.setBounds(right - widthWidth, yMiddle - widthHeight, right + widthWidth, yMiddle
				+ widthHeight);
		mResizeDrawableWidth.draw(canvas);
 
		mResizeDrawableHeight.setBounds(xMiddle - heightWidth, top - heightHeight, xMiddle + heightWidth, top
				+ heightHeight);
		mResizeDrawableHeight.draw(canvas);
 
		mResizeDrawableHeight.setBounds(xMiddle - heightWidth, bottom - heightHeight, xMiddle + heightWidth, bottom
				+ heightHeight);
		mResizeDrawableHeight.draw(canvas);
	} 
 
	// Maps the cropping rectangle from image space to screen space. 
	private Rect computeLayout() {
		RectF r = new RectF(mCropRect.left, mCropRect.top, mCropRect.right, mCropRect.bottom);
		mMatrix.mapRect(r);
		return new Rect(Math.round(r.left), Math.round(r.top), Math.round(r.right), Math.round(r.bottom));
	} 
 
	private void init() { 
		android.content.res.Resources resources = mContext.getResources();
		mResizeDrawableWidth = resources.getDrawable(R.drawable.crop_handle_x);