如何从左到右,从右到左为ImageView设置动画

时间:2014-03-13 20:00:23

标签: 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.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向左或向右移动。有没有人知道左侧和右侧动画的代码?

1 个答案:

答案 0 :(得分: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>