Fade out AlphaAnimation bug on Android > 5.0 - android

The following code is used to fade out a View. It works for Android SDK < 21.
AlphaAnimation animOut = new AlphaAnimation(1, 0); //fromAlpha, toAlpha
animOut.setDuration(500);
animOut.setAnimationListener(new Animation.AnimationListener()
{
public void onAnimationStart(Animation animation){}
public void onAnimationRepeat(Animation animation){}
public void onAnimationEnd(Animation animation)
{
view.setVisibility(View.INVISIBLE);
}
});
view.startAnimation(animOut);
Strangely, it doesn't work as expected for Android 5.0 and above. As soon as animation starts, the animated View becomes invisible immediately.
How can I fix this?
Edit: this bug occurs if AlphaAnimation is started onAnimationEnd() of a TranslateAnimation.

You could use ViewPropertyAnimator.
Should work on newer APIs (12+) and you don't have to bother with setting the visibility of your View.
Something like this (to fade out):
view.animate().alpha(0).setDuration(500).start();
To fade back in, just animate to your initial alpha value:
view.animate().alpha(1).setDuration(500).start();
To add an AnimatorListener, you can call the setListener() method:
view.animate().alpha(0).setDuration(500)
.setListener(new Animator.AnimatorListener() {
// ...
}).start();

Use the AnimationUtils. For example:
AlphaAnimation animOut = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
animOut.setDuration(500);
animOut.setAnimationListener(new Animation.AnimationListener()
{
public void onAnimationStart(Animation animation){}
public void onAnimationRepeat(Animation animation){}
public void onAnimationEnd(Animation animation)
{
view.setVisibility(View.INVISIBLE);
}
});
view.startAnimation(animOut);

Related

How to animate an ImageButton into invisibility

This is a follow up to Animate ImageView from alpha 0 to 1.
I have an ImageButton that I want to animate into view when user does X and then animate out of view when user does Y. Animating into view is working fine. But animating out of view is kind of not working. In fact it is working, in that the animation happens. But after the button fades out of view, it pops back as VISIBLE. So I use this code
AlphaAnimation animation1 = new AlphaAnimation(0.9f, 0.0f);
animation1.setDuration(5000);
animation1.setStartOffset(1000);
animation1.setFillAfter(true);
animation1.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
GVLog.d(TAG,"MAKE INVISIBLE onAnimationStart: %s",tokenBtn.getVisibility());
}
#Override
public void onAnimationRepeat(Animation animation) {
GVLog.d(TAG,"MAKE INVISIBLE onAnimationRepeat: %s",tokenBtn.getVisibility());
}
#Override
public void onAnimationEnd(Animation animation) {
tokenBtn.setVisibility(View.INVISIBLE);
GVLog.d(TAG,"MAKE INVISIBLE onAnimationEnd: %s",tokenBtn.getVisibility());
}
});
tokenBtn.startAnimation(animation1);
but the call to make the ImageButton invisible is not taking effect. So how do I make it take effect so that the ImageButton remains invisible?
Try like this. These codes always work on my apps.
AlphaAnimation animation1 = new AlphaAnimation(1, 0);
animation1.setDuration(1000);
tokenBtn.startAnimation(animation1);
tokenBtn.setVisibility(View.INVISIBLE);

basic animation issue - can't find animate method

I'm reading up on Android property animation and getting stuck on the ground floor. I have an ImageView (and a TextView) that I am trying to animate but there appears to be no "animate" method. I type myImageView. and there are many methods that dropdown but no animate method.
I'm using Java in Eclipse and I have see other code examples where it works.
This is my ImageView...
<ImageView
android:id="#+id/molePic"
android:layout_width="36dip"
android:layout_height="36dip"
android:layout_marginTop="7dip"
android:layout_marginLeft="20dip"
android:src="#drawable/mutebeacon36x36" />
The View.animate() method was added in API level 12 (Android 3.1). What version of Android are you compiling against?
here is an example animation method. it will translate a view 500 pixels to the right in 1000 millisecs.
public void startMyAnimation(final View view) {
TranslateAnimation animation = new TranslateAnimation(0, 500, 0, 0);
animation.setDuration(1000);
view.startAnimation(animation);
animation.setAnimationListener(new TranslateAnimation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
isAnimating = true;
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
});
}
and you call it like so
startMyAnimation(findViewById(R.id.molePic));

Click Listener behavior when two layouts get animated

