AnimationDrawable问题

时间:2013-03-26 14:45:40

标签: android

我有一个关于AnimationDrawable的问题。我希望在停止frameAnimation之后显示的背景与启动frameAnimation之前显示的背景相同。不幸的是它不是,因为当frameAnimation停止时,背景图像被设置为frameAnimation的最后一个ImageView。

有没有办法解决它?

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);
    }
}

1 个答案:

答案 0 :(得分:1)

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);
                }

            }
        });
    }
}); 

像这样更改setOnItemClickListener,默认情况下,帧动画将循环播放。此外,您可能需要在为getView设置的Adapter调用GridView方法时重置背景。对于动画侦听器,您可以执行一些操作,例如获取帧的总数,并将其与单帧的持续时间相乘。或者这样做

view.post()