user2071976
user2071976

Reputation: 51

Issue with AnimationDrawable

I have one issue with AnimationDrawable. I want the background shown after stoping the frameAnimation to be the same as the background shown before starting the frameAnimation. Unfortunately it isn't, because when the frameAnimation stops, the background image is set to the last ImageView show by frameAnimation.

is there any way to fix it?

ImageView view;

gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            view=(ImageView)v;
            frameAnimation = (AnimationDrawable) v.getBackground();
            frameAnimation.start();
            myDataThread=new Thread(new DataThread());
            myDataThread.start();

        }
    });

public class DataThread implements Runnable {

    public void run() {
    //do smth
    view.stop();
    view.setBackgroundResource(R.drawable.connectbutton);
    }
}

Upvotes: 0

Views: 1687

Answers (1)

Triode
Triode

Reputation: 11359

gridview.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        view=(ImageView)v;
        view.setBackgroundResource(R.drawable.your_frame_animation_id);
        AnimationDrawable frameAnimation = (AnimationDrawable) view.getBackground();
        frameAnimation.start();

        view.post(new Runnable() {

            @Override
            public void run() {

                if(frameAnimation.getCurrent() != frameAnimation.getFrame(frameAnimation.getNumberOfFrames() - 1))
                {
                    view.post(this);
                }else
                {
                    view.removeCallbacks(this);
                    view.setBackgroundResource(R.id.some_image_resource);
                }

            }
        });
    }
}); 

Change your setOnItemClickListener like this, by default the frame animation will be looping. Also you may want to reset the background when getView method is called for the Adapter that you have set for the GridView. For animation listener you can do some thing like get total number of frames and multiply it with the duration of a single frame. Or do this

view.post()

Upvotes: 1

Related Questions