2011-05-25 196 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