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

放肆的年华 提交于 2019-12-05 04:29:20
CodeShane

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/

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!