Code example for Canvas

Methods: drawPoint, drawText

0
	 
	/** 
	 * Draw a text with a nice shadow 
	 */ 
	public static void drawTextShadowed(Canvas canvas, String text, float x, float y, Paint textPain, Paint shadowPaint) {
    	canvas.drawText(text, x-1, y, shadowPaint);
    	canvas.drawText(text, x, y+1, shadowPaint);
    	canvas.drawText(text, x+1, y, shadowPaint);
    	canvas.drawText(text, x, y-1, shadowPaint);    	
    	canvas.drawText(text, x, y, textPain);
    } 
	 
	/** 
	 * Draw a text with a nice shadow centered in the X axis 
	 * @param canvas 
	 * @param text 
	 * @param y 
	 * @param textPain 
	 * @param shadowPaint 
	 */ 
	public static void drawCentered(Canvas canvas, String text, float y, Paint textPain, Paint shadowPaint)
	{ 
		float posx = (canvas.getWidth() - textPain.measureText(text))/2;
		drawTextShadowed(canvas, text, posx, y, textPain, shadowPaint);
	} 
	 
	/** 
	 * Draw debug info 
	 * @param canvas 
	 */ 
	private void drawDebug(Canvas canvas)
	{ 
		float posX = 10;
		float posY = 20;
		 
		Paint paint = new Paint();
		paint.setStrokeWidth(5);
		paint.setStyle(Style.STROKE);
		 
		paint.setColor(Color.BLACK);		
		canvas.drawCircle(mOrigin.x, mOrigin.y, getWidth(), paint);
		 
		paint.setStrokeWidth(3);
		paint.setColor(Color.RED);		
		canvas.drawCircle(mOrigin.x, mOrigin.y, getWidth(), paint);
		 
		paint.setStrokeWidth(5);
		paint.setColor(Color.BLACK);
		canvas.drawLine(mOrigin.x, mOrigin.y, mMovement.x, mMovement.y, paint);
		 
		paint.setStrokeWidth(3);
		paint.setColor(Color.RED);
		canvas.drawLine(mOrigin.x, mOrigin.y, mMovement.x, mMovement.y, paint);
		 
		posY = debugDrawPoint(canvas,"A",mA,Color.RED,posX,posY);
		posY = debugDrawPoint(canvas,"B",mB,Color.GREEN,posX,posY);
		posY = debugDrawPoint(canvas,"C",mC,Color.BLUE,posX,posY);
		posY = debugDrawPoint(canvas,"D",mD,Color.CYAN,posX,posY);
		posY = debugDrawPoint(canvas,"E",mE,Color.YELLOW,posX,posY);
		posY = debugDrawPoint(canvas,"F",mF,Color.LTGRAY,posX,posY);
		posY = debugDrawPoint(canvas,"Mov",mMovement,Color.DKGRAY,posX,posY);
		posY = debugDrawPoint(canvas,"Origin",mOrigin,Color.MAGENTA,posX,posY);
		posY = debugDrawPoint(canvas,"Finger",mFinger,Color.GREEN,posX,posY);
		 
		// Draw some curl stuff (Just some test) 
		/* 
		canvas.save(); 
		Vector2D center = new Vector2D(getWidth()/2,getHeight()/2); 
	    //canvas.rotate(315,center.x,center.y); 
	     
	    // Test each lines 
		//float radius = mA.distance(mD)/2.f; 
	    //float radius = mA.distance(mE)/2.f; 
	    float radius = mA.distance(mF)/2.f; 
		//float radius = 10; 
	    float reduction = 4.f; 
		RectF oval = new RectF(); 
		oval.top = center.y-radius/reduction; 
		oval.bottom = center.y+radius/reduction; 
		oval.left = center.x-radius; 
		oval.right = center.x+radius; 
		canvas.drawArc(oval, 0, 360, false, paint); 
		canvas.restore(); 
		/**/ 
	} 
	 
	private float debugDrawPoint(Canvas canvas, String name, Vector2D point, int color, float posX, float posY) {	
		return debugDrawPoint(canvas,name+" "+point.toString(),point.x, point.y, color, posX, posY);
	} 
	 
	private float debugDrawPoint(Canvas canvas, String name, float X, float Y, int color, float posX, float posY) {
		mTextPaint.setColor(color);
		drawTextShadowed(canvas,name,posX , posY, mTextPaint,mTextPaintShadow);
		Paint paint = new Paint();
		paint.setStrokeWidth(5);
		paint.setColor(color);	
		canvas.drawPoint(X, Y, paint);
		return posY+15;
	} 
 
}