如何调用android动画图像的onclicklistner
我正在开发android应用程序 其中我有一个动画图像。 我的代码是如何调用android动画图像的onclicklistner,android,image,animation,Android,Image,Animation,我正在开发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(20
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在动画制作过程中起作用。如何做到这一点。
很抱歉,您的
AnimationListener
中的onAnimationStart
和onAnimationEnd
函数英语不好。保留一个变量以检查单击图像时是否正在播放动画
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");
}
}
});
}
}
我添加了一个完整的示例。