0

我正在开发一个必须在 Android 2.2 及更高版本上运行的项目,我刚刚意识到我正在使用 View 的 3.0+ 方法。

我有一个菜单,可以在按下按钮时垂直滑入/滑出动画。动画完成后,我使用 setY() 方法更新视图位置。

我试图将其更改为 getTop/setTop 但它无法正常工作,我怀疑是因为 getY 实际上考虑了转换而 getTop 没有(我猜动画被视为转换)。

在不修改太多代码的情况下,Froyo 有什么简单的替代方案吗?

这是动画部分:

    animationSlideInDown = AnimationUtils.loadAnimation(this, R.anim.slide_out_down);
    animationSlideOutUp = AnimationUtils.loadAnimation(this, R.anim.slide_in_up);
    animationSlideInDown.setDuration(200);
    animationSlideOutUp.setDuration(200);
    animationSlideInDown.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
            if (menuInitialPosition == -1) {
                menuInitialPosition = menu.getY();
                menuHeight = menu.getHeight();
            } else {
                menu.setY(menuInitialPosition);
            }
        }
        @Override
        public void onAnimationRepeat(Animation animation) {}

        @Override
        public void onAnimationEnd(Animation animation) {
            menu.setY(menuInitialPosition);
        }
    });
    animationSlideOutUp.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {}

        @Override
        public void onAnimationRepeat(Animation animation) {}

        @Override
        public void onAnimationEnd(Animation animation) {
            menu.setY(-menuHeight);
        }
    });

这是 slide_out 过渡:

<translate
xmlns:android="http://schemas.android.com/apk/res/android"

android:fromYDelta="-100%"
android:toYDelta="0"
android:fillAfter="true"
android:fillEnabled="true"

android:duration="@android:integer/config_longAnimTime" />

谢谢!

4

1 回答 1

2

如果您想在屏幕上获得特定视图的位置,则可以使用:

int[] locationOnScreen = new int[2]; menu.getLocationOnScreen(locationOnScreen);

对于将视图设置到特定位置,您可以使用 LayoutParams 并设置视图的左边距和上边距。

于 2013-09-10T12:39:57.530 回答