Applying physics to Android View objects
Asked Answered
T

4

18

I currently have an android application that utilises the native android views for the UI (e.g. imageviews/buttons etc)

Although this app is not a game, nor requires any heavy graphics or OpenGL, I would like to incorporate a little physics-related interaction. Nothing too significant but maybe display minor collisions/bounces, deceleration/acceleration or possibly friction.

Is this possible to simulate either within the android framework or using an external physics library like jbox2d without having to utilize an entire game engine (like andengine, libgx etc).

P.S. this is for API 15+

Thanks all.

Tensor answered 6/2, 2014 at 16:47 Comment(1)
Please elaborate how you wish to utilize physics, do you want to be able to "throw" a button around? Or an image? What standard user interaction functions will you need for the objects with physical property imbued? The specific requirements would affect how the result can be achieved.Overbite
A
1

As long as you can set/update the position of the UI elements every frame (e.g. with View.setTranslationX()), you can do it.

Animating the UI by hand would mean keeping a mapping between the UI elements and the physical (e.g. jbox2d) bodies that correspond to them. Then you update the respective positions of the UI elements to what the simulated bodies have each frame (such as body.getPosition()).

In event-driven apps, something like a Timer object is useful for scheduling the physics updates.

Arst answered 21/4, 2015 at 5:18 Comment(0)
A
1

The easiest way (not that flexible) is using ViewPropertyAnimator.

With it you can animate properties even in parallel. Maybe you want to change a view margin from the parent left. You can change that margin there, setting up the time to move and else. Some examples on this blog

ValueAnimator is other alternative, which uses the AccelerateDecelerateInterpolator by default and you have to provide the code that changes the property you need. This official guide may suffice to get them working.

Aerostat answered 21/4, 2015 at 15:22 Comment(0)
D
0

what you want is quite impossible - your options are:

  1. you may fake stuff like bounces with animations
  2. you can implement a whole new ui lib
  3. you use openGL what i would do
Disaccord answered 6/2, 2014 at 16:49 Comment(5)
Thank's for the prompt response, how about jBox2d? I've seen stuff like this implementation, but upon further reading elsewhere, my understanding is that it's not 'android friendly'Tensor
i can't tell something concrete about this lib but a android View is not ment to have physics.. rendering the views is more or less "depending" on static none moving content.. so as i said above..Disaccord
@jedikv jBox2d is what Angry Bird games use, so it should work fine enough.Overbite
i thought it uses the flash box2d thingy - but not sureDisaccord
jbox2d is not applying physics to viewsDisaccord
S
0

I think this can be achieved, at least at some point.

You have access to a pretty complete Animations API. This last, together with some view bounds, distance, and probably device orientation calculations, can be used to simulate almost all the cases you mentioned.

You can create Accelerate/Decelerate, form change... and almost whatever animation you want.

How you can achieve what you are asking for:
Having different animations, one after another, creating and applying them dynamically depending on the view item state. And by state, I mean the "physical state": falling, collisioning, in touch with another view, and so on...

Smashup answered 18/4, 2015 at 1:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.