简体   繁体   English

有时无法收到ACTION_UP或ACTION_CANCEL

[英]Sometimes not receiving ACTION_UP or ACTION_CANCEL

I made an app that has a couple of buttons that get smaller when pressed in order to simulate a physical button going down. 我制作了一个具有几个按钮的应用程序,这些按钮在按下时会变小,以模拟物理按钮下降。

Everything works as intended, but unfortunately, during IRL testing with users, we find that sometimes the button doesn't pop back up! 一切都按预期工作,但不幸的是,在与用户进行IRL测试期间,我们发现有时按钮不会弹出! (But only sometimes - like 5% chance) (但仅有时-5%的机会)

We have no idea what is causing this anomaly, and was wondering if anyone could shed some light on this. 我们不知道是什么原因导致了这种异常,并且想知道是否有人可以对此有所了解。

Currently we are assuming its because ACTION_UP || ACTION_CANCEL 当前,我们假设它是因为ACTION_UP || ACTION_CANCEL ACTION_UP || ACTION_CANCEL isn't being called in some edge case - but we are not sure. 在某些情况下未调用ACTION_UP || ACTION_CANCEL但我们不确定。

public class MainActivity extends CustomActivity
{
    public AppCompatButton myButton;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myButton = (AppCompatButton) findViewById(R.id.my_button);

        myButton.setOnClickListener(myClickListener); // handle the actual click
        myButton.setOnTouchListene(myTouchListener); // simulate press down
    }

    ...

    private View.OnTouchListener myTouchListener = new View.OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            int action = event.getAction();

            if (action == MotionEvent.ACTION_DOWN)
            {
                ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(v, "scaleX", 0.7f);
                ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(v, "scaleY", 0.7f);

                scaleDownX.setDuration(120);
                scaleDownY.setDuration(120);

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

                scaleDown.start();
            }

            if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL)
            {
                ObjectAnimator scaleUpX = ObjectAnimator.ofFloat(v, "scaleX", 1);
                ObjectAnimator scaleUpY = ObjectAnimator.ofFloat(v, "scaleY", 1);

                scaleUpX.setDuration(100);
                scaleUpY.setDuration(100);

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

                scaleUp.start();
            }

            return false;
        }
    };
}

I tested your code. 我测试了您的代码。 It's working perfectly for me. 它对我来说很完美。 If you still getting issues with it, I suggest you to use Rebound - Spring animations for android 如果仍然遇到问题,建议您使用Rebound-Spring android动画

Problem solved. 问题解决了。 ACTION_UP and ACTION_CANCEL were being called. ACTION_UPACTION_CANCEL被调用。 The problem lies in the interaction between scaleDown.start(); 问题在于scaleDown.start();之间的交互scaleDown.start(); and scaleUp.start(); scaleUp.start(); .

If they occur immedietly after one and another, scaleUp.start(); 如果它们紧接着彼此发生,请执行scaleUp.start(); does not automatically override scaleDown.start(); 不会自动覆盖scaleDown.start(); and thus your button is left stuck "down". 因此,您的按钮将保持“按下”状态。

SOLUTION: use facebook's spring library. 解决方案:使用facebook的spring库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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