这是我的xml文件
<?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>
我的java文件
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();
答案 0 :(得分:1)
我使用按钮点击事件测试代码,它运行正常。
<强> 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>
答案 1 :(得分:0)
声明ImageView后设置图像可见性。
img.setVisibility(ImageView.VISIBLE);
在按下的按钮上,检查frameAnimation是否正在运行。
如果是,请调用 stop()
方法
frameAnimation.stop();
并将您的方法调用 start()
动画。
frameAnimation.start();
使用runnable启动动画: -
new Runnable {
public void run() {
frameAnimation.start();
}
}
OR
implements Runnable {
public void run() {
frameAnimation.start();
}
}
参考现有问题: -
<强> Frame Animation Not Running 强>