Reputation: 352
While working on project with various animations, found that it is impossible to click on View while it is animated.
onClick works only either on end point of animation or start, but not during.
Let's say, I have a Car object which extends some custom ImageView. And I want to make car moving on a screen. Aim is to make car clickable while moving, so while driving animation user can click on it to perform some action.
public class Car extends GraphicObject{
public Car(Context context, int resourceImage, int rawHeight, int rawWidth) {
super(context, resourceImage, rawHeight, rawWidth);
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "Works while animated.");
}
});
}
public void startMoving(int duration) {
Animation animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, +7f,
Animation.RELATIVE_TO_SELF, -7f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f
);
animation.setRepeatCount(Animation.INFINITE);
animation.setRepeatMode(Animation.RESTART);
animation.setDuration(duration);
startAnimation(animation);
}
}
Given above code performs onClick only on area where car is supposed to finish transition. Unfortunately during transition nothing works.
Is there any possible solution to make an object clickable during animation transition? I really don't want to move object without animation, since it will cause much inconvenience for me.
Upvotes: 1
Views: 1392
Reputation: 352
Thanks to the advice of pskink, the solution was to use Animator instead of Animation. Main issue related to inability of performing onClick on View during animation is resolved in Animator. This would be useful:
https://android-developers.googleblog.com/2011/05/introducing-viewpropertyanimator.html
https://developer.android.com/guide/topics/graphics/prop-animation.html
Note please: If by any chance you are testing your application on Samsung device and animation doesn't work, go to Developer Settings and enable Animation Duration Scale to minimum 1X. In my case it blocked from viewing animation used in app.
Upvotes: 1