Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android View.bringToFront() causes flicker

I have several Views within a FrameLayout. There is a transition I have written where each view has a custom Animation class applied. During this transition, I need to bring the View at the bottom of the z-order to the front. I do this with:

    public static void putBackAtFront(ViewGroup v) 
    {
        v.getChildAt(0).bringToFront();
        refreshEverything(v);
    }

This gets called from within the applyTransformation() of my custom Animation.

i.e.

public class PivotAnimation extends Animation {
    private View view;
    ...
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) 
    {
        ...
        if(interpolatedTime >= 1.f && isAtBack(view))
        {
           putBackAtFront(view);
        }
        ...
    }
    ...
}

refreshEverything() calls invalidate() and requestLayout() on the parent FrameLayout and all its children.

Everything works perfectly except that when putBackAtFront() is called, the View that is now at the bottom disappears for a single frame before instantly re-appearing, resulting in a noticeable flicker. I've also tried without calling refreshEverything(), it makes no difference.

I'm targeting API Level 7.

like image 320
Jarrod Smith Avatar asked Mar 25 '12 05:03

Jarrod Smith


Video Answer


1 Answers

applyTransformation() is called multiple times during the animation so you should do the animation based on the interpolatedTime, when its 1.0, the animation is at the end, so you should call your bring to front here.

So in my opinion this flicker happens when in some of these calls to your method, the getChildAt(0) gets a different view than the one you want and the flicker appears.

like image 113
Marcio Covre Avatar answered Oct 04 '22 23:10

Marcio Covre