tags:

views:

37

answers:

1

The AbsoluteLayout class (and contained LayoutParams class with x and y properties) is deprecated. While it is possible to roll my own class that extends ViewGroup and enables me to control the absolute positioning of the child views - is this the best approach for sprite animation?

First of all, I've made the assumption that moving ImageViews around by changing their x and y position will be more efficient than redrawing bitmaps inside a SurfaceView. Is this assumption right?

My wider question is really: what is the best approach for animating sprites in Android?

A: 

To answer the first question: you can use FrameLayout instead. It's the simplest of all layout managers and simply slaps all children into the top left corner. You can translate them using the offsetTopAndBottom() and offsetLeftAndRight() functions defined on View. Alternatively, the layout parameters for FrameLayout support setting margins. Droid-Fu uses this technique to place sticky views on the screen. Here's the source code.

As to your other question, I assume you mean animations based on key frames. Frame animations are documented here.

Matthias
No, I didn't mean frame animations. Although particular sprites may be frame animations. FrameLayout looks like what I want. Again, my assumption is that moving view widgets around is faster than redrawing bitmaps in a SurfaceView animation.
That doesn't work. offsetTopAndBottom() and offsetLeftAndRight() have no effect on the position of the view. Neither does setPadding(). Is there some kind of "refresh()/invalidate()" method I should be calling? At the moment, I'm trying to move the icon within the run() method of a TimerTask. I assume my TimerTask object is in the UI thread, hence should be no problem updating the UI. Why doesn't it work?
yes it does work -- I am using it. Did you work through the source code I linked? Just run it and go from there. As to the animations, there are only tween and frame animations on Android. Why is that not sufficient for you?
Matthias
Looking back at your example, I see you're setting it in onLayout(). That must be the key to making it work. Like the "Home" example in the SDK that inherits ViewGroup and overrides onLayout() there.