如何在API 21/Lollipop以下的设备上实现 setZ() 函数?

6

我已成功使用setZ()函数实现了拖动滑动动画,代码如下:

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) {}
});

然而,我在使用的应用程序面向最低API为15的设备,而函数setZ()仅适用于API 21及以上版本。

bringToFront()或更改绘制顺序不起作用,因为它是一个LinearLayout,前者只会将视图移到列表的末尾。

对于API 21以下的设备,如何至少模拟setZ()的效果?

1个回答

1
在Lollipop版本之前,没有高度(elevation)的概念,因此也没有setZ()方法:视图的绘制顺序总是按照添加的顺序进行的。
ViewCompat.setZ()方法,但显然在Lollipop版本之前它不起作用,只允许你删除任何版本检查。

我是这样使用它的吗:ViewCompat.setZ(<view_to_elevate>,<new_z_value>) - Gensoukyou1337
是的。请记住,z值以像素为单位,就像setZ()一样。 - ianhanniballake
ViewCompat.setTranslationZ() 做的事情和 setZ() 一样吗?因为当我查看我的版本的 ViewCompat 时,发现 setZ() 不存在。 - Gensoukyou1337
就像view.setZ()view.setTranslationZ()有着不同的功能一样,这两个也是如此。你应该尽快升级到最新的支持库(目前是24.0.0版本)。 - ianhanniballake
啊,这就解释了。我的支持库还停留在23.2.1版本。我应该同时安装24版本的构建工具吗? - Gensoukyou1337
如果您想使用24.0.0支持库,您需要版本为24的构建工具和API 24 SDK。 - ianhanniballake

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接