Code example for RectF

Methods: height, inset, width

0
	/** 
	 * Generates scaled logo based on the gesture tracking scale factor. 
	 * @return 
	 */ 
	private RectF generateScaledLogo() {
		RectF scaledRect = new RectF(mLogoRect);
		 
		float deltaWidth = mLogoRect.width() - mLogoRect.width()*mScaleFactor;
		float deltaHeight = mLogoRect.height() - mLogoRect.height()*mScaleFactor;
		 
		scaledRect.inset(deltaWidth, deltaHeight);
		return scaledRect;
	} 
	 
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		 
		if (mCurrLogo != null) {
			canvas.drawBitmap(mCurrLogo, null, generateScaledLogo(), null);
		} 
	} 
	/** 
	 * When it detects a scaling gesture, it will scale. Moves the logo if there is only 
	 * one pointer and it's on the logo. 
	 */ 
	@Override 
	public boolean onTouchEvent(MotionEvent ev) {
	    // Let the ScaleGestureDetector inspect all events. 
	    mScaleDetector.onTouchEvent(ev);
	     
	    if (ev.getPointerCount() == 1) {
		    switch (ev.getAction()) {
			    case MotionEvent.ACTION_DOWN:
			    	if (mPrevTouch == null) {
			    		mPrevTouch = new PointF(ev.getX(), ev.getY());
			    	} else { 
			    		mPrevTouch.set(ev.getX(), ev.getY());	
		    		} 
			    	break; 
				case MotionEvent.ACTION_MOVE:
					if (mPrevTouch == null) {
						mPrevTouch = new PointF(ev.getX(), ev.getY());
					} else { 
						moveLogo(ev.getX() - mPrevTouch.x, ev.getY() - mPrevTouch.y);
						mPrevTouch.set(ev.getX(), ev.getY());
					} 
		    		 
					break; 
				case MotionEvent.ACTION_CANCEL:
					break; 
				case MotionEvent.ACTION_UP:
					break; 
				default: 
					break; 
		    } 
	    } 
	    return true; 
	} 
	 
	 
	public void moveLogo(float deltaX, float deltaY) {
		mLogoRect.offset(deltaX, deltaY);	
		invalidate();
	} 
 
	@Override 
	public boolean onTouch(View v, MotionEvent event) {
		return false; 
	} 
	 
	/** Combines the set logo with the current ad 
	 * and returns the result. If a logo hasn't yet been 
	 * set this function returns null 
	 * @return The ad with the logo embedded. Null if no logo set 
	 */ 
	public Bitmap addLogo() {
		// if a logo hasn't yet been set, return null 
		if (mCurrLogo == null) return null;
		 
 
		 
        Bitmap newAd = Bitmap.createBitmap(mCurrAd.getWidth(), mCurrAd.getHeight(), mCurrAd.getConfig());
        Canvas canvas = new Canvas(newAd);
        canvas.drawBitmap(mCurrAd, new Matrix(), null);
         
         
        // We must scale down the logo now that we aren't displaying it on a scaled Ad. 
        RectF scaledLogo = generateScaledLogo();
        RectF scaledDown = new RectF();
         
        float scaledLeft = ((scaledLogo.left - (this.getPaddingLeft() / 2)) / mRatio) ;
        float scaledTop = ((scaledLogo.top - (this.getPaddingTop() / 2))/ mRatio);
        float scaledRight = scaledLeft + (scaledLogo.width() / mRatio);
        float scaledBottom = scaledTop + (scaledLogo.height() / mRatio);
        scaledDown.set(scaledLeft, scaledTop, scaledRight, scaledBottom);
        canvas.drawBitmap(mCurrLogo, null, scaledDown, null);
 
 
		return newAd;