2015-11-07 53 views
0

我剛開始學習android。我想在圖像視圖中顯示兩個以上的圖像,並在它們之間有延遲。我能做到這一點的時候只有兩個圖像都顯示,像這樣:在圖像視圖中顯示兩個以上圖像,並在它們之間存在延遲活動

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    btn1 = (Button) findViewById(R.id.button); 
    img = (ImageView) findViewById(R.id.img); 

    btn1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      new CountDownTimer(3000, 1000) { 
       public void onTick(long millisUntilFinished) { 
       } 

       public void onFinish() { 
        img.setImageResource(R.drawable.icon2); 
       } 
      }.start(); 
     } 
    }); 
} 

我重複倒計時器,但第二圖像未顯示和第三的表現。這裏是我的問題:有沒有辦法解決上述問題或者我還需要做其他的事情?感謝回覆。

+0

圖像在哪裏? –

回答

0

嘗試;

public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     btn1 = (Button) findViewById(R.id.button); 
     img = (ImageView) findViewById(R.id.img); 

     btn1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       new CountDownTimer(3000, 1000) { 
        public void onTick(long millisUntilFinished) { 
        } 

        public void onFinish() { 
         img.setImageResource(R.drawable.icon2); 
        } 
       }.start(); 
       Timer timer = new Timer(); 
       timer.schedule(new TimerTask() { 
        @Override 
        public void run() { 

         Activity.this.runOnUiThread(new Runnable() { 
           @Override 
           public void run() { 
      if(order%2==0){ 
          img.setImageResource(R.drawable.icon1); 
         }else{ 
          img.setImageResource(R.drawable.icon2); 
         } 
           } 
          }); 

         order++; 
        } 
       },0,4000); 
      } 
     }); 

    } 

    private int order = 0; 
+0

對不起,它不起作用。謝謝 – Beppe

+0

哦,我更新了它 –

+0

哇,你太棒了,非常感謝@tiny的sublight。 – Beppe

1

您可以使用TransitionDrawable這個。例如。

Drawable drawables[] = new Drawable[2]; 
drawables[0] = getResources().getDrawable(R.drawable.icon1); 
drawables[1] = getResources().getDrawable(R.drawable.icon2); 
TransitionDrawable crossfader = new TransitionDrawable(drawables); 
ImageView img = (ImageView)findViewById(R.id.img); 
img.setImageDrawable(crossfader); 
crossfader.startTransition(3000); 

代碼將在三秒

交叉衰減ICON1和ICON2

編輯:

爲n個不同的繪製資源,你可以使用一個處理程序。例如

int drawables [] = { R.drawable.icon1, R.drawable.icon1, ... R.drawable.icon }; 

聲明int currentIndex = 0;作爲成員類。然後在的onCreate,後

img = (ImageView) findViewById(R.id.img); 

你做

img.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      img.setImageResource(drawables[currentIndex++%drawables.length]); 
      img.postDelayed(this, 1000); 
     } 
    }, 1000); 

這樣的繪製將改變每一秒都沒有淡入淡出它。撥打img.removeCallbacks(null)

+0

對不起,它只適用於2張圖片,儘管第一張圖片並沒有消失。但我的問題是:我有3個或更多的圖像。 – Beppe

1

忘記更改圖像資源參數。
您的代碼總是參考R.id.icon2而不是另一個圖像

img.setImageResource(R.drawable.icon2); //你的問題
您可以將圖像保存Drawble陣列

+0

其工作正常,但對於第三張圖片,並延遲之前我有麻煩。 – Beppe

0

這是正確的方向。使用處理程序。

new Handler().postDelayed(new Runnable() { 
//Some code 
},3000); 
+0

其工作時,我只有2個圖像,但是當我想顯示3或4並寫兩次或更多的建議塊,它不起作用。 – Beppe

0
使用

陣列

int[] images = { 

     R.drawable.troj1,R.drawable.troj2,R.drawable.troj3,R.drawable.troj4,R.drawable.troj5, //0-4 
     R.drawable.koc1,R.drawable.koc5,R.drawable.koc3,R.drawable.koc4,R.drawable.koc2,//5-9 
     R.drawable.kruh1,R.drawable.kruh2,R.drawable.kruh3, ///10-12 
     R.drawable.tutorial1, 
     R.drawable.tutorial2, 
     R.drawable.tutorial3, 
     R.drawable.nic, 
}; 

呼叫陣列

ImageView的potom;

current_image_index =(int)(Math.random()* 12); potom.setImageResource(圖像[current_image_index]);

+0

設置圖像沒有問題,我的問題是我想在設置圖像之間延遲,當我有3,4或更多的圖像。 – Beppe

相關問題