I have two buttons.When first button is pressed i am translating my layout layout to top and from the top of the screen another layout will come.But my issue is when came back to first layout the click events of the second layout still get fired at its previous position.So what is the Solution of it?I found a lot here on SO as well as on Google but still cannot get the right solution yet.So Please someone help me for my this issue.Thanks in Advance.
TranslateAnimation tr1 = new TranslateAnimation(0, 0, 0, -1100);
tr1.setDuration(1000);
tr1.setFillAfter(true);
layout_login.startAnimation(tr1);
tr1.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
layout_signup.setVisibility(View.VISIBLE);
TranslateAnimation tr2 = new TranslateAnimation(0, 0,
-1100, 0);
tr2.setDuration(1000);
tr2.setFillAfter(true);
tr2.setInterpolator(MainActivity.this,
android.R.anim.linear_interpolator);
layout_signup.startAnimation(tr2);
tr2.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
home_activity_btn_login.setEnabled(true);
}
});
}
});
I think this is a bug with the old animator schemes (I beleive a pretty well known bug, involving fill after not working sometimes). Try using ObjectAnimator instead
Here is an example,
ObjectAnimator oa = ObjectAnimator.ofFloat(view, "translationX", 0, 100f);
oa.setDuration(1000);
oa.start();
If you want to move in the Y direction, you can use translationY. If you want to move in both directions, you need a translationX and translationY, and use an AnimatorSet to play simultaneously.
Check out this comment to this question. Using the old Animation API, apparently dispite fillAfter(true), the buttons click position remains the same. This confirms your issue. So just use the new API, and you should be in good shape.

How to avoid the blink image when save the image after rotate animation?

I am confusing the translate animation and rotate animation. In my game I use this two animations, after completing the animation I am save my image. In translate animation it is fine, but after completing rotate animation my image is blink once. See my code in bellow, please solve my problem……..
Why anyone not respond my question, it is not understand or I am asking any wrong question ? Please tell me reason.................
Thanks.
Bitmap bmp=BitmapFactory.decodeResource(getResources(),R.drawable.train);
//1)
TranslateAnimation TAnimation=new TranslateAnimation(0, 0, 0,-100);//bottom to start
TAnimation.setInterpolator(new LinearInterpolator());
TAnimation.setDuration(2000);
TAnimation.setFillAfter(false);
TAnimation.setFillEnabled(true);
//TAnimation.setFillBefore(true);
Train.startAnimation(TAnimation);
TAnimation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
RelativeLayout RL=(RelativeLayout)findViewById(R.id.rl);
param=new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
param.setMargins(x, y, 0, 0);
Train.setLayoutParams(param);
Train.setImageBitmap(bmp);
}
});
//x and y values are exact position of compliting translateanimation position
//2)
RotateAnimation RAnimation=new RotateAnimation(0,90,50,25);
RAnimation.setInterpolator(new LinearInterpolator());
RAnimation.setDuration(2000);
RAnimation.setFillAfter(false);
TAnimation.setFillEnabled(true);
//RAnimation.setFillBefore(true);
Train.startAnimation(RAnimation);
RAnimation.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
RelativeLayout RL=(RelativeLayout)findViewById(R.id.rl);
param=new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
param.setMargins(x, y, 0, 0);//x and y values are exact position of compliting translateanimation position
Train.setLayoutParams(param);
Train.setImageBitmap(bmp);
}
});
i had that problem but it's reaaaaally simple to fix. You don't need to implement the animation listener, simple don't do it (i had your problem because I use that way).
Do your animation and before call the animation method:
setFillAfter(true); //this save view at the end of the animation
Like so:
//my animation
final Animation rotation = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_up);
//hide login content
content.setVisibility(View.GONE);
//animContent = AnimationUtils.loadAnimation(getActivity(), R.anim.show_up);
rotation.setFillAfter(true);
//animate the arrow
arrow.startAnimation(rotation);
So, delete the listeners and change your setFillAfter(false) to TRUE. Will works ;)

How do I stop an infinite animation on an ImageView, but let it complete its current cycle?

I applied an infinite animation on an ImageView to indicate a running background thread in my app. When the thread finishes, I'm able to stop the animation by using clearAnimation(), but it snaps the ImageView back to its starting position, and I'd like the current animation cycle to complete (which is designed to gracefully end in its starting position). Is there a way to do this?
Register an AnimationListener, and wait until onAnimationRepeat() to clear it. I haven't tried this, but I think it will work.
Just call setRepeatCount(0) and then listen for onAnimationEnd. See here for more details.
You can set the desirable position of your animation in the onAnimationEnd() method of your animation listener. Then, instead of going to the initial position, the View will be displayed at the coordinates you set up there.
animation = new TranslateAnimation(0, -length, 0, 0); //setup here your translation parameters
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(10);
animation.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
animationRunning = true;
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
animationRunning = false;
// setAnimationPositionAfterPositionChange();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) runningText.getLayoutParams();
params.leftMargin = currentPosition; //Calculate you current position here
runningText.setLayoutParams(params);
}
});
runningText.setAnimation(animation);
If you are using animate()
you use setListener(null) to stop it the animation,
works for me.
image.animate()
.translationXBy(-width* 0.5f)
.setDuration(300)
.setStartDelay(6000)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
image.animate().rotationBy(90).setDuration(1000).setStartDelay(1).setListener(null);
}
});

Categories

Resources