Frame-by-frame animations

2019-02-25 17:10发布

问题:

When I try to code a frame-by-frame animation in Eclipse, It gives me a bug. I found something on the internet that says they screwed up in the sdk tutorial documentation but I cannot help but wonder what android:id="selected" means or what should be put in the quotations instead.

Also, can somebody explain the last part of the frame animation tutorial to me? http://developer.android.com/guide/topics/resources/animation-resource.html#Frame Do you put another code in the filename.Java, and if so, where do you put it? I cannot understand where to put the second code that is not XML. I think I need to know what the code below is and where it should go:

ImageView fileimage = (ImageView) findViewById(R.id.file_image);
fileimage.setBackgroundResource(R.drawable.file_image2);

fileAnimation = (AnimationDrawable)fileimage.getBackground();
fileAnimation.start();

But here is the XML code I used:

 <animation-list android:id="selected" android:oneshot="false">
 <item android:drawable="@drawable/filename" android:duration="200" />
 <item android:drawable="@drawable/filename2" android:duration="200" />
 </animation-list>

Should anything be removed or added from that? I don't know what else to do, because I need to start the animation and have the code for that (1st one) but I don't know where it goes, or if I need another code along with it.

回答1:

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.