0

我正在开发 android 应用程序,其中我有一个动画图像。我的代码是

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth()/2;
left = new TranslateAnimation(0, hight, width, hight);
left1= new TranslateAnimation( 480, 10, 0, 10);
left.setDuration(2000);
left.setAnimationListener(this);
b1 =(ImageView)findViewById( R.id.balloon);
b1.setOnClickListener(this);
b1.startAnimation(left);


@Override
 public void onClick(View v) {
  Toast.makeText(this, "Clicked", 27).show();
}

使用此代码我可以为气球或我的图片制作动画,但我的 onclick lisnter 仅在动画完成时才有效我希望 onclicklistner 应该在动画期间工作如何做到这一点。抱歉英语不好

4

1 回答 1

0

在 您onAnimationStart的. 保留一个变量以检查单击图像时是否正在播放动画。onAnimationEndAnimationListener

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

    private ImageView imageView;
    private boolean animationPlaying;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TranslateAnimation animation = new TranslateAnimation( 480, 10, 0, 10);
        animation.setDuration(2000);
        animation.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                animationPlaying = true;
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                animationPlaying = false;
            }
        });

        imageView = (ImageView) findViewById(R.id.imageView1);
        imageView.startAnimation(animation);

        imageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if(animationPlaying) {
                    Toast.makeText(getBaseContext(), "Click", Toast.LENGTH_SHORT).show();
                } else {
                    Log.d("ANIMATION", "click missed because animation was not playing");
                }
            }
        });
    }

}
于 2013-11-04T09:46:42.180 回答