【问题标题】:Android translation animation for hiding views without changing visiblility用于隐藏视图而不改变可见性的 Android 翻译动画
【发布时间】:2014-07-03 10:06:59
【问题描述】:

我需要动画方面的帮助。 (我的观点更复杂,我只向您展示我的布局 xml 的主要元素)

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" >

        <LinearLayout
            android:id="@+id/oneBigView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:gravity="center_vertical"
            android:orientation="vertical">


        </LinearLayout>

        <LinearLayout
            android:id="@+id/fourSmallViews"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:gravity="center_vertical"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weightSum="2" >

                <RelativeLayout
                    android:id="@+id/topLeft"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1" >                       
                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/topRight"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1">
                </RelativeLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="invisible"
                android:weightSum="2" >

                <RelativeLayout
                    android:id="@+id/bottomLeft"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1" >


                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/bottomRight"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1" >


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

我想显示和隐藏一些视图:

  1. 没有可见的视图
  2. topLeft 和 topRight 从边缘(topLeft 从左侧,topRight 从右侧)移到屏幕中心(父级)。
  3. 当 topLeft 和 topRight 达到其移动的一半时,bottomLeft 和 bottomRight 显示与上一步中的 topLeft 和 topRight 完全相同
  4. 所有四个视图都保持可见几秒钟。
  5. topLeft 和 topRight 从屏幕中心(父级)移到边缘(topLeft 到左,topRight 到右)。
  6. 当 topLeft 和 topRight 达到其移动的一半时,bottomLeft 和 bottomRight 移出与上一步中的 topLeft 和 topRight 完全相同
  7. 当所有视图消失时,一个BigView 从左边缘移动并填满屏幕。
  8. oneBigView 会停留几秒钟。
  9. oneBigView 从屏幕中心移动到左边缘。
  10. 当 oneBigView 消失时,一切都从 1 开始。

我试过这样做,但总是有问题:

  1. 我尝试添加一些翻译动画并更改可见性(我尝试了 View.INVISIBLE 和 VIEW.GONE)onAnimationEnd:

显示

    <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator">
    <translate 
    android:fromXDelta="-100%p" 
    android:toXDelta="0"
    android:duration="500" />
    </set>

隐藏

    <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator">
        <translate 
        android:fromXDelta="0"
        android:toXDelta="-100%p"
        android:duration="500" />

    </set>

.java

final Animation leftIn=AnimationUtils.loadAnimation(this, R.anim.show);
final Animation leftOut=AnimationUtils.loadAnimation(this, R.anim.hide);
leftIn.setAnimationListener(new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

            //start animation for showing topRight bottomLeft and bottomRight 
            //with propper startOffset
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // TODO Auto-generated method stub
            left1.setVisibility(View.VISIBLE);
            leftOut.setStartOffset(4000);
            left1.startAnimation(leftOut);

        }
    });
leftOut.setAnimationListener(new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // TODO Auto-generated method stub

            left1.setVisibility(View.INVISIBLE);
        }
    });

等等......它非常适合可见性,但动画一点也不流畅(大部分时间它就像我没有使用动画一样工作)。当我停止更改可见性时,动画可以正常工作,但不能正常工作。

有什么想法吗?

【问题讨论】:

    标签: android android-layout android-animation


    【解决方案1】:

    您可以尝试更改视图的 alpha 值,而不是使其不可见或消失。

    【讨论】:

      【解决方案2】:

      您应该尝试将fillAfter 添加到您的&lt;set&gt;,如下所示:

      <set 
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:interpolator="@android:anim/decelerate_interpolator"
          android:fillAfter="true">
      

      使用View.INVISIBLEView.VISIBLE 设置可见性通常不适用于动画。您唯一的其他选择是将 alpha 动画添加到 &lt;set&gt;。这是一个模板:

      <alpha
          android:fromAlpha="1.0"
          android:toAlpha="0.0"
          android:duration="200"
          android:interpolator="@android:anim/accelerate_interpolator"/>
      

      根据您的需要进行调整。避免设置可见性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-25
        • 2016-09-06
        • 2015-08-01
        • 2015-07-08
        • 1970-01-01
        • 2016-04-19
        • 1970-01-01
        • 2015-04-19
        相关资源
        最近更新 更多