Code example for RectF

Methods: height, inset, offset, set, sort, width

0
		double heightCap = mMaintainAspectRatio ? ( widthCap / mInitialAspectRatio ) : widthCap;
		if ( tmpRectMotionF.height() < heightCap ) {
			tmpRectMotionF.inset( 0F, (float) ( -( heightCap - tmpRectMotionF.height() ) / 2F ) );
		} 
 
		mCropRect.set( tmpRectMotionF );
		computeLayout( true, mDrawRect );
		mContext.invalidate();
	} 
 
	/** 
	 * Adjust crop rect based on the current image. 
	 *  
	 * @param r 
	 *           - The {@link Rect} to be adjusted 
	 */ 
	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