2012-05-09 26 views

回答

2

我無法解釋這裏的一切,但我幫你做到這一點。首先,您必須爲每個圖像創建兩個新的XML文件(舊版本的動畫或新版本的Tween動畫),然後使用翻譯。例如:

<translate android:fromXDelta="10" 
    android:toXDelta="100" 
    android:duration="2000"  
    /> 

用於左側圖像,右側用於您的號碼。 然後在java文件中設置你的Button和圖像。然後定義動畫並使用它們。我認爲這個例子是可以理解的:

final ImageView iv1 = (ImageView) findViewById(R.id.imageView1); 
    final ImageView iv2 = (ImageView) findViewById(R.id.imageView2); 
    final Button b = (Button) findViewById(R.id.button1); 
    b.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Animation anim = AnimationUtils.loadAnimation(main.this, R.anim.animation); 
      iv1.startAnimation(anim); 
      iv2.startAnimation(anim); 
     } 
    }); 

在這個例子中,我爲它們都使用了一個動畫。 並記住在這個網站,你可以從其他,而不是絕對的代碼獲得幫助。

+0

謝謝你的幫助... – user1371813

+1

如果這是你的答案,你必須接受它。這是我們回答您的問題的理由。 – Hosein

1

兩個XML文件添加到您的項目res/anim文件夾象下面這樣:

lefttoright.xml:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
android:interpolator="@android:anim/accelerate_decelerate_interpolator"> 
<translate android:fromXDelta="-600%" android:toXDelta="0%" 
    android:fromYDelta="0%" android:toYDelta="0%" android:duration="300" 
    android:zAdjustment="bottom"> 
</translate> 

righttoleft.xml:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
android:interpolator="@android:anim/accelerate_decelerate_interpolator"> 
<translate android:fromXDelta="0%" android:toXDelta="400%" 
    android:fromYDelta="0%" android:toYDelta="0%" android:duration="300" 
    android:zAdjustment="bottom"> 
</translate> 

在你的佈局文件保持一個ImageView在屏幕和另一個左邊屏幕

使用下面的代碼片段在按鈕的onClick事件的正確:

lefttoright = AnimationUtils.loadAnimation(context, 
      R.anim.lefttoright); 
    righttoleft = AnimationUtils.loadAnimation(context, 
      R.anim.righttoleft); 
imageView1.startAnimation(lefttoright); 
imageView2.startAnimation(righttoleft); 

然後實現動畫聽衆:

lefttoright.setAnimationListener(new Animation.AnimationListener() { 

        @Override 
        public void onAnimationStart(Animation animation) { 
         // TODO Auto-generated method stub 

        } 

        @Override 
        public void onAnimationRepeat(Animation animation) { 
         // TODO Auto-generated method stub 

        } 

        @Override 
        public void onAnimationEnd(Animation animation) { 
         // TODO Auto-generated method stub 
         //update imageView's position (e.g. center) 
        } 
       }); 

righttoleft做同樣的事情。

希望它有幫助。

+0

感謝您的幫助... – user1371813

+0

lefttoright = AnimationUtils.loadAnimation(context,R.anim.lefttoright);什麼是lefttoright在這裏 – user1371813

+0

對不起,忘了定義類型! 'lefttoright'是一個'Animation'類型的變量。 – jtanveer

相關問題