I have a basic question about starting a frame-by-frame animation.
When I call the AnimationDrawable.start() method from my code directly, it doesn't seem to work.
public void onCreate(Bundle savedInstanceState) {
...
mAnimation.start();
...
}
But if I put this line inside the onClick() callback method of a button, pressing the buton starts the animation.
Why doesn't this line work in the code?
Thanks!
public class MyAnimation extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
AnimationDrawable mframeAnimation = null;
super.onCreate(savedInstanceState);
setContentView(R.layout.my_animation);
ImageView img = (ImageView) findViewById(R.id.imgMain);
BitmapDrawable frame1 = (BitmapDrawable) getResources().getDrawable(
R.drawable.splash1);
BitmapDrawable frame2 = (BitmapDrawable) getResources().getDrawable(
R.drawable.splash2);
int reasonableDuration = 250;
mframeAnimation = new AnimationDrawable();
mframeAnimation.setOneShot(false);
mframeAnimation.addFrame(frame1, reasonableDuration);
mframeAnimation.addFrame(frame2, reasonableDuration);
img.setBackgroundDrawable(mframeAnimation);
mframeAnimation.setVisible(true, true);
//If this line is inside onClick(...) method of a button, animation works!!
mframeAnimation.start();
}
}
Frame-by-frame animation is more commonly known as stop-motion animation. It is achieved by manipulating a physical object and making it appear to move on its own by shooting one frame, manipulating the object, then shooting another frame, and so on.
Many animators still use this method for the following reasons: Recreate hand-drawn animation's traditional characteristics and appeal. Frame-by-frame animation, such as stop motion or rotoscope, is the only way to create some forms of animation. Help make your characters feel alive and key in on facial expressions.
It's Time-Consuming:Having to produce each frame one-by-one is an incredibly time-consuming process. This is especially true for traditional animation, which requires an extremely detailed drawing to be completed for every new frame.
For one second in a cartoon, the animator has to create 24 frames. The more detailed the drawing made, the smoother the movement of the character. It's a hard job, and that is why often it's done by several artists. This technique takes more time, but the results look very effective.
It's important to note that the start() method called on the AnimationDrawable cannot be called during the onCreate() method of your Activity, because the AnimationDrawable is not yet fully attached to the window. If you want to play the animation immediately, without requiring interaction, then you might want to call it from the onWindowFocusChanged() method in your Activity, which will get called when Android brings your window into focus. Very end of the page http://developer.android.com/guide/topics/graphics/2d-graphics.html
ImageView img = (ImageView)findViewById(R.id.some layout);
AnimationDrawable frameAnimation = (AnimationDrawable)img.getDrawable();
frameAnimation.setCallback(img);
frameAnimation.setVisible(true, true);
frameAnimation.start();
and to add animation you can do something like
<animation-list android:id="@+id/my_animation" android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/frame1" android:duration="150" />
<item android:drawable="@drawable/frame2" android:duration="150" />
</animation-list>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With