0

我对 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);
    }
}
4

1 回答 1

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,默认情况下帧动画将循环播放。此外,您可能希望在getViewAdapter您设置的GridView. 对于动画侦听器,您可以执行一些操作,例如获取总帧数并将其乘以单帧的持续时间。或者这样做

视图.post()

于 2013-03-26T15:33:11.520 回答