7

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:

Button myButton = (Button)findViewById(R.id.myButton);
    myButton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                // Some timer action here or is there a better way?
                v.setScaleX(v.getScaleX() + 0.1f);
                v.setScaleY(v.getScaleY() + 0.1f);
                return true;
            }
            else if(event.getAction() == MotionEvent.ACTION_UP) {
                v.setScaleX(1);
                v.setScaleY(1);
                return true;
            }

            return false;
        }
    });

Other Idea - doesn't work:

    myButton.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                Timer timer = new Timer();
                TimerTask timerTask = new TimerTask() {
                    @Override
                    public void run() {
                      myButton.setScaleX(myButton.getScaleX() + 0.1f);
                      myButton.setScaleY(myButton.getScaleY() + 0.1f);
                    }
                };
                while(event.getAction() != MotionEvent.ACTION_UP){ //Seems to be an infinite loop
                    timer.schedule(timerTask, 100);
                }

            }
            else if(event.getAction() == MotionEvent.ACTION_UP) {
                v.setScaleX(1);
                v.setScaleY(1);
            }

            return false;
        }
    });

And is there a way to do the whole thing with xml (drawable and animations)?

Maik
  • 79
  • 1
  • 5
  • [Here](http://www.tutorialspoint.com/android/android_animations.htm) is a very good example for what you are trying to achieve. Instead of imageview in the tutorial, you can animate your button. – chejaras Jul 20 '16 at 07:21
  • Does this answer your question? [Animatedly reduce button size on press and regain it's size on release](https://stackoverflow.com/questions/37318133/animatedly-reduce-button-size-on-press-and-regain-its-size-on-release) – Valeriy Katkov Nov 19 '20 at 12:17

2 Answers2

12

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 ;)

Alex Walger
  • 244
  • 2
  • 5
1

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.

Valeriy Katkov
  • 33,616
  • 20
  • 100
  • 123