So I\'m animating a view using animation:
I find that simply setting fillEnabled and fillAfter to TRUE does not solve the issue when things like click/touch listeners are involved. Setting fill enabled and fill after to be true just ensures that the pixels of the view are DRAWN in the correct final place. However, suppose the view being animated has a onclick listener associated with it. Clicking the view in its new visible position will do nothing, however clicking the screen in the position the view was originally will trigger the onClick.
The following is an example of how to get around this:
Suppose we have a view with an onClickListener set to it. Suppose also that the views bounds are 0, 0, 0, 0 (left, top, right, bottom). Suppose now we wish to move the view to the right by 200. The following code will demonstrate how to do this in a manner that will move both the pixels to be drawn and the underlying view object to the correct position, hence adjusting the onClick to be called from the correct position.
private Animation generateMoveRightAnimation() {
final Animation animation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 200,
Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0);
animation.setDuration(300);
animation.setFillAfter(true);
animation.setFillEnabled(true);
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) view.getLayoutParams();
// change the coordinates of the view object itself so that on click listener reacts to new position
view.layout(view.getLeft()+200, view.getTop(), view.getRight()+200, view.getBottom());
repeatLevelSwitch.clearAnimation();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
return animation;
}