2015-02-23 47 views
3

我在应用程序内部有一个ImageView。 我有2张可绘制文件的照片。 我想照片ImagView 每秒钟都在变化。 即: 后第二如何更改图片ImagView连续?

imagview.setImageDrawable(getResources().getDrawable(R.drawable.photo1)); 

秒后

imagview.setImageDrawable(getResources().getDrawable(R.drawable.photo2)); 

而这一过程将一直持续到年底

谁能告诉我该怎么办呢?

回答

8

它被称为逐帧动画,由一系列Drawable对象定义,可用作View对象的背景。您可以找到详细信息here。有样品代码在下面发布。 1 sec的持续时间以毫秒为单位,您必须将其作为1000。在res /绘制/文件夹

spin_animation.xml文件:

<animation-list android:id="@+id/selected" android:oneshot="false"> 
    <item android:drawable="@drawable/wheel0" android:duration="50" /> 
    <item android:drawable="@drawable/wheel1" android:duration="50" /> 
    <item android:drawable="@drawable/wheel2" android:duration="50" /> 
    <item android:drawable="@drawable/wheel3" android:duration="50" /> 
    <item android:drawable="@drawable/wheel4" android:duration="50" /> 
    <item android:drawable="@drawable/wheel5" android:duration="50" /> 
</animation-list> 

这里是加载和播放这个动画的代码。

// Load the ImageView that will host the animation and 
// set its background to our AnimationDrawable XML resource. 
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(); 

,并停止动画

frameAnimation.stop(); 
+1

非常感谢你。 它帮了我 – mariya 2015-02-23 15:14:54

+0

快乐地帮助玛丽亚:)。 – 2015-02-23 15:17:39

+0

谢谢...点击一个按钮后,这个过程可以停止吗? – mariya 2015-02-23 15:24:40