0

I have created a button that fades in when it is being touched and fades out when it isn't. I was able to achieve this by using setAlpha in java. The code and the problem is shown below:

    buttonPressed.getBackground().setAlpha(0);

    buttonPressed.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View view, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                buttonPressed.getBackground().setAlpha(255);
                buttonPressed.startAnimation(animationFadeIn);
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                buttonPressed.startAnimation(animationFadeOut);
                return true;
            }
            return false;
        }
    });

The issue I have is whenever I release the button, the alpha is set to 0 before animationFadeOut is able to fully play, so the button does not fade back.

If I remove the line:

buttonPressed.getBackground().setAlpha(0);

then animationFadeOut will play but it will immediately go back to setAlpha(255).

How can I get animationFadeOut to play fully and have the button alpha be 0 when the user stops touching the button?

Kevin Li
  • 195
  • 2
  • 14

2 Answers2

1

I think using setInterpolator() for fadeIn and fadeOut animation solves your problem. eg: Animation animationFadeIn = new AlphaAnimation(0, 1); animationFadeIn.setInterpolator(new DecelerateInterpolator()); animationFadeIn.setDuration(1000);

Animation animationFadeOut = new AlphaAnimation(1, 0);
animationFadeOut.setInterpolator(new AccelerateInterpolator());
animationFadeOut.setDuration(1000);

I got his solution from this link and you can know more about AccelerateInterpolator here.

Currently unable to test it. But seems to be promising. Hope this helps!

Community
  • 1
  • 1
Deeps
  • 327
  • 5
  • 13
0

Better not to set the alpha manually, use animators to set the alpha

// Define the animators
Animation fadeInAnimation = new AlphaAnimation(0.0f, 1.0f);
Animation fadeOutAnimation = new AlphaAnimation(1.0f, 0.0f);

// Duration of animation
fadeInAnimation.setDuration(1000);
fadeOutAnimation.setDuration(1000);

public boolean onTouch(View view, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        buttonPressed.startAnimation(fadeInAnimation);
        return true;
     } else if (event.getAction() == MotionEvent.ACTION_UP) {
        buttonPressed.startAnimation(fadeOutAnimation);
        return true;
     }
     return false;
  }
Bajji
  • 2,243
  • 2
  • 22
  • 35
  • With this code, the button would be at full alpha before the animation, and full alpha after the fade out animation is played. I was wondering how I can start and end with 0 alpha, but play the animations when the button is touched. I forgot to mention in my code but I actually have another line to set the button to 0 alpha (updated in the edit). How can I get the button back to 0 alpha when the touch is released. Thanks! – Kevin Li Jul 28 '15 at 00:25