2012-09-05 34 views
0

我正在查看animation-list的文档,XML布局非常简单,但我很困惑他们在代码中如何处理它。如何使动画列表显示在屏幕上

在该网页,他们有这样的:

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 object. 
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground(); 

// Start the animation (looped playback by default). 
frameAnimation.start(); 

但他们从来没有引用spinning_wheel_image在他们表现出任何地方的XML,也不spin_animation ....在他们的例子自旋片段中的ID是“选择“

所以我想知道这两个引用来自哪里?并且为什么从未使用过“被选中”的XML片段的实际ID?

谢谢!

编辑:

我把我的动画XML到一个名为animation.xml

并在代码现在我有这样的:

ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image); 
     img.setBackgroundResource(R.drawable.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(); 

回答

1

但他们从来没有引用任何地方spinning_wheel_image在他们显示的xml中

spinning_wheel_imageImageView的ID,推测来自与setContentView()LayoutInflater一起使用的布局资源。

也不spin_animation ....在他们的例子自旋片段中的ID是“选择”

这大概是一个错字 - 没有android:idAnimationDrawable。至少,它不在文档中。动画资源的名称基于文件名,文件名为spin_animation .xml

1

动画与任何视图都没有关系,除非您明确将其分配给。它只是定义了一组要完成的转换。然后,您选择您的视图并对其应用动画。这使您可以将相同的动画应用于多个视图。您可能需要将动画XML文件移至res/anim文件夹。

相关问题