Android scale button on touch

前端 未结 2 1630
谎友^
谎友^ 2021-01-12 19:42

I know how to scale the button to a determinated value, but is there a way to increase/decrease the button size per time as long the user is touching it? Something like this

相关标签:
2条回答
  • 2021-01-12 20:25

    Since API 21 (Lollipop) an xml-only solution is available based on the stateListAnimator attribute. Take a look at this answer of a similar question for an example.

    0 讨论(0)
  • 2021-01-12 20:39

    Try the following:

    @Override
    public boolean onTouch(View v, MotionEvent motionEvent) {
        int action = motionEvent.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            v.animate().scaleXBy(100f).setDuration(5000).start();
            v.animate().scaleYBy(100f).setDuration(5000).start();
            return true;
        } else if (action == MotionEvent.ACTION_UP) {
            v.animate().cancel();
            v.animate().scaleX(1f).setDuration(1000).start();
            v.animate().scaleY(1f).setDuration(1000).start();
            return true;
        }
    
        return false;
    }
    

    This should do the trick ;)

    0 讨论(0)
提交回复
热议问题