具有scale属性的ObjectAnimator会使bg变黑吗?

时间:2013-07-29 14:02:06

标签: android android-animation android-view

我使用ObjectAnimator缩小RelativeLayout:

            ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(view, "scaleX", 0.5f);
            ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(view, "scaleY", 0.5f);
            scaleDownY.setDuration(1000);
            AnimatorSet scaleDown = new AnimatorSet();
            scaleDown.play(scaleDownX).with(scaleDownY);
            scaleDown.start();

它按预期顺利缩小,但问题是现在较小视图周围的区域是黑色的,直到另一个用户操作,这是不合需要的。我希望它匹配缩放视图的父级的背景。任何想法如何使红场周围的区域立即具有正确的颜色?

enter image description here

我的xml:

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:gui="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/bg"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity$DummySectionFragment">
      <RelativeLayout
        android:id="@+id/res1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:tag="res1"
        android:background="@android:color/holo_red_dark"
        android:layout_alignTop="@+id/res2"
        android:layout_alignRight="@+id/include3"
        android:layout_marginRight="11dp">
            <TextView
                android:layout_marginLeft="15dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="res1"
                android:id="@+id/res1T"
                android:layout_centerVertical="true"
                android:layout_centerHorizontal="true"/>
        </RelativeLayout>
    </RelativeLayout>

3 个答案:

答案 0 :(得分:35)

之前的答案需要使父项无效的原因是因为AnimatorSet scaleDown没有Target。您应该将ObjectAnimator的Target设置为null并在AnimatorSet上设置Target。或者更好的方法是使用下面的代码:

ObjectAnimator scaleDown = ObjectAnimator.ofPropertyValuesHolder(view, 
    PropertyValuesHolder.ofFloat("scaleX", 0.5f),
    PropertyValuesHolder.ofFloat("scaleY", 0.5f));
scaleDown.setDuration(1000);
scaleDown.start();

答案 1 :(得分:26)

它可能不是最干净的解决方案,但添加动画更新侦听器并使父级无效可能会完成这项工作。

ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(view, "scaleX", 0.5f);
ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(view, "scaleY", 0.5f);
scaleDownX.setDuration(1000);
scaleDownY.setDuration(1000);

AnimatorSet scaleDown = new AnimatorSet();
scaleDown.play(scaleDownX).with(scaleDownY);

scaleDownX.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
     @Override
     public void onAnimationUpdate(ValueAnimator valueAnimator) {
         View p= (View) v.getParent();
         p.invalidate();
     });
scaleDown.start();

答案 2 :(得分:0)

如评论中所述,创建ObjectAnimator的方法是使用View的静态Property对象而不是传递字符串,因为使用字符串值涉及setter的反射以及可选的getter以获取属性的起始值。这可以从API 14

获得
ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(view, View.SCALE_X, 0.5f);
ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(view, View.SCALE_Y, 0.5f);

Google的工程师Chet Haase在DevBytes: Property Animations视频中可以找到完整的说明

相关问题