abc
abc

Reputation: 3

frame by frame animation not running

well,i am a newbie to android..and i dont know whats wrong in my code..

this is my xml file

<?xml version="1.0" encoding="utf-8"?>  
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" id="selected" android:oneshot="false">  
    <item android:drawable="@drawable/w1" android:duration="50" />
    <item android:drawable="@drawable/w2" android:duration="50" />
</animation-list>

my java file

ImageView img = (ImageView)findViewById(R.id.s);
img.setBackgroundResource(R.anim.shape_animation);


// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();


// Start the animation (looped playback by default).
frameAnimation.start();

Upvotes: 0

Views: 700

Answers (2)

Ramesh Akula
Ramesh Akula

Reputation: 5740

I test code with button click event ,it works perfectly.

MainAcitvity:

public class FrameAnimationActivity extends Activity {

    AnimationDrawable frameAnimation;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ImageView img = (ImageView) findViewById(R.id.imageView1);

        img.setBackgroundResource(R.anim.frames);
        frameAnimation = (AnimationDrawable) img.getBackground();

        Button btn = (Button) findViewById(R.id.button1);

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                frameAnimation.start();
            }
        });

    }

}

Frames.xml

<?xml version="1.0" encoding="UTF-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/img_0" android:duration="50"  />
    <item android:drawable="@drawable/img_1" android:duration="50" />
    <item android:drawable="@drawable/img_2" android:duration="50" />
    <item android:drawable="@drawable/img_3" android:duration="50" />
    <item android:drawable="@drawable/img_4" android:duration="50" />
    <item android:drawable="@drawable/img_5" android:duration="50" />
    <item android:drawable="@drawable/img_6" android:duration="50" />
    <item android:drawable="@drawable/img_7" android:duration="50" />
    <item android:drawable="@drawable/img_8" android:duration="50" />
    <item android:drawable="@drawable/img_9" android:duration="50" />
</animation-list>

Main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button android:text="Button" android:id="@+id/button1" 
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <ImageView android:layout_height="wrap_content" android:id="@+id/imageView1"
        android:layout_width="wrap_content"></ImageView>
</LinearLayout>

Upvotes: 1

Siva Charan
Siva Charan

Reputation: 18064

Set Image visibility after the ImageView is declared.

  img.setVisibility(ImageView.VISIBLE);

On the button pressed, check frameAnimation is running.

If Yes, then call stop() method

     frameAnimation.stop();

and call your method to start() the animation.

     frameAnimation.start();

use runnable to start the animation:-

new Runnable {
        public void run() {
            frameAnimation.start();
        }
}

OR

implements Runnable {
        public void run() {
            frameAnimation.start();
        }
}

Refer this existing questions:-

Frame Animation Not Running

Upvotes: 0

Related Questions