我有一个关于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);
}
}
答案 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()