Over a million developers have joined DZone.

Connect pictures on Android (Jigsaw puzzle example)

DZone 's Guide to

Connect pictures on Android (Jigsaw puzzle example)

· Java Zone ·
Free Resource

Few tips how to make pictures on Android more realistic and connect to each other smoothly.

I want to share some experince what I got from my work with Android images when I wrote simple Android appilcation. Here it is in 3 tips:

1) Use Antialiasing option to draw

Unles you want see ugly edges like this

Not using antialiasing

always use Paint.ANTI_ALIAS_FLAG

 However, since we are drawing pictures it's not as simple as drawing lines or pathes. If you simply clipPath no Antialising will help. To make picture really smooth  draw desired path in Antialias mode and then use Xfermode mode and cut your picture through this already Antialising pattern:

	public Bitmap getPuzzlePiece(Path path, Bitmap image) {
		Drawable imageDrawable =  new BitmapDrawable(image);

		RectF rectf = new RectF();
		path.computeBounds(rectf, false);
		path.offset(-rectf.left, -rectf.top);

		Bitmap targetBitmap = Bitmap.createBitmap((int) rectf.width(),
				(int) rectf.height(), Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(targetBitmap);
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		canvas.drawPath(path, paint);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
		imageDrawable.setBounds(0, 0, width, height);// add bounds or picture will be blank

		canvas.saveLayer(null, paint, Canvas.ALL_SAVE_FLAG);
		canvas.translate(-rectf.left, -rectf.top);

		return targetBitmap;

 With such tecnique picture will look a little nicer:

 Antialis image

2) Don't do precise calculations. Leave connections to PATH. 

There is a temptation to calculate all points precisely and make picture perfect. However, remeber, firts of all this is still computer only and it can't do precise calculation even with enourmous resorces. Another thought is that, after all, pictire is in pixels and so it will be never precise by itself.

Let's take example of some critical point of some connection on the drawing path. Assuming that calculation of intersection was wrong (for any reason mintioned above) and resul is blue dot on the left side then it could be some unwanted extra pieces on this path like trainlge. But, if you stop your path earlier and start next element later then path itslef will add not unwanted triangle but real and, probably, not bad connection. See the right connection on the circle:

 Critical points of PATH connection

On real picture it will look pretty nice:

 Real connection on the picture


3) Overlap

Since picture is always not precise make each piece a little bigger so it will overlap other pieces and it will be no gaps between them.

Even final connected picture looks solid in reality there is small invisible overlap:

 Pieces overlap

Here is application itself on the Android Market:



Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}