如何连续更改图片ImageView?

mar*_*iya 3 android android-imageview

我在应用程序中有一个ImageView.我有2张可绘制文件的照片.我想拍照ImagView每秒改变一次.即:一秒钟后

  imagview.setImageDrawable(getResources().getDrawable(R.drawable.photo1));
Run Code Online (Sandbox Code Playgroud)

几秒钟后

   imagview.setImageDrawable(getResources().getDrawable(R.drawable.photo2));
Run Code Online (Sandbox Code Playgroud)

这个过程将持续到最后

我是怎么做到的

Rem*_*yde 9

它被称为逐帧动画,由一系列Drawable对象定义,可用作View对象的背景.您可以在此处找到详细信息.示例代码发布在下面.持续时间以毫秒为单位,1 sec你必须给它1000.

res/drawable /文件夹中的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>
Run Code Online (Sandbox Code Playgroud)

这是加载和播放此动画的代码.

 // 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();
Run Code Online (Sandbox Code Playgroud)

并停止动画

frameAnimation.stop();
Run Code Online (Sandbox Code Playgroud)