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! (But only sometimes - like 5% chance)
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
isn't being called in some edge case - but we are not sure.
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
Problem solved. ACTION_UP
and ACTION_CANCEL
were being called. The problem lies in the interaction between scaleDown.start();
and scaleUp.start();
.
If they occur immedietly after one and another, scaleUp.start();
does not automatically override scaleDown.start();
and thus your button is left stuck "down".
SOLUTION: use facebook's spring library.
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.