Alex has posted 1 posts at DZone. View Full User Profile

Connect pictures on Android (Jigsaw puzzle example)

12.27.2011
| 9105 views |
  • submit to reddit

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);
		paint.setColor(Color.RED);
		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);
		imageDrawable.draw(canvas);
		canvas.restore();

		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:

 https://market.android.com/details?id=com.puzzle.jigsaw

Published at DZone with permission of its author, Alex Kizub.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)