2014-03-13 204 views
0

我想開發一個遊戲,當用戶點擊右鍵時,ImageView應該向右移動,當用戶點擊左鍵時ImageView應該放在佈局的左側。我已經把我的兩個左右按鍵OnClickListener:如何從左到右和從右到左動畫一個ImageView

package com.example.game; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.ImageButton; 


public class PlayActivity extends Activity { 
ImageButton leftButton, rightButton; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_play); 
    leftButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

     } 
    }); 
    rightButton.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

     } 
    }); 
} 

但是我不知道該怎麼把代碼OnClickListener方法中,使ImageView的移動或左或右。有沒有人知道動畫的左側和右側的代碼?

+0

什麼API級別,你打算支持?這將有助於確定在動畫中使用什麼選項 – rperryng

回答

0
package com.example.game; 

    import android.os.Bundle; 
    import android.app.Activity; 
    import android.view.Menu; 
    import android.view.View; 
    import android.widget.ImageButton; 


    public class PlayActivity extends Activity implements AnimationListener { 
    ImageButton leftButton, rightButton; 
    ImageView imgLogo; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_play); 
     imgLogo = (ImageView) findViewById(R.id.imgLogo); 


       // load the animation 
     animMove = AnimationUtils.loadAnimation(getApplicationContext(), 
       R.anim.move); 

     // set animation listener 
     animMove.setAnimationListener(this); 


     leftButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
        imgLogo.startAnimation(animMove); 

      } 
     }); 
     rightButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

      } 
     }); 
@Override 
    public void onAnimationEnd(Animation animation) { 
     // Take any action after completing the animation 

     // check for zoom in animation 
     if (animation == animMove) { 
     } 

    } 

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

    } 

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

    } 
    } 

添加此XML在動畫文件夾

<?xml version="1.0" encoding="utf-8"?> 
<set 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/linear_interpolator" 
    android:fillAfter="true"> 

    <translate 
     android:fromXDelta="0%p" 
     android:toXDelta="75%p" 
     android:duration="800" /> 
</set> 
+0

它給我提供錯誤「移動無法解析或不是字段」 – user3412995

+0

當您說「anim文件夾」時,您的意思是活動的xml嗎? – user3412995

+0

在res文件夾中創建一個文件夾anim,然後將上面的xml保存爲該文件夾中的move.xml – Sishin