Android 如何从左到右和从右到左设置ImageView的动画
我正在尝试开发一个游戏,当用户点击右键时,ImageView应该转到右边,当用户点击左键时,ImageView应该转到布局的左边。我已经为左右按钮设置了OnClickListener:Android 如何从左到右和从右到左设置ImageView的动画,android,button,jquery-animate,imageview,clicklistener,Android,Button,Jquery Animate,Imageview,Clicklistener,我正在尝试开发一个游戏,当用户点击右键时,ImageView应该转到右边,当用户点击左键时,ImageView应该转到布局的左边。我已经为左右按钮设置了OnClickListener: package com.example.game; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.I
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向左或向右移动。有人知道左右动画的代码吗
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>
您计划支持哪种api级别?这将有助于确定在java中设置动画时有哪些选项。当您说“动画文件夹”时,会出现错误“移动无法解决或不是字段”,您是指活动的xml吗?在res文件夹中创建一个文件夹anim,然后将上述xml另存为该文件夹中的move.xml