Animation flickering in ANDROID - android

I am trying to implement something relatively simple in ADNROID. I have a bunch of Drawables stored in a list. I want to create an effect where the drawables are displayed on top of each other, one by one, using a fade-in effect.
I have had partial success with the code below. It actually works flawlessly on my phone (Nexus S), but it shows flickering on my tablet (ASUS TF101). This is probably due to the fact that the tablet has a faster CPU.
Here is my setup: I have stored all the Drawables in the drawables list. I have also defined two images in my layout, one on top of the other: imageViewForeground and imageViewBackground.
The idea is to set the background image first, then start an animation where the foreground image starts from alpha-0 and goes to alpha-1. Then replace the background with the new foreground image, choose a new foreground (i.e. the next drawable) and loop forever.
The counter object is a simple wrapper for an int counter.
Here is my code:
final Animation fadeInAnimation = new AlphaAnimation(0f, 1f);
fadeInAnimation.setDuration(2000);
fadeInAnimation.setStartOffset(3000);
fadeInAnimation.setFillBefore(false);
fadeInAnimation.setFillAfter(true);
fadeInAnimation.setRepeatCount(Animation.INFINITE);
fadeInAnimation.setRepeatMode(Animation.RESTART);
fadeInAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
imageViewForeground.setImageDrawable(drawables.get(counter.value()));
Log.d(TAG, "onAnimationStart");
}
#Override
public void onAnimationEnd(Animation animation) {
Log.d(TAG, "onAnimationEnd");
}
#Override
public void onAnimationRepeat(Animation animation) {
imageViewBackground.setImageDrawable(drawables.get(counter.value()));
counter.increase();
// the problem appears in this line,
// where the foreground becomes visible for a very small period,
// causing the flickering
imageViewForeground.setImageDrawable(drawables.get(counter.value()));
}
});
imageViewForeground.startAnimation(fadeInAnimation);
Any ideas how I can overcome this flickering problem?

Related

AlphaAnimation Rotates Framelayout in KitKat

I have a simple activity that spins an arrow to a random angle, and when the arrow stops spinning it flashes. The flashing effect is an alpha animation applied to an imageview of a lit version of the arrow.
It works just fine in Lollipop and above, but in KitKat when the alpha animation is applied it negates the setFillAfter of the completed rotation animation and the arrows instantly change to pointing straight up (0 degrees)
public void Spin2 ()
{
spinning = true;
degreeEnd = rnd.nextInt(360);
RotateAnimation spinIt = new RotateAnimation(0,3600+degreeEnd, RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
spinIt.setDuration(6000);
spinIt.setFillAfter(true);
spinIt.setInterpolator(new DecelerateInterpolator());
spinIt.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
arrowLit.setVisibility(View.VISIBLE);
animBlink = new AlphaAnimation(1,0);
animBlink.setDuration(500);
animBlink.setInterpolator(new AccelerateInterpolator() );
animBlink.setRepeatCount(Animation.INFINITE);
// in KitKat this animation seems to reset the rotation..
arrowLit.startAnimation(animBlink);
spinning = false;
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
theSpinner.startAnimation(spinIt);
}
Interestingly, when you start the spinning again it flicks back to where it should be (degreeEnd) and starts the spin from there.
It is like it is temporarily overriding the setFillAfter of the rotation. I have tried various combinations of setFillBefore etc on the animations. Also, if I set the blinking animation to a finite number (eg 4) it will stop and stay at the zero - it won't correct until i re-spin.
Again, this works just fine in API 20 and up.
Thanks for any help
Andy
It seems to be something to do with the Frame Layout. Once it was out of the equation it works fine on KitKat.

Android: How to create bed design in xml?

Actually i am able to create this design by using RotateAnimation but getting problem when i want to change it and applying any animation.We know, Animation does not alter the actual properties of the View, it only animates it. So every time i am facing the issue like :
animation get back to its real state after finishing the animation and
start every time from initial position.
So to avoid this i want to apply static design in xml . I tried using android:rotation but not getting as per the exact requirement.
suggestions and helps will be mostly appreciable. Please suggest.
I know android:fillAfter="true" causes many problems!
Another option would be you could add animation listeners to the animation and once that animation finished, you manually set final transfomrations to the views.
yourAnimation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
/* Here, you could set them manually */
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});

