ScaleAnimation仅应用于父视图

时间:2014-05-09 07:49:08

标签: android android-animation relativelayout

我在这个相对布局中有一个相对布局(父视图)和一些子视图(imageview和textview)。

我在父视图上应用缩放动画以获得放大效果。唯一的问题是,当缩放动画应用于父视图时,其子视图也会缩放。 有没有什么可以让孩子停止动画?

2 个答案:

答案 0 :(得分:0)

据我所知,您必须将父ViewGroup的图像/背景设置为单独的View,然后将动画应用于此以使其生效。

答案 1 :(得分:0)

管理这样做。不知道这是否是最好的方式,但它的工作原理并且看起来不错。我正在使用这里的动画:https://www.youtube.com/watch?v=CPxkoe2MraA,但是我有一个带有3个TextView的ViewGroup而不是图像。

我有一个带有3个TextView的蓝色容器:名称,地址和密钥。容器从GridView单元格的大小开始,并扩展到整个屏幕的宽度,从单元格的位置移动到屏幕的顶部。要在不调整TextViews大小的情况下执行此操作,我会调整调整大小的蓝色背景和不调整大小但仅进行翻译的ViewGroup。

在我的布局中,我将背景划分为一个单独的视图,并将ViewGroup的TextView显示在顶部(将它们包含在RelativeLayout中):

<RelativeLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/outter_margin">

    <View
        android:id="@+id/title_bg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/activity_login_application_name"
        android:layout_alignTop="@+id/container"
        android:layout_alignBottom="@+id/container"/>

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

        <TextView
            android:id="@+id/address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

        <TextView
            android:id="@+id/key"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

    </LinearLayout>
    ....
</RelativeLayout>

在活动中,我正在为两者制作动画。首先在onViewCreated中设置它们的起始尺寸和比例因子(有关更多详细信息,请参阅上面的链接):

ViewTreeObserver observer = titleBg.getViewTreeObserver();
observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

    @Override
    public boolean onPreDraw() {
        titleBg.getViewTreeObserver().removeOnPreDrawListener(this);

        // Figure out where the background view and full size versions are, relative
        // to the screen and each other
        int[] screenLocation = new int[2];
        titleBg.getLocationOnScreen(screenLocation);
        mLeftDelta = leftCoordinate - screenLocation[0];
        mTopDelta = topCoordinate - screenLocation[1];

        //set TextViews container a fix width
        final ViewGroup.LayoutParams params = container.getLayoutParams();
        params.width = width;
        container.setLayoutParams(params);

        // Scale factors View Group to make the large version the same size as the initial one from GridView, width scale is 1
        mWidthScale = (float) 1;
        mHeightScale = (float) height / container.getHeight();

        // Scale factors background View to make the large version the same size as the initial one from GridView
        mWidthScaleBG = (float) width / titleBg.getWidth();
        mHeightScaleBG = (float) height / titleBg.getHeight();

        runEnterAnimation();

        return true;
    }
});

带动画:

public void runEnterAnimation() {
    // Set starting values for properties we're going to animate. These
    // values scale and position the full size version down to the thumbnail
    // size/location, from which we'll animate it back up
    container.setPivotX(0);
    container.setPivotY(0);
    container.setScaleX(mWidthScale);
    container.setScaleY(mHeightScale);
    container.setTranslationX(mLeftDelta);
    container.setTranslationY(mTopDelta);

    titleBg.setPivotX(0);
    titleBg.setPivotY(0);
    titleBg.setScaleX(mWidthScaleBG);
    titleBg.setScaleY(mHeightScaleBG);
    titleBg.setTranslationX(mLeftDelta);
    titleBg.setTranslationY(mTopDelta);

    // Animate scale and translation of Bg to go from initial to full size
    titleBg.animate().setDuration(duration).
           scaleX(1).scaleY(1).
           translationX(0).translationY(0).
           setInterpolator(sDecelerator);

    // Animate scale and translation of ViewGroup to go from initial to full size
    container.animate().setDuration(duration).
           scaleX(1).scaleY(1).
           translationX(0).translationY(0).
           setInterpolator(sDecelerator).
           withEndAction(new Runnable() {
                  public void run() {
                        .....
                  }
           });

}

public void runExitAnimation(final Runnable endAction) {

     final boolean fadeOut;
     if (getResources().getConfiguration().orientation != orientation) {
         container.setPivotX(width / 2);
         container.setPivotY(container.getHeight() / 2);

         titleBg.setPivotX(titleBg.getWidth() / 2);
         titleBg.setPivotY(titleBg.getHeight() / 2);

         mLeftDelta = 0;
         mTopDelta = 0;
         fadeOut = true;
     } else {
         fadeOut = false;
     }

    // Animate ViewGroup back to initial size/location
    container.animate().setDuration(duration).
              scaleX(mWidthScale).scaleY(mHeightScale).
              translationX(mLeftDelta).translationY(mTopDelta).
              withEndAction(endAction);

    titleBg.animate().setDuration(duration).
              scaleX(mWidthScaleBG).scaleY(mHeightScaleBG).
              translationX(mLeftDelta).translationY(mTopDelta);
    if (fadeOut) {
        container.animate().alpha(0);
        titleBg.animate().alpha(0);
    }

}