Android每10秒更换一张图片

15

我正在尝试编写一个非常基础的Android应用程序,它在屏幕上依次显示约5张图片。我想让它在大约10秒钟后显示不同的图片。有谁能告诉我如何解决这个问题?下面我概述了我要寻找的内容。

图片1
图片2
图片3
图片4
图片5

全屏显示图片1
等待10秒钟
删除图片1并显示图片2
等待10秒钟
删除图片2并显示图片3
等待10秒钟
删除图片3并显示图片4
等待10秒钟
删除图片4并显示图片5
等待10秒钟

重新开始

4个回答

42

你考虑过使用关键帧动画(Frame Animations)吗?

你可以在anim文件夹中指定一个xml文件来描述逐帧动画,指定每个图像的持续时间和其他设置,了解一下吧。

更新

当然,你也可以通过编程方式构建一个帧动画:

    AnimationDrawable animation = new AnimationDrawable();
    animation.addFrame(getResources().getDrawable(R.drawable.image1), 100);
    animation.addFrame(getResources().getDrawable(R.drawable.image2), 500);
    animation.addFrame(getResources().getDrawable(R.drawable.image3), 300);
    animation.setOneShot(false);

    ImageView imageAnim =  (ImageView) findViewById(R.id.img);
    imageAnim.setBackgroundDrawable(animation);

    // start the animation!
    animation.start()

谢谢,非常有效。我是一名安卓新手,正在使用3.1为平板编写程序。当我在虚拟安卓设备上运行应用程序时,它只在一个很小的屏幕上显示,就像竖屏一样。有没有什么方法可以解决这个问题?并且是否有任何简单的方法使图像视图填充整个屏幕?非常感谢! - Marty Cochrane
1
尝试将以下内容放置在ImageView的XML属性中:android:layout_width="fill_parent" android:layout_height="fill_parent"。然后,还要设置android:scaleType属性并设置所需的值。请查看此链接:http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html - BFil
我们可以为壁纸服务做这个吗? - user6787631

8
创建 blink.xml 文件。
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/selected" android:oneshot="false">
<item android:drawable="@drawable/Picture_1" android:duration="10000" />
<item android:drawable="@drawable/Picture_2" android:duration="10000" />
<item android:drawable="@drawable/Picture_3" android:duration="10000" />
<item android:drawable="@drawable/Picture_4" android:duration="10000" />
<item android:drawable="@drawable/Picture_5" android:duration="10000" />
</animation-list>

把blink.xml放在drawable文件夹中,然后在activity代码中写入以下内容。
ImageView mImageView ;
mImageView = (ImageView)findViewById(R.id.imageView); //this is your imageView
mImageView .setImageDrawable(getResources().getDrawable( R.drawable.blink));

那么你将得到你想要的东西!


2
AnimationDrawable frameAnimation = (AnimationDrawable) mImageView.getDrawable(); frameAnimation.start(); 你应该添加这些代码来启动动画。 - praveenb

7

您可以使用CountDownTimer:按照以下步骤:

1)声明一个包含您图片标识符的array

2)像这样声明CountDownTimer

int i=0;
new CountDownTimer(10000,1000) {

                @Override
                public void onTick(long millisUntilFinished) {}

                @Override
                public void onFinish() {
                    imgView.setImageDrawable(sdk.getContext().getResources().getDrawable(array[i]));
                    i++;
                    if(i== array.length-1) i=0;
                    start();
                }
            }.start();

2
创建一个 Runnable,执行你想要的更改(我猜想它将会更改一个 ImageView 的 bitmap/drawable),并使用一个 Handler 和它的 postDelayed() 方法,延迟发送到主线程循环中。
为了让它成为一个循环,你可以让这个 runnable 自己再次 post。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接