2011-09-09 24 views
0

我正在開發android應用程序。當第一個動畫正在進行時,我需要開始第二個動畫。如何在Android應用程序中一個接一個地移動圖像?

我需要在一段時間後逐一移動圖像。我在main.xml中使用了兩個imageViews的framelayout。

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


    <ImageView android:src="@drawable/ars1" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
      android:id="@+id/imageView2"> 
    </ImageView> 

    <ImageView android:src="@drawable/ars" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
      android:id="@+id/imageView1"> 
    </ImageView> 


</FrameLayout> 

下面是我的animation.xml:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_interpolator"> 
    <translate android:fromXDelta="100%p" android:toXDelta="-100%p" 
     android:duration="1500" /> 

</set> 
在我的活動類的onCreate方法

,我在做類似下面這是行不通的。

ImageView imageView1; 
ImageView imageView2; 
Animation tranOut; 

    tranOut = AnimationUtils.loadAnimation(this, R.anim.animation); 

    imageView1 = (ImageView)findViewById(R.id.imageView1); 
    imageView1.setAnimation(tranOut); 

    imageView2 = (ImageView)findViewById(R.id.imageView2); 
    imageView2.setAnimation(tranOut); 

請告訴我如何一個接一個地移動這兩個圖像。

由於 喬蒂

回答

0

嘗試使用動畫監聽器,使用onAnimationEnd(),然後啓動第二個。有關動畫偵聽器的更多信息,請參閱以下鏈接。

http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html

+0

感謝您的回覆。但是當第一個圖像移動時,我必須開始第二個圖像。所以我需要在第一個圖像的動畫開始時,而不是在第一個動畫結束時開始第二個圖像的動畫。請看看你能否幫忙。 – Jyoti

0

我尋覓了很多,最後它使用ImageSwitcher做。

animation.xml:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_interpolator"> 
    <translate android:fromXDelta="150%p" android:toXDelta="0%p" 
     android:duration="1500" /> 

</set> 

animation1.xml:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_interpolator"> 
<translate android:fromXDelta="0%p" android:toXDelta="-150%p" 
     android:duration="1500" /> 

</set> 

helloworld.java:

public class helloworld extends Activity implements ViewFactory { 

public void onCreate(Bundle savedInstanceState) { 

Animation rollIn = AnimationUtils.loadAnimation(this, R.anim.animation); 
Animation rollOut = AnimationUtils.loadAnimation(this, R.anim.animation1); 

final ImageSwitcher iSwitcher = (ImageSwitcher)findViewById(R.id.ImageSwitcher01); 

iSwitcher.setFactory(this); 
iSwitcher.setInAnimation(rollIn); 
iSwitcher.setOutAnimation(rollOut); 
iSwitcher.setImageResource(R.drawable.img1); 

// TODO: Change the image in ImageSwitcher on end of animation rollIn. 

} 

上面的代碼是爲實現該功能的基本代碼(移動圖像相繼)。您可以根據您的要求進行修改。

相關問題