我正在尝试编写一个非常基础的Android应用程序,它在屏幕上依次显示约5张图片。我想让它在大约10秒钟后显示不同的图片。有谁能告诉我如何解决这个问题?下面我概述了我要寻找的内容。
图片1
图片2
图片3
图片4
图片5
全屏显示图片1
等待10秒钟
删除图片1并显示图片2
等待10秒钟
删除图片2并显示图片3
等待10秒钟
删除图片3并显示图片4
等待10秒钟
删除图片4并显示图片5
等待10秒钟
重新开始
你考虑过使用关键帧动画(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()
<?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>
ImageView mImageView ;
mImageView = (ImageView)findViewById(R.id.imageView); //this is your imageView
mImageView .setImageDrawable(getResources().getDrawable( R.drawable.blink));
那么你将得到你想要的东西!
您可以使用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();
Runnable
,执行你想要的更改(我猜想它将会更改一个 ImageView
的 bitmap/drawable),并使用一个 Handler
和它的 postDelayed()
方法,延迟发送到主线程循环中。