Connect pictures on Android (Jigsaw puzzle example)
Join the DZone community and get the full member experience.
Join For FreeFew 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
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:
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:
On real picture it will look pretty nice:
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:
Here is application itself on the Android Market:
https://market.android.com/details?id=com.puzzle.jigsaw
Opinions expressed by DZone contributors are their own.
Trending
-
Avoiding Pitfalls With Java Optional: Common Mistakes and How To Fix Them [Video]
-
Extending Java APIs: Add Missing Features Without the Hassle
-
Effortlessly Streamlining Test-Driven Development and CI Testing for Kafka Developers
-
A Complete Guide to AWS File Handling and How It Is Revolutionizing Cloud Storage
Comments