Code example for RectF

Methods: centerX, centerY, height, mapRect, width

0
	 * @param m 
	 *           the m 
	 * @param supportRect 
	 *           the support rect 
	 * @return the display rect 
	 */ 
	protected RectF getDisplayRect( final Matrix m, final RectF supportRect ) {
		final RectF r = new RectF( supportRect );
		m.mapRect( r );
		return r;
	} 
 
	/** 
	 * Gets the display rect f. 
	 *  
	 * @return the display rect f 
	 */ 
	public RectF getDisplayRectF() {
		final RectF r = new RectF( mDrawRect );
		mRotateMatrix.mapRect( r );
		return r;
	} 
 
	/** 
	 * Gets the draw rect. 
	 *  
	 * @return the draw rect 
	 */ 
	public RectF getDrawRect() {
		return mDrawRect;
	} 
 
	/** 
	 * Gets the hit. 
	 *  
	 * @param x 
	 *           the x 
	 * @param y 
	 *           the y 
	 * @return the hit 
	 */ 
	public int getHit( float x, float y ) {
 
		final RectF rect = new RectF( mDrawRect );
		rect.inset( -mPadding, -mPadding );
 
		final float pts[] = new float[] { x, y };
 
		final Matrix rotateMatrix = new Matrix();
		rotateMatrix.postTranslate( -rect.centerX(), -rect.centerY() );
		rotateMatrix.postRotate( -mRotation );
		rotateMatrix.postTranslate( rect.centerX(), rect.centerY() );
		rotateMatrix.mapPoints( pts );
 
		x = pts[0];
		y = pts[1];
 
		mContext.invalidate();
 
		int retval = DrawableHighlightView.GROW_NONE;
		final boolean verticalCheck = ( y >= ( rect.top - DrawableHighlightView.HIT_TOLERANCE ) )
				&& ( y < ( rect.bottom + DrawableHighlightView.HIT_TOLERANCE ) );
		final boolean horizCheck = ( x >= ( rect.left - DrawableHighlightView.HIT_TOLERANCE ) )
				&& ( x < ( rect.right + DrawableHighlightView.HIT_TOLERANCE ) );
		 
		// if horizontal and vertical checks are good then 
		// at least the move edge is selected 
		if( verticalCheck && horizCheck ){
			retval = DrawableHighlightView.MOVE;
		} 
 
		if ( !mRotateAndScale ) {
			if ( ( Math.abs( rect.left - x ) < DrawableHighlightView.HIT_TOLERANCE ) && verticalCheck )
				retval |= DrawableHighlightView.GROW_LEFT_EDGE;
			if ( ( Math.abs( rect.right - x ) < DrawableHighlightView.HIT_TOLERANCE ) && verticalCheck )
				retval |= DrawableHighlightView.GROW_RIGHT_EDGE;
			if ( ( Math.abs( rect.top - y ) < DrawableHighlightView.HIT_TOLERANCE ) && horizCheck )
				retval |= DrawableHighlightView.GROW_TOP_EDGE;
			if ( ( Math.abs( rect.bottom - y ) < DrawableHighlightView.HIT_TOLERANCE ) && horizCheck )
				retval |= DrawableHighlightView.GROW_BOTTOM_EDGE;
		} 
 
		if ( ( Math.abs( rect.right - x ) < DrawableHighlightView.HIT_TOLERANCE )
				&& ( Math.abs( rect.bottom - y ) < DrawableHighlightView.HIT_TOLERANCE ) && verticalCheck && horizCheck )
			retval = DrawableHighlightView.ROTATE;
 
		if ( ( retval == DrawableHighlightView.GROW_NONE ) && rect.contains( (int) x, (int) y ) )
			retval = DrawableHighlightView.MOVE;
		return retval;
	} 
 
	/** 
	 * On single tap confirmed. 
	 *  
	 * @param x 
	 *           the x 
	 * @param y 
	 *           the y 
	 */ 
	public void onSingleTapConfirmed( float x, float y ) {
 
		final RectF rect = new RectF( mDrawRect );
		rect.inset( -mPadding, -mPadding );
 
		final float pts[] = new float[] { x, y };
 
		final Matrix rotateMatrix = new Matrix();
		rotateMatrix.postTranslate( -rect.centerX(), -rect.centerY() );
		rotateMatrix.postRotate( -mRotation );
		rotateMatrix.postTranslate( rect.centerX(), rect.centerY() );
		rotateMatrix.mapPoints( pts );
 
		x = pts[0];
		y = pts[1];
 
		mContext.invalidate();
 
		final boolean verticalCheck = ( y >= ( rect.top - DrawableHighlightView.HIT_TOLERANCE ) )
				&& ( y < ( rect.bottom + DrawableHighlightView.HIT_TOLERANCE ) );
		final boolean horizCheck = ( x >= ( rect.left - DrawableHighlightView.HIT_TOLERANCE ) )
				&& ( x < ( rect.right + DrawableHighlightView.HIT_TOLERANCE ) );
 
		if ( mShowDeleteButton )
			if ( ( Math.abs( rect.left - x ) < DrawableHighlightView.HIT_TOLERANCE )
					&& ( Math.abs( rect.top - y ) < DrawableHighlightView.HIT_TOLERANCE ) && verticalCheck && horizCheck )
				if ( mDeleteClickListener != null ) {
					mDeleteClickListener.onDeleteClick();
				} 
	} 
 
	/** 
	 * Gets the invalidation rect. 
	 *  
	 * @return the invalidation rect 
	 */ 
	protected Rect getInvalidationRect() {
		final RectF r = new RectF( mDrawRect );
		r.inset( -mPadding, -mPadding );
		mRotateMatrix.mapRect( r );
 
		final Rect rect = new Rect( (int) r.left, (int) r.top, (int) r.right, (int) r.bottom );
		rect.inset( -mAnchorWidth * 2, -mAnchorHeight * 2 );
		return rect;
	} 
 
	/** 
	 * Gets the matrix. 
	 *  
	 * @return the matrix 
	 */ 
	public Matrix getMatrix() {
		return mMatrix;
	} 
 
	/** 
	 * Gets the mode. 
	 *  
	 * @return the mode 
	 */ 
	public Mode getMode() { 
		return mMode;
	} 
 
	/** 
	 * Gets the rotation. 
	 *  
	 * @return the rotation 
	 */ 
	public float getRotation() { 
		return mRotation;
	} 
 
	public Matrix getRotationMatrix() {
		return mRotateMatrix;
	} 
 
	/** 
	 * Increase the size of the View. 
	 *  
	 * @param dx 
	 *           the dx 
	 */ 
	protected void growBy( final float dx ) {
		growBy( dx, dx / mRatio, true );
	} 
 
	/** 
	 * Increase the size of the View. 
	 *  
	 * @param dx 
	 *           the dx 
	 * @param dy 
	 *           the dy 
	 * @param checkMinSize 
	 *           the check min size 
	 */ 
	protected void growBy( final float dx, final float dy, boolean checkMinSize ) {
		final RectF r = new RectF( mCropRect );
 
		if ( mAlignVerticalMode == AlignModeV.Center ) {
			r.inset( -dx, -dy );
		} else if ( mAlignVerticalMode == AlignModeV.Top ) {
			r.inset( -dx, 0 );
			r.bottom += dy * 2;
		} else { 
			r.inset( -dx, 0 );
			r.top -= dy * 2;
		} 
 
		RectF testRect = getDisplayRect( mMatrix, r );
		Log.d( LOG_TAG, "growBy: " + testRect.width() + "x" + testRect.height() );
		 
		if ( !mContent.validateSize( testRect ) && checkMinSize ) {
			return; 
		} 
 
		mCropRect.set( r );
		invalidate(); 
		mContext.invalidate();
	} 
 
	/** 
	 * On mouse move. 
	 *  
	 * @param edge 
	 *           the edge 
	 * @param event2 
	 *           the event2 
	 * @param dx 
	 *           the dx 
	 * @param dy 
	 *           the dy 
	 */ 
	void onMouseMove( int edge, MotionEvent event2, float dx, float dy ) {
		if ( edge == GROW_NONE ) {
			return; 
		} 
 
		fpoints[0] = dx;
		fpoints[1] = dy;
 
		float xDelta;
		@SuppressWarnings("unused") 
		float yDelta;
 
		if ( edge == MOVE ) {
			moveBy( dx * ( mCropRect.width() / mDrawRect.width() ), dy * ( mCropRect.height() / mDrawRect.height() ) );
		} else if ( edge == ROTATE ) {
			dx = fpoints[0];
			dy = fpoints[1];
			xDelta = dx * ( mCropRect.width() / mDrawRect.width() );
			yDelta = dy * ( mCropRect.height() / mDrawRect.height() );
			rotateBy( event2.getX(), event2.getY(), dx, dy );
 
			invalidate(); 
			mContext.invalidate( getInvalidationRect() );
		} else { 
 
			Matrix rotateMatrix = new Matrix();
			rotateMatrix.postRotate( -mRotation );
			rotateMatrix.mapPoints( fpoints );
			dx = fpoints[0];
			dy = fpoints[1];
 
			if ( ( ( GROW_LEFT_EDGE | GROW_RIGHT_EDGE ) & edge ) == 0 ) dx = 0;
			if ( ( ( GROW_TOP_EDGE | GROW_BOTTOM_EDGE ) & edge ) == 0 ) dy = 0;
 
			xDelta = dx * ( mCropRect.width() / mDrawRect.width() );
			yDelta = dy * ( mCropRect.height() / mDrawRect.height() );
			growBy( ( ( ( edge & GROW_LEFT_EDGE ) != 0 ) ? -1 : 1 ) * xDelta );
 
			invalidate(); 
			mContext.invalidate( getInvalidationRect() );
		} 
	} 
	 
	void onMove( float dx, float dy ) {
		moveBy( dx * ( mCropRect.width() / mDrawRect.width() ), dy * ( mCropRect.height() / mDrawRect.height() ) );
	} 
	 
	/** 
	 * Inits the. 
	 */ 
	private void init() { 
 
		final android.content.res.Resources resources = mContext.getResources();
		// mAnchorResize = resources.getDrawable( R.drawable.camera_crop_width ); 
		mAnchorRotate = resources.getDrawable( R.drawable.feather_resize_knob );
		mAnchorDelete = resources.getDrawable( R.drawable.feather_highlight_delete_button );
 
		mAnchorWidth = mAnchorRotate.getIntrinsicWidth() / 2;
		mAnchorHeight = mAnchorRotate.getIntrinsicHeight() / 2;
 
		mOutlineStrokeColorNormal = mContext.getResources().getColor( R.color.feather_drawable_highlight_focus );
		mOutlineStrokeColorPressed = mContext.getResources().getColor( R.color.feather_drawable_highlight_down );
		mOutlineStrokeColorUnselected = 0;
 
		mOutlineStrokePaint = new Paint( Paint.ANTI_ALIAS_FLAG );
		mOutlineStrokePaint.setStrokeWidth( 2.0f );
		mOutlineStrokePaint.setStyle( Paint.Style.STROKE );
		mOutlineStrokePaint.setColor( mOutlineStrokeColorNormal );
		 
		mOutlineFillPaint = new Paint( Paint.ANTI_ALIAS_FLAG );
		mOutlineFillPaint.setStyle( Paint.Style.FILL );
		mOutlineFillPaint.setColor( mOutlineFillColorNormal );
 
		setMode( Mode.None );
	} 
 
	/** 
	 * Invalidate. 
	 */ 
	public void invalidate() { 
		mDrawRect = computeLayout(); // true
 
		mRotateMatrix.reset();
		mRotateMatrix.postTranslate( -mDrawRect.centerX(), -mDrawRect.centerY() );
		mRotateMatrix.postRotate( mRotation );
		mRotateMatrix.postTranslate( mDrawRect.centerX(), mDrawRect.centerY() );
	} 
 
	/** 
	 * Move by. 
	 *  
	 * @param dx 
	 *           the dx