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

sle*_*dav 10 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();
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

我的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>
Run Code Online (Sandbox Code Playgroud)

Sim*_*mon 36

之前的答案需要使父项无效的原因是因为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();
Run Code Online (Sandbox Code Playgroud)


Mic*_*hal 27

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

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();
Run Code Online (Sandbox Code Playgroud)