2011-05-25 194 views
12

我试图编写一个非常基本的android应用程序,在屏幕上一个接一个地显示大约5张图片。我希望它在大约10秒后显示不同的图片。任何人都可以告诉我如何去解决这个问题。下面我列出了我将要寻找的东西。Android每10秒更改一次图片

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

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

再次启动

回答

38

你有没有考虑使用Frame Animations

您可以指定你的动画文件夹中的XML,它包括一帧一帧的动画,specifing每个图像的持续时间,和其他设置,检查出来

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() 
+0

谢谢,这真的很好,即时通讯android新手和即时编程的平板电脑使用3.1的东西,当我在我的虚拟android设备上运行的应用程序,它带来了一个更小的屏幕像肖像是有无论如何修复这个和有什么简单的方法可以让图像视图充满屏幕?谢谢你这么多 – 2011-05-25 16:53:00

+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 2011-05-25 17:01:22

+0

工程很好@BFil。谢谢!!! – groff07 2013-10-24 12:50:35

2

创建Runnable执行您想要的变化(我想这将改变ImageView的位图/可绘制),并使用Handler及其postDelayed()方法将它们延迟发布到主线程循环。

为了让它成为一个循环,你可以拥有可运行的帖子本身。

6

可以使用CountDownTimer:请按照下列步骤操作:

1)声明array这将包含图片的identifients

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(); 
+0

这工作。非常感谢。 – 2015-05-20 07:40:36

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> 

将blink.xml放入可绘制文件夹中并在活动代码中写入 将此代码写入。

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

然后你会得到你想要的。

+1

这一个更清洁 – 2015-08-10 01:00:18

+0

AnimationDrawable frameAnimation =(AnimationDrawable)mImageView.getDrawable(); frameAnimation.start();你也应该添加这些行,以开始动画。 – praveenb 2017-05-02 11:14:21