1

我创建了 animation.xml 文件,在其中添加了我的帧动画,但它不起作用我在我的活动类中调用了 5this,如下所示......

animation = (ImageView)findViewById(R.id.imageAnimation);
        animation.setBackgroundResource(R.drawable.animation);      // the frame-by-frame animation defined as a xml file within the drawable folder
        AnimationDrawable frameAnimation = 
                (AnimationDrawable) animation.getBackground();
            frameAnimation.start();

谁能告诉我那有什么问题

动画.xmal 文件

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="false">

    <item android:drawable="@drawable/anatomy_5" android:duration="50" />
    <item android:drawable="@drawable/anatomy_6" android:duration="50" />
    <item android:drawable="@drawable/anatomy_7" android:duration="50" />
    <item android:drawable="@drawable/anatomy_8" android:duration="50" />
    <item android:drawable="@drawable/anatomy_9" android:duration="50" />
    <item android:drawable="@drawable/anatomy_10" android:duration="50" />
    <item android:drawable="@drawable/anatomy_11" android:duration="50" />
    <item android:drawable="@drawable/anatomy_12" android:duration="50" />
    <item android:drawable="@drawable/anatomy_13" android:duration="50" />
    <item android:drawable="@drawable/anatomy_14" android:duration="50" />
    <item android:drawable="@drawable/anatomy_15" android:duration="50" />
    <item android:drawable="@drawable/anatomy_16" android:duration="50" />
    <item android:drawable="@drawable/anatomy_17" android:duration="50" />
    <item android:drawable="@drawable/anatomy_18" android:duration="50" />
    <item android:drawable="@drawable/anatomy_19" android:duration="50" />
    <item android:drawable="@drawable/anatomy_20" android:duration="50" />
    <item android:drawable="@drawable/anatomy_21" android:duration="50" />
    <item android:drawable="@drawable/anatomy_22" android:duration="50" />
    <item android:drawable="@drawable/anatomy_23" android:duration="50" />
    <item android:drawable="@drawable/anatomy_24" android:duration="50" />
    <item android:drawable="@drawable/anatomy_25" android:duration="50" />
    <item android:drawable="@drawable/anatomy_26" android:duration="50" />
    <item android:drawable="@drawable/anatomy_27" android:duration="50" />
    <item android:drawable="@drawable/anatomy_28" android:duration="50" />
    <item android:drawable="@drawable/anatomy_29" android:duration="50" />
    <item android:drawable="@drawable/anatomy_30" android:duration="50" />
    <item android:drawable="@drawable/anatomy_31" android:duration="50" />
    <item android:drawable="@drawable/anatomy_32" android:duration="50" />
    <item android:drawable="@drawable/anatomy_33" android:duration="50" />
    <item android:drawable="@drawable/anatomy_34" android:duration="50" />
    <item android:drawable="@drawable/anatomy_35" android:duration="50" />
    <item android:drawable="@drawable/anatomy_36" android:duration="50" />
    <item android:drawable="@drawable/anatomy_37" android:duration="50" />
    <item android:drawable="@drawable/anatomy_38" android:duration="50" />
    <item android:drawable="@drawable/anatomy_39" android:duration="50" />
    <item android:drawable="@drawable/anatomy_40" android:duration="50" />
    <item android:drawable="@drawable/anatomy_41" android:duration="50" />
    <item android:drawable="@drawable/anatomy_42" android:duration="50" />
    <item android:drawable="@drawable/anatomy_43" android:duration="50" />
    <item android:drawable="@drawable/anatomy_44" android:duration="50" />
    <item android:drawable="@drawable/anatomy_45" android:duration="50" />
    <item android:drawable="@drawable/anatomy_46" android:duration="50" />
    <item android:drawable="@drawable/anatomy_47" android:duration="50" />
    <item android:drawable="@drawable/anatomy_48" android:duration="50" />
    <item android:drawable="@drawable/anatomy_49" android:duration="50" />

</animation-list>
4

3 回答 3

3

如可绘制动画示例页面的底部所示:

需要注意的是,在 Activity 的 onCreate() 方法期间不能调用 AnimationDrawable 上调用的 start() 方法,因为 AnimationDrawable 尚未完全附加到窗口。如果您想立即播放动画而不需要交互,那么您可能希望从 Activity 中的 onWindowFocusChanged()方法调用它,当 Android 使您的窗口成为焦点时,该方法将被调用。

希望它有帮助.. ;) 干杯

于 2012-05-30T07:15:44.873 回答
1

试试这个

把你的animation.xml文件放在anim文件夹里

简单动画

Animation rAnim = AnimationUtils.loadAnimation(your_context, R.anim.animation);
animation.startAnimation(rAnim);

animation你在哪里imageview

更新

可绘制动画

    AnimationDrawable rocketAnimation;

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

      ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
      rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
      rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
       final Thread thread = new Thread(new Runnable() 
    {
    @Override
    public void run() 
    { wait(1000);      
     activity.runOnUiThread(new Runnable() {@Override public void run()
    {
     rocketAnimation.start();
    }});
     }
});
thread.start();

    }

    public boolean onTouchEvent(MotionEvent event) {
      if (event.getAction() == MotionEvent.ACTION_DOWN) {
        rocketAnimation.start();
        return true;
      }
      return super.onTouchEvent(event);
    }
于 2012-05-30T07:13:44.227 回答
1

res中制作文件夹anim并将您的animation.xml放在anim文件夹中,然后使用这种方式

 Animation anim = AnimationUtils.loadAnimation(this,R.anim.animation);
 imageview.setAnimation(anim); 
 or
 imageview.startAnimation(anim);
于 2012-05-30T07:23:16.753 回答