android 动画结束后调整视图边界

android adjust view bounds after animation is finished

我有一个水平线性布局,其中包含三个 children;夹在两个按钮之间的文本视图。

我已将 LinearLayout 设置为可聚焦,将按钮设置为不可聚焦,并将以下 onFocusChangeListener 添加到 LinearLayout:

        public void onFocusChange(final View v, boolean hasFocus)
        {
            if(hasFocus)
            {
                v.startAnimation(_anims.Expand());
            }
            else
            {
                v.startAnimation(_anims.Unexpand());
            }
        }

动画如下:

展开:

<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fillEnabled="true"
android:fillAfter="true"
android:fromXScale="1.0"
android:toXScale="1.2"
android:fromYScale="1.0"
android:toYScale="1.1"
android:pivotX="50%"
android:pivotY="50%"
android:duration="200" />

和展开:

<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fillEnabled="true"
android:fromXScale="1.2"
android:toXScale="1.0"
android:fromYScale="1.1"
android:toYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="50" />

一切似乎都正常,但不幸的是,当 LinearLayout 展开时,按钮的可点击区域似乎仍显示在它们处于 'unexpanded' 状态时的位置 - 即比例为 0 , 因此与按钮在屏幕上的实际位置不匹配。

为了尝试解决这个问题,我想到了删除 fillEnabled 和 fillAfter 标签,并向动画添加 AnimationWatcher - 在动画完成时将比例设置为适当的值。然而,这会导致轻微的 'flick' 效果,在动画结束后视图会在再次展开之前返回到其原始大小。

Everything appears to work OK, but unfortunately when the LinearLayout is expanded, the clickable area of the buttons appear to be where they would be in their 'unexpanded' state...

发生这种情况是因为您使用的是 View animations here. That's an old animation system which was in Android since the beginning and one of its drawbacks is that it changes the appearance of the view (pixels on the screen), not its actual size/position. If you want to change not only appearance, but also View's properties, use Property animation framework

发生这种情况是因为 ViewAnimation 不会更改视图的实际位置和大小。您必须设置动画侦听器并相应地调整视图大小。

但是如果您正在为 >Honeycomb 开发,有一个简单的方法: ObjectAnimator

quote from http://developer.android.com/

ObjectAnimator anim = ObjectAnimator.ofFloat(foo, "scale", 0f, 1f);
anim.setDuration(1000);
anim.start();

通过使用它,您将不需要使用动画侦听器。