Code example for Canvas

Methods: drawBitmap, drawRoundRect, drawText, restore, saveLayer

0
		Drawable imageDrawable = (image != null) ? new BitmapDrawable(
				getResources(), image) : placeholder;
 
		Bitmap output = Bitmap
				.createBitmap(size, size, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
 
		RectF outerRect = new RectF(0, 0, size, size);
		float cornerRadius = size / 20f;
 
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(Color.RED);
		canvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, paint);
 
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
		imageDrawable.setBounds(0, 0, size, size);
 
		// Save the layer to apply the paint 
		canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
		imageDrawable.draw(canvas);
		canvas.restore();
 
		// FRAMING THE PHOTO 
		float border = size / 15f;
 
		// 1. Create offscreen bitmap link: 
		Bitmap framedOutput = Bitmap.createBitmap(size, size,
				Bitmap.Config.ARGB_8888);
		Canvas framedCanvas = new Canvas(framedOutput);
		// End of Step 1 
 
		// 2. Draw an opaque rounded rectangle link: 
		RectF innerRect = new RectF(border, border, size - border, size
				- border - border);
 
		Paint innerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		innerPaint.setColor(Color.RED);
		framedCanvas.drawRoundRect(innerRect, cornerRadius, cornerRadius,
				innerPaint);
 
		// 3. Set the Power Duff mode 
		Paint outerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		outerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
 
		// 4. Draw a translucent rounded rectangle link: 
		outerPaint.setColor(Color.argb(100, 0, 0, 0));
		framedCanvas.drawRoundRect(outerRect, cornerRadius, cornerRadius,
				outerPaint);
 
		// Draw the frame on top of original bitmap 
		canvas.drawBitmap(framedOutput, 0f, 0f, null);
		Paint paint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint2.setColor(Color.WHITE);
		paint2.setTextSize(size / 20);
		canvas.drawText("This is my profile",
				size / 2 - paint2.measureText("This is my profile"), size
						- border / 2, paint2);
		framedPhoto = output;
	}