动画完成后,android调整视图边界

时间:2015-06-26 10:32:32

标签: android animation views

我有一个水平线性布局,有三个孩子;夹在两个按钮之间的文本视图。

我已将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" />

和Unexpand:

<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展开时,按钮的可点击区域似乎仍然处于“未展开”状态 - 即比例为0,因此不匹配直到按钮实际在屏幕上的位置。

为了尝试解决此问题,我考虑删除fillEnabled和fillAfter标记,并向动画添加AnimationWatcher - 在动画完成时将比例设置为适当的值。然而,这会导致轻微的“轻弹”效果,视图会在动画结束后再次展开之前恢复原始大小。

2 个答案:

答案 0 :(得分:1)

  

所有内容似乎都运行正常,但不幸的是,当LinearLayout展开时,按钮的可点击区域似乎处于“未展开”状态......

这是因为您在此处使用View animations。这是一个旧的动画系统,从一开始就在Android中,它的一个缺点是它改变了视图的外观(屏幕上的像素),而不是它的实际尺寸/位置。如果您不仅要更改外观,还要更改View的属性,请改用Property animation framework

答案 1 :(得分:1)

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

但如果您正在为&gt; Honeycomb开发,那么有一种简单的方法: ObjectAnimator

  

引自http://developer.android.com/

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

通过使用它,您将不需要使用animationlisteners。