我已经在Android开发者网站上看到了如何将图像序列转换为动画。但没有显示任何内容。无论是图像而不是动画。
package org.example.anim;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.widget.ImageView;
public class MainActivity extends Activity {
AnimationDrawable frameAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
img.setBackgroundResource(R.drawable.spin_animation);
// Get the background, which has been compiled to an AnimationDrawable
frameAnimation = (AnimationDrawable) img.getBackground();
// Start the animation (looped playback by default).
}
public void onWindowFocusChanged()
{
frameAnimation.start();
}
}
答案 0 :(得分:1)
ImageView progressSpinner = (ImageView) findViewById(R.id.progressSpinner);
// Set the background of the image - In this case an animation
// (/res/anim folder)
progressSpinner.setBackgroundResource(R.anim.progress_animation);
// Get the image background and attach the AnimationDrawable to it.
progressAnimation = (AnimationDrawable) progressSpinner.getBackground();
progressAnimation.start();
将图像从drawable移动到anim文件夹,并在onCreate中尝试此代码。
答案 1 :(得分:0)
如果您添加了多张图片,并且您的spin_animation列表如下:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>
然后代码必须正常工作,我认为您还没有正确生成此列表。另请参阅此链接以获取更多帮助animation drawable