I want to build complex shapes as the intersection of two circles and a rectangle. After researching a bit, java.awt.geom.Area class seems perfect for this task.
I was dismayed, however, when I discovered that the awt
package doesn't come with the android SDK. Does anyone know of any alternatives for android that allows me to create complex shapes by defining the union and intersection of simpler shapes?
Note: Using graphics clipping to draw the shape doesn't work because I don't just want to draw the shapes, I also want to store the shapes in memory to do collision detection and other interactions.
geom Description. Provides the Java 2D classes for defining and performing operations on objects related to two-dimensional geometry. Some important features of the package include: classes for manipulating geometry, such as AffineTransform and the PathIterator interface which is implemented by all Shape objects.
An Area object stores and manipulates a resolution-independent description of an enclosed area of 2-dimensional space. Area objects can be transformed and can perform various Constructive Area Geometry (CAG) operations when combined with other Area objects.
Android Alternatives to java.awt.geom.Area
EDIT: @numan pointed out an excellent option using some classes in the Android SDK that I was unaware of at the time of the original answer:
https://developer.android.com/reference/android/graphics/Region.html https://developer.android.com/reference/android/graphics/Region.Op.html
Region
allows you to define geometric areas, and then you can use Region
s op()
method with Region.Op
enum
to calculate intersections and more complex shapes.
You can use a Canvas
to draw custom shapes, particularly using the clip* methods:
http://developer.android.com/reference/android/graphics/Canvas.html
Here are some pages about 2d graphics in Android:
http://developer.android.com/guide/topics/graphics/2d-graphics.html http://developer.android.com/guide/topics/graphics/2d-graphics.html#shape-drawable http://developer.android.com/guide/topics/graphics/opengl.html
Some other good options if your graphics remain the same (or roughly the same) are XML-based:
http://developer.android.com/guide/topics/graphics/2d-graphics.html#drawables-from-xml
And one solution I find quite neat, is using 9-patch drawables:
http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
Collision detection It might be overkill for your purposes, but there are a number of game physics libraries:
http://www.andengine.org http://code.google.com/p/andengineexamples/
http://bulletphysics.org
http://www.emini.at/
http://www.dremsus.com/index.php/2012/01/box2d-game-demo-in-android/
Android, libgdx and box2d basics
Or you can roll your own solution:
http://cooers.blogspot.com/2012/08/simple-collision-detection-in-2d.html
http://content.gpwiki.org/index.php/Polygon_Collision
http://www.codeproject.com/Questions/244344/Collision-Detection-in-Android
Collision detection for rotated bitmaps on Android
It really depends on the purpose; for games, you'd probably be best to just use a library; but if collision detection is the only feature you need, you'd be better off doing it yourself to save resources.
Extra Credit: Some indexes of Android libraries
http://www.appbrain.com/stats/libraries/dev
http://www.theultimateandroidlibrary.com/
http://www.openintents.org/en/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With