问题
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.
回答1:
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.
Some other options
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/
回答2:
Android UI tooklit uses Skia for graphics rendering and skia uses the Region abstractions for set operations on shapes (e.g intersection, union, subtract. see Region.Op class) shapes from Paths or Rects.
Region class will also get your far for simple collision detection with Region.quickContains or Region.quickReject methods.
来源:https://stackoverflow.com/questions/13189305/what-is-the-android-equivalent-of-java-awt-geom-area