ClickEvent on animated ImageView fires only at the start position
Android:
I have an animated ImageView with an onClickEvent registered. I want the ImageView to move around the screen and then click on the ImageView. But when I click on the moving imageView, nothing happens. W开发者_StackOverflow社区hen I click at the position of the screen, where the imageView was at the beginning, the clickEvent is fired. So the real position of the ImageView does not move with the animation. Has anybody an idea how to get the event when someone clicks the image which is moving on the screen?
My code so far:
public class OnePlayer extends Activity {
private ImageView imageView;
private int points;
private TextView timeTextView;
private TextView pointsTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.oneplayer);
timeTextView = (TextView) findViewById(R.id.time);
timeTextView.setText("01:00");
points = 0;
pointsTextView = (TextView) findViewById(R.id.points);
pointsTextView.setText("0/500");
Animation animation = new TranslateAnimation(0, 100, 0, 200);
animation.setDuration(2000);
animation.setRepeatCount(-1);
animation.initialize(10, 10, 10, 10);
imageView = new ImageView(this);
imageView.setImageResource(R.drawable.plus10);
imageView.setAdjustViewBounds(true);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
imageView.startAnimation(animation);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
v.clearAnimation();
v.setVisibility(View.GONE);
points += 10;
pointsTextView.setText(points + "/500");
}
});
RelativeLayout layout = (RelativeLayout) findViewById(R.id.oneplayergame);
layout.addView(imageView);
}
}
I have shared my solution here. Hope this helps.
精彩评论