Frame-by-frame animations

[亡魂溺海] 提交于 2019-12-02 01:07:47

This is how I implemented it.

In your main java file you should have something like this.

public class Main extends Activity { 
AnimationDrawable mainanimation;

public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.main);

     ImageView mainimage = (ImageView) findViewById(R.id.MainAnim);
     mainimage.setBackgroundResource(R.anim.mainanim);
     mainanimation = (AnimationDrawable) mainimage.getBackground();

So you set the ImageView in your main.xml layout file to the xml that contains the animation (R.id.MainAnim)

Then in your MainAnim.xml (located in res/anim) file you write

<?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/image1" android:duration="2000" />
<item android:drawable="@drawable/image2" android:duration="2000" />
</animation-list>

Now image1 and image2 will alternate back and forth at 2 seconds each. Also I didn't use andriod:id="selectable".

To recap you need 3 files. Your Main.java, your main.xml layout file, and your mainanim.xml file located in res/anim. Also your 2 images in the drawable folder.

Hope that clears it up a little.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!