Home > Software engineering >  view.setOnClick() during animation not working
view.setOnClick() during animation not working

Time:12-10

I'm new in android development and have now problem and can not understand why and solve it. Sorry, here is my idea and hope it helps to understand my question: I would like to develop an app for the kids. I have two car roads. On both roads drive different cars with a certain number (0-10). The cars drive from left to right with the help of animation. A question with a number is asked. The child has to click on a certain car with the number x. Below I've imageview (imgCarUp1) and try to animate from left to right. During animation I want to make click-event on my image and should run some code.

`

    imgCarUp1 = findViewById(R.id.imgCarUp_1);
    imgCarUp1.setImageResource(vehicleUpStreetList.get(0).getResId());
    imgCarUp1.setTag(vehicleUpStreetList.get(0).getResId());

    imgNumberUp1.setOnClickListener(view -> {
        Log.d(TAG, String.format("setOnClickListener Tag: %s", view.getTag()));
    ...
    });

    TranslateAnimation animation = new TranslateAnimation(0, width - 50, 0, 0);
    Animation.AnimationListener animL = new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
    }
    @Override
    public void onAnimationRepeat(Animation animation) {
    }
    @Override
    public void onAnimationEnd(Animation animation) {
        LogUtils.i(TAG, "onAnimationEnd is stopped!");
    }
    };

    animation.setAnimationListener(animL);
    animation.setDuration(15000);
    animation.setRepeatCount(5);
    animation.setRepeatMode(1);
    animation.setFillAfter(true);
    imgNumberUp1.startAnimation(animation); 

`

Aninmation is working well. But if I try to click on imageview (imgNumberUp1) nothing will work. But if I click on area where animation was started, setOnClickListener works. I should be able to make click during animation. What is here my problem? Can someone please help with code?

Thanks a lot for helping.

Tried to search in internet, without any solution yet.

CodePudding user response:

You are using Translate animation. Translate Animation translates the views position to the animated positions and wont actually change the views original position.

So you will need to change X and Y value to change the original position and then handle clicks wherever the animation is currently positioned

you can do . view.animate().X().y().start()

while x and y will have the params of where you want to animate

  • Related