Android fragment animation doesn't actually finish

I encountered this annoying problem, where I have 2 fragments, and I am running 3d flip animation on Fragments getView() component. Problem comes here, when the flip is done, I try to set another view invisible, but nothing happens. It seems that the animation is still on for the view and if I setAnimation(null) or clearAnimation for the view, the whole view is reset to start state.
I also did a little test, with just running alpha animation for the view and after that I cannot change the view visibility anymore. Any solution or hack for this kind of problem?
Thanks.
Here is small code snippet to reproduce problem.
Following code is ran when Fragment onCreateView has been called and button is clicked:
AlphaAnimation fadeHalf = new AlphaAnimation(1, 0.5f);
fadeHalf.setFillAfter(true);
fadeHalf.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
getView().setVisibility(View.GONE);
});
getView().startAnimation(fadeHalf);
So when running the code above, whole fragment view gets translated to 0.5f alpha, but setVisilibity(View.GONE) does nothing.
not sure if this is what you need but you can probably remove the view and add it back on when you do the same animation in the future.

How to check to see if animation is running before starting new animation?

I have a activity where I have multiple ImageViews and when you click on it the ImageView will fade out and fade back in. What I'm trying to figure out is how I can click one ImageView and start the animation and when I click a 2nd one and the animation is still running it will ignore the second one. I think I need to do something with the animationListener but I can't figure out how to use that to check if the animation is running or not before I initiate a new animation. I could have sworn I saw an example that did this but I've been looking for days and can't find it anymore, I'm hoping someone would be able to help out here..... below is the code for my animation:
// Create Animation
protected void fadeAnimation() {
tempImg.startAnimation(fadeout);
//Allow animation to finish
mHandler.postDelayed(new Runnable() {
public void run() {
tempImg.startAnimation(fadein);
}
}, 1000);
}
I'm assuming both fadeout and fadein are Animation objects.
Use fadeout.hasEnded() to check if the first has finished before starting your second one.
For more details about the Animation class, see here:
http://developer.android.com/reference/android/view/animation/Animation.html
Instead of having to loop possibly in another thread checking if an animation has ended, you could use an animation listener, doing something like this:
// Create Animation
protected void fadeAnimation() {
fadeout.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
tempImg.startAnimation(fadein);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
};
tempImg.startAnimation(fadeout);
}
With this kind of solution you wouldn't need to be actively checking if an animation has finished and time it with the duration of the previous animation.
The onAnimationEnd(Animation) is fired right after the animation has ended. This also solves the issue of users with developer options "on" and animation speed set to "off".

Android: PopupWindow Animation - Need to know when the animation has ended

I've got a PopupWindow that's using an alpha animation to produce a fade-in display of the window.
Using PopupWindow.setAnimationStyle() works as expected: the popup fades-in when shown.
However, once the popup is displayed (meaning the fade-in animation has completed) I'd like to kick off another animation.
I've tried using the following to obtain the underlying animation that's referred to via setAnimationStyle() and attach an AnimationListener to it:
Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.popup_fade_in);
fadeInAnimation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
Log.d(TAG, "fade-in animation START");
}
#Override
public void onAnimationEnd(Animation animation) {
Log.d(TAG, "fade-in animation END");
// Kick off the next animation
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
This does't work: None of the AnimationListener methods ever get called.
Anyone know of a way to determine when a popup window's animation has ended?
Alternatively, if there's a way to determine when the popup window has initially become visible I could kick off the secondary animation at that time. Unfortunately, I'm not finding anything in the API docs indicating how to do this.
Appreciate the help!

Categories

Resources