Android每隔10秒更换一次图片

Mar*_*ane 13 android image

我试着写一个非常基本的Android应用程序,在屏幕上一个接一个地显示大约5张图片.我希望它在大约10秒后显示不同的图片.任何人都可以告诉我如何解决这个问题.下面我概述了我要找的东西.

图1
图2
图3
图4
图5

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

重新开始

BFi*_*Fil 39

你考虑过使用Frame Animations吗?

您可以在动画文件夹中指定包含逐帧动画的xml,指定每个图像持续时间以及其他设置,然后检查出来

UPDATE

您当然可以通过编程方式构建帧动画:

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


Hou*_*ine 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();
Run Code Online (Sandbox Code Playgroud)


Dev*_*von 5

创建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>
Run Code Online (Sandbox Code Playgroud)

把blink.xml放在drawable文件夹和活动代码中写这个.

ImageView mImageView ;
mImageView = (ImageView)findViewById(R.id.imageView); //this is your imageView
mImageView .setImageDrawable(getResources().getDrawable( R.drawable.blink));
Run Code Online (Sandbox Code Playgroud)

那么你会得到你想要的东西.

  • AnimationDrawableframeAnimation = (AnimationDrawable) mImageView.getDrawable(); 帧动画.start(); 您也应该添加这些行来启动动画。 (2认同)