简体   繁体   中英

onTouch Event on Layout only registering one Touch per Click

I have the following Problem: I created a new Class extending a ConstraintLayout and I am overriding the onTouchEvent function to animate the Layout.

@Override
    public boolean onTouchEvent(MotionEvent event) {

        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(this, "scaleX", 0.85f);
                ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(this, "scaleY", 0.85f);
                scaleDownX.setDuration(100);
                scaleDownY.setDuration(100);

                AnimatorSet scaleDown = new AnimatorSet();
                scaleDown.play(scaleDownX).with(scaleDownY);

                scaleDown.start();
                break;

            case MotionEvent.ACTION_CANCEL:
                Log.d("arne", "CANCEL");
                // Beide Cases lösen das Event aus
                // ACTION_CANCEL minimiert den Button auch, wenn man den Knopf nicht loslässt, sondern weg wischt
            case MotionEvent.ACTION_UP:
                ObjectAnimator scaleUpX = ObjectAnimator.ofFloat(this, "scaleX", 1f);
                ObjectAnimator scaleUpY = ObjectAnimator.ofFloat(this, "scaleY", 1f);
                scaleUpX.setDuration(100);
                scaleUpY.setDuration(100);

                AnimatorSet scaleUp = new AnimatorSet();
                scaleUp.play(scaleUpX).with(scaleUpY);

                scaleUp.start();
                break;
        }

        return super.onTouchEvent(event);
    }

This works flawlessly for Buttons and if I keep the button pressed down the Event fires multiple times in one Second.

But on an ConstraintLayout it fires only one time, just like the onClick Event.

This is a bit weired in my understanding.

Do you have a solution for this Problem?

Okay, I finally found the solution.

You just need to set your Layout clickable.

android:clickable="true"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM