2014-03-13 206 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