Code example for RectF

Methods: height, inset, offset, sort, width

0
	 */ 
	private void adjustCropRect( RectF r ) {
		// Log.i( LOG_TAG, "adjustCropRect: " + r + ", mImageRect: " + mImageRect ); 
 
		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 ) );
		} 
 
		double diffx = -1, diffy = -1;
 
		if ( r.width() > mImageRect.width() ) {
 
			if ( r.left < mImageRect.left ) {
				diffx = mImageRect.left - r.left;
				r.left += diffx;
			} else if ( r.right > mImageRect.right ) {
				diffx = ( r.right - mImageRect.right );
				r.right += -diffx;
			} 
 
		} else if ( r.height() > mImageRect.height() ) {
			if ( r.top < mImageRect.top ) {
				// top 
				diffy = mImageRect.top - r.top;
				r.top += diffy;
 
			} else if ( r.bottom > mImageRect.bottom ) {
				// bottom 
				diffy = ( r.bottom - mImageRect.bottom );
				r.bottom += -diffy;
			} 
		} 
 
		if ( mMaintainAspectRatio ) {
			// Log.d( LOG_TAG, "diffx: " + diffx + ", diffy: " + diffy ); 
			if ( diffy != -1 ) {
				diffx = diffy * mInitialAspectRatio;
				r.inset( (float) ( diffx / 2.0 ), 0 );
			} else if ( diffx != -1 ) {
				diffy = diffx / mInitialAspectRatio;
				r.inset( 0, (float) ( diffy / 2.0 ) );
			} 
		} 
 
		r.sort();
	} 
 
	/** 
	 * Adjust real crop rect. 
	 *  
	 * @param matrix 
	 *           the matrix 
	 * @param rect 
	 *           the rect 
	 * @param outsideRect 
	 *           the outside rect 
	 * @return the rect f 
	 */ 
	private RectF adjustRealCropRect( Matrix matrix, RectF rect, RectF outsideRect ) {
		// Log.i( LOG_TAG, "adjustRealCropRect" ); 
 
		boolean adjusted = false;
 
		tempLayoutRectF.set( rect );
		matrix.mapRect( tempLayoutRectF );
 
		float[] mvalues = new float[9];
		matrix.getValues( mvalues );
		final float scale = mvalues[Matrix.MSCALE_X];
 
		if ( tempLayoutRectF.left < outsideRect.left ) {
			adjusted = true;
			rect.offset( ( outsideRect.left - tempLayoutRectF.left ) / scale, 0 );
		} else if ( tempLayoutRectF.right > outsideRect.right ) {
			adjusted = true;
			rect.offset( -( tempLayoutRectF.right - outsideRect.right ) / scale, 0 );
		} 
 
		if ( tempLayoutRectF.top < outsideRect.top ) {
			adjusted = true;
			rect.offset( 0, ( outsideRect.top - tempLayoutRectF.top ) / scale );
		} else if ( tempLayoutRectF.bottom > outsideRect.bottom ) {
			adjusted = true;
			rect.offset( 0, -( tempLayoutRectF.bottom - outsideRect.bottom ) / scale );
		} 
 
		tempLayoutRectF.set( rect );
		matrix.mapRect( tempLayoutRectF );
 
		if ( tempLayoutRectF.width() > outsideRect.width() ) {
			adjusted = true;
			if ( tempLayoutRectF.left < outsideRect.left ) rect.left += ( outsideRect.left - tempLayoutRectF.left ) / scale;
			if ( tempLayoutRectF.right > outsideRect.right ) rect.right += -( tempLayoutRectF.right - outsideRect.right ) / scale;
		} 
 
		if ( tempLayoutRectF.height() > outsideRect.height() ) {
			adjusted = true;
			if ( tempLayoutRectF.top < outsideRect.top ) rect.top += ( outsideRect.top - tempLayoutRectF.top ) / scale;
			if ( tempLayoutRectF.bottom > outsideRect.bottom )
				rect.bottom += -( tempLayoutRectF.bottom - outsideRect.bottom ) / scale;
		} 
 
		if ( mMaintainAspectRatio && adjusted ) {
			if ( mInitialAspectRatio >= 1 ) { // width > height
				final double dy = rect.width() / mInitialAspectRatio;
				rect.bottom = (float) ( rect.top + dy );
			} else { // height >= width 
				final double dx = rect.height() * mInitialAspectRatio;
				rect.right = (float) ( rect.left + dx );
			} 
		} 
 
		rect.sort();
		return rect;
	} 
 
	/** 
	 * Compute and adjust the current crop layout 
	 *  
	 * @param adjust 
	 *           - If true tries to adjust the crop rect 
	 * @param outRect 
	 *           - The result will be stored in this {@link Rect} 
	 */ 
	public void computeLayout( boolean adjust, Rect outRect ) {
		if ( adjust ) {
			adjustCropRect( mCropRect );
			tmpRect2.set( 0, 0, mContext.getWidth(), mContext.getHeight() );
			mCropRect = adjustRealCropRect( mMatrix, mCropRect, tmpRect2 );
		} 
 
		getDisplayRect( mMatrix, mCropRect, outRect );
	} 
 
	public void getDisplayRect( Matrix m, RectF supportRect, Rect outRect ) {
		tmpDisplayRectF.set( supportRect.left, supportRect.top, supportRect.right, supportRect.bottom );
		m.mapRect( tmpDisplayRectF );
		outRect.set( Math.round( tmpDisplayRectF.left ), Math.round( tmpDisplayRectF.top ), Math.round( tmpDisplayRectF.right ),
				Math.round( tmpDisplayRectF.bottom ) );
	} 
 
	/** 
	 * Invalidate. 
	 */ 
	public void invalidate() { 
		if ( !mRunning ) {
			computeLayout( true, mDrawRect );
		} 
	} 
 
	/** true while the view is animating */ 
	protected volatile boolean mRunning = false;
 
	/** animation duration in ms */ 
	protected int animationDurationMs = 300;
 
	/** {@link Easing} used to animate the view */ 
	protected Easing mEasing = new Quad();
 
	/** 
	 * Return true if the view is currently running 
	 *  
	 * @return 
	 */ 
	public boolean isRunning() { 
		return mRunning;
	} 
 
	public void animateTo( Matrix m, Rect imageRect, RectF cropRect, final boolean maintainAspectRatio ) {
 
		if ( !mRunning ) {
			mRunning = true;
			setMode( Mode.None );
 
			mMatrix = new Matrix( m );
			mCropRect = cropRect;
			mImageRect = new RectF( imageRect );
			mMaintainAspectRatio = false;
 
			double ratio = (double) mCropRect.width() / (double) mCropRect.height();
			mInitialAspectRatio = Math.round( ratio * 1000.0 ) / 1000.0;
			// Log.i( LOG_TAG, "aspect ratio: " + mInitialAspectRatio ); 
 
			final Rect oldRect = mDrawRect;
			final Rect newRect = new Rect();