Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement setZ() for devices below API 21/Lollipop?

Tags:

android

I've succeeded in implementing a drag-slide animation with the setZ() function as follows:

dragAnimSet.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
        ((ViewGroup)sourceParent.getParent()).setClipChildren(false);
        for(int a = 0; a < ifvList.size(); a++)
        {
            if(a != sourceIndex && a != indexInListener)
            {
                ifvList.get(a).setZ(-20);
            }
        }
        if(sourceParent.indexOfChild(toReplace) != -1 && toReplaceParent.indexOfChild(source) != -1) {
            source.setZ(-10);
        } else {
            sourceParent.setZ(-10);
        }
    }

    @Override
    public void onAnimationEnd(Animation animation) {
        ((ViewGroup)sourceParent.getParent()).setClipChildren(true);
        for(int a = 0; a < ifvList.size(); a++)
            {
                if(a != sourceIndex && a != indexInListener)
                {
                    ifvList.get(a).setZ(0);
                }
            }
            source.setZ(0);
            sourceParent.setZ(0);
        }

    @Override
    public void onAnimationRepeat(Animation animation) {}
});

However, the app I'm using this in targets devices with a minimum API of 15, and the function setZ() only works for API 21 and above.

bringToFront() or changing the draw order won't work because it's a LinearLayout and the former will just bring the view to the end of the list.

How can I at least emulate the effect of setZ() for devices below API 21?

like image 331
Gensoukyou1337 Avatar asked Jul 12 '16 03:07

Gensoukyou1337


1 Answers

There is no elevation prior to Lollipop, hence no setZ(): the order of drawing is always in order of the views being added.

There is ViewCompat.setZ(), but obviously it doesn't do anything prior to Lollipop, just allows you to remove any version checks.

like image 139
ianhanniballake Avatar answered Oct 18 '22 17:10

ianhanniballake