2011-08-17 100 views
4

我正在開發一個圖庫應用程序,在這個應用程序中,我使用圖庫在幻燈片中顯示圖像。它工作正常,但我想添加一些動畫。我只能使用此代碼應用一個動畫,但我想在圖庫視圖中添加兩個動畫效果。 說在翻譯效果,從右到中心和從中心到左側。請任何人幫助我。Android圖庫幻燈片動畫

public void slidShow(){ 

    Runnable runnable = new Runnable() { 

     @Override 
     public void run() { 
      myslideshow(); 
      handler.postDelayed(this, 3000);     
     } 
    }; 
    new Thread(runnable).start(); 
} 

private void myslideshow(){ 
    PicPosition = gallery.getSelectedItemPosition() +1;    
    if (PicPosition >= bitmaps.size()){ 
     PicPosition = gallery.getSelectedItemPosition(); //stop  
    } 
    else{ 
     Animation inFromRight = new TranslateAnimation(
       Animation.RELATIVE_TO_PARENT, +1.0f, 
       Animation.RELATIVE_TO_PARENT, 0.0f, 
       Animation.RELATIVE_TO_PARENT, 0.0f, 
       Animation.RELATIVE_TO_PARENT, 0.0f); 
     inFromRight.setDuration(500); 
     inFromRight.setInterpolator(new AccelerateInterpolator()); 
     gallery.startAnimation(inFromRight); 
     gallery.setSelection(PicPosition); 

    } 
} 

回答

2

使用基於XML的動畫 創建文件夾資源XML文件/動畫/ animate.xml

把代碼

<set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="true"> 
    <translate 
     android:fromXDelta="0%p" android:toXDelta="50%p" // change this to decide the range 
     android:duration="500" android:startOffset="0"/> 
    <translate 
     android:fromXDelta="0%p" android:toXDelta="100%p" 
     android:duration="500" android:startOffset="500"/>// change this to increase the 
time for image to stay 

</set> 

現在在你的函數myslideshow()變化

Animation inFromRight = AnimationUtils.loadAnimation(this, R.anim.animate); 
gallery.startAnimation(inFromRight); 
     gallery.setSelection(PicPosition); 

這就是所有.....