Code example for Bitmap

Methods: getHeightisRecycled

0
		return 0; 
	} 
	 
	private int getHeight() { 
		if ( mBackground != null ) {
			return mBackground.getHeight();
		} 
		 
		return 0; 
	} 
	 
	@Override 
	public void draw(Canvas canvas) {			
				 
		// We need to initialize all size data when we first draw the view 
		if ( !bViewDrawn ) {
			bViewDrawn = true;
			onFirstDrawEvent(canvas);
		} 
		 
		canvas.drawColor(backgroundColor);		
		 
		// TODO: This just scales the views to the current 
		// width and height. We should add some logic for: 
		//  1) Maintain aspect ratio 
		//  2) Uniform scale 
		//  3) ... 
		Rect rect = new Rect();
		rect.left = 0;
		rect.top = 0;
		rect.bottom = getHeight();
		rect.right = getWidth();
		 
		// First Page render 
		Paint paint = new Paint();
		 
	 
		// Draw our elements 
		try { 
			drawForeground(canvas, rect, paint);
		} catch (Exception e ) {
			//ErrorReporter.getInstance().handleException(e); 
		} 
		 
		try { 
			drawBackground(canvas, rect, paint);
		} catch (Exception e) {
			//ErrorReporter.getInstance().handleException(e); 
		}		 
		 
		drawCurlEdge(canvas);
		 
		if ( this.drawDebugEnabled  ) {
			drawDebug(canvas);
		} 
		 
	} 
	 
	/** 
	 * Initialize the view 
	 */ 
	private final void init() {		 
		 
		// Foreground text paint 
		mTextPaint = new Paint();
		mTextPaint.setAntiAlias(true);
		mTextPaint.setTextSize(16);
		mTextPaint.setColor(0xFF000000);
		 
		// The shadow 
		mTextPaintShadow = new TextPaint();
		mTextPaintShadow.setAntiAlias(true);
		mTextPaintShadow.setTextSize(16);
		mTextPaintShadow.setColor(0x00000000);
		 
		// Base padding 
		//setPadding(3, 3, 3, 3); 
		 
		mMovement =  new Vector2D(0,0);
		 
		mOldMovement = new Vector2D(0,0);				
		 
		// Create our edge paint 
		mCurlEdgePaint = new Paint();		
		mCurlEdgePaint.setAntiAlias(true);
		mCurlEdgePaint.setStyle(Paint.Style.STROKE);
	//	mCurlEdgePaint.setColor(this.edgeColor); 
		 
	//	mCurlEdgePaint.setShadowLayer(10, -5, 5, 0x99000000); 
		 
		 
		// Set the default props, those come from an XML :D 
		mCurlSpeed = 30;		
		mInitialEdgeOffset = 20;
		 
	} 
	 
	/** 
	 * Reset points to it's initial clip edge state 
	 */ 
	public void ResetClipEdge() 
	{ 
		// Set our base movement 
		mMovement.x = mInitialEdgeOffset;
		mMovement.y = mInitialEdgeOffset;		
		mOldMovement.x = 0;
		mOldMovement.y = 0;		
		 
		if ( ! bFlipRight ) {
			mMovement.x = getWidth();
			mMovement.y = mInitialEdgeOffset;
		} 
		 
		// Now set the points 
		// TODO: OK, those points MUST come from our measures and 
		// the actual bounds of the view! 
		mA = new Vector2D(mInitialEdgeOffset, 0);
		mB = new Vector2D(this.getWidth(), this.getHeight());
		mC = new Vector2D(this.getWidth(), 0);
		mD = new Vector2D(0, 0);
		mE = new Vector2D(0, 0);
		mF = new Vector2D(0, 0);		
				 
		// The movement origin point 
		mOrigin = new Vector2D(this.getWidth(), 0);
	}	 
	 
	/** 
	 * Set the curl speed. 
	 * @param curlSpeed - New speed in px/frame 
	 * @throws IllegalArgumentException if curlspeed < 1 
	 */ 
	public void SetCurlSpeed(int curlSpeed)
	{ 
		if ( curlSpeed < 1 )
			throw new IllegalArgumentException("curlSpeed must be greated than 0");
		mCurlSpeed = curlSpeed;
	} 
	 
	/** 
	 * Get the current curl speed 
	 * @return int - Curl speed in px/frame 
	 */ 
	public int GetCurlSpeed() 
	{ 
		return mCurlSpeed;
	}	 
	 
	/** 
	 * Set the initial pixel offset for the curl edge 
	 * @param initialEdgeOffset - px offset for curl edge 
	 * @throws IllegalArgumentException if initialEdgeOffset < 0 
	 */ 
	public void SetInitialEdgeOffset(int initialEdgeOffset)
	{ 
		if ( initialEdgeOffset < 0 )
			throw new IllegalArgumentException("initialEdgeOffset can not negative");
		mInitialEdgeOffset = initialEdgeOffset;
	} 
	 
	/** 
	 * Get the initial pixel offset for the curl edge 
	 * @return int - px 
	 */ 
	public int GetInitialEdgeOffset() 
	{ 
		return mInitialEdgeOffset;
	} 
	 
	/** 
	 * Make sure we never move too much, and make sure that if we  
	 * move too much to add a displacement so that the movement will  
	 * be still in our radius. 
	 * @param radius - radius form the flip origin 
	 * @param bMaintainMoveDir - Cap movement but do not change the 
	 * current movement direction 
	 * @return Corrected point 
	 */ 
	private Vector2D CapMovement(Vector2D point, boolean bMaintainMoveDir)
	{ 
		// Make sure we never ever move too much 
		if (point.distance(mOrigin) > mFlipRadius)
		{ 
			if ( bMaintainMoveDir )
			{ 
				// Maintain the direction 
				point = mOrigin.sum(point.sub(mOrigin).normalize().mult(mFlipRadius));
			} 
			else 
			{ 
				// Change direction 
				if ( point.x > (mOrigin.x+mFlipRadius))
					point.x = (mOrigin.x+mFlipRadius);
				else if ( point.x < (mOrigin.x-mFlipRadius) )
					point.x = (mOrigin.x-mFlipRadius);
				point.y = (float) (Math.sin(Math.acos(Math.abs(point.x-mOrigin.x)/mFlipRadius))*mFlipRadius);
			} 
		} 
		return point;
	} 
	 
	/** 
	 * Do a simple page curl effect 
	 */ 
	private void doSimpleCurl() { 
		int width = getWidth();
		int height = getHeight();
		 
		// Calculate point A 
		mA.x = width - mMovement.x;
		mA.y = height;
 
		// Calculate point D 
		mD.x = 0;
		mD.y = 0;
		if (mA.x > width / 2) {
			mD.x = width;
			mD.y = height - (width - mA.x) * height / mA.x;
		} else { 
			mD.x = 2 * mA.x;
			mD.y = 0;
		} 
		 
		// Now calculate E and F taking into account that the line 
		// AD is perpendicular to FB and EC. B and C are fixed points. 
		double angle = Math.atan((height - mD.y) / (mD.x + mMovement.x - width));
		double _cos = Math.cos(2 * angle);
		double _sin = Math.sin(2 * angle);
 
		// And get F 
		mF.x = (float) (width - mMovement.x + _cos * mMovement.x);
		mF.y = (float) (height - _sin * mMovement.x);
		 
		// If the x position of A is above half of the page we are still not 
		// folding the upper-right edge and so E and D are equal. 
		if (mA.x > width / 2) {
			mE.x = mD.x;
			mE.y = mD.y;
		} 
		else 
		{ 
			// So get E 
			mE.x = (float) (mD.x + _cos * (width - mD.x));
			mE.y = (float) -(_sin * (width - mD.x));
		} 
	} 
 
	 
	/** 
	 * Called on the first draw event of the view 
	 * @param canvas 
	 */ 
	protected void onFirstDrawEvent(Canvas canvas) {
		 
		mFlipRadius = getWidth();
		 
		ResetClipEdge(); 
		doSimpleCurl(); 
	} 
	 
	/** 
	 * Draw the foreground 
	 * @param canvas 
	 * @param rect 
	 * @param paint 
	 */ 
	private void drawForeground( Canvas canvas, Rect rect, Paint paint ) {		
		if ( ! finished || !bFlipRight ) {
			if ( ! mForeground.isRecycled() ) {
				canvas.drawBitmap(mForeground, null, rect, null);
			} 
		} 
	} 
	 
	/** 
	 * Create a Path used as a mask to draw the background page 
	 * @return 
	 */ 
	private Path createBackgroundPath() {
		Path path = new Path();
		path.moveTo(mA.x, mA.y);
		path.lineTo(mB.x, mB.y);
		path.lineTo(mC.x, mC.y);
		path.lineTo(mD.x, mD.y);
		path.lineTo(mA.x, mA.y);
		return path;
	} 
	 
	/** 
	 * Draw the background image. 
	 * @param canvas 
	 * @param rect 
	 * @param paint 
	 */ 
	private void drawBackground( Canvas canvas, Rect rect, Paint paint ) {
		 
		if ( ! finished ) {
			Path mask = createBackgroundPath();
 
			// Save current canvas so we do not mess it up 
			canvas.save();
			canvas.clipPath(mask);						
		} 
		 
		if ( ! (finished && !bFlipRight) ) { 
			 
			if ( ! mBackground.isRecycled() ) {
				canvas.drawBitmap(mBackground, null, rect, paint);
			} 
			 
			canvas.restore();
		}