2012-11-08 64 views
3

我使用下面的代碼來設置2個圖像之間的動畫我閃屏:Android的過渡

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.splash); 

    // Show A Transitions for Splash image here. 
    TransitionDrawable transition = (TransitionDrawable) getResources() 
      .getDrawable(R.drawable.splash_animation); 

    //Set interval for the transition between two image. 
    transition.startTransition(5000); 

    //Fetch imageView from your layout and apply transition on the same. 

    ImageView imageView= (ImageView) findViewById(R.id.splash_image); 
    imageView.setImageDrawable(transition); 
} 

我splash.xml是:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ImageView 
     android:scaleType="fitXY" 
     android:id="@+id/splash_image" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:src="@drawable/img_1" /> 
</RelativeLayout> 

我splash_animation.xml文件是:

<?xml version="1.0" encoding="utf-8"?> 
<transition xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/img_1"></item> 
    <item android:drawable="@drawable/img_2"></item> 
</transition> 

轉換工作正常,但我想知道是否有可能爲3張圖片創建它。我嘗試在splash_animation中添加第三張圖片,但僅在第一張圖片上進行了轉換。我如何才能實現儘可能多的圖像?

回答

1

放入TransitionDrawable的數組。

List<TransitionDrawable>array = new ArrayList<TransitionDrawable>(); 

TransitionDrawable transition1 = (TransitionDrawable) getResources() 
      .getDrawable(R.drawable.splash_animation1); // first,second image 

TransitionDrawable transition2 = (TransitionDrawable) getResources() 
      .getDrawable(R.drawable.splash_animation2); // third,fourth image 

array.add(transition1); 
array.add(transition2); 
// call array 
for(TransitionDrawable transition :array){ 
transition.start(5000); 
} 
ImageView imageView= (ImageView) findViewById(R.id.splash_image); 
imageView.setImageDrawable(transition[0]); // if transition[0] is finished setImageDrawable(transition[1]); 
+0

如何調用該數組.... ..../ –

+0

foreach(Transaction transaction:array)transition.startTransition(5000); – Yahor10

+0

我沒有得到...可以請你更新你的代碼... –