Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the android equivalent of java.awt.geom.Area?

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.

like image 866
Razor Storm Avatar asked Nov 02 '12 04:11

Razor Storm


People also ask

What is Java AWT geom?

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.

What is Area in java?

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.


1 Answers

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 Regions 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/

like image 167
CodeShane Avatar answered Sep 25 '22 06:09

CodeShane