用于隐藏 View 而不改变可见性的 Android 翻译动画

标签 android android-layout android-animation

我需要动画方面的帮助。 (我的观点更复杂,我只向您展示我的布局 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>

我想显示和隐藏一些 View :

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

animation overview

我试过这样做,但总是有些地方不对:

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

显示

    <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);
        }
    });

等等...它的可见性非常好,但动画一点也不流畅(大多数时候它就像我没有使用动画一样工作)。当我停止更改可见性时,动画可以正常工作,但无法完成工作。

有什么想法吗?

最佳答案

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

关于用于隐藏 View 而不改变可见性的 Android 翻译动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24550752/

相关文章:

android - 使用 TabHost 时如何消除像素错误

Android ScrollView 高度不变

android - 为什么为带有动画的 View 设置可见性失败?

android - 与模拟器和 Web 相比,真实 Android 设备中的 Retrofit 响应时间更慢

java - 显示图像 - 错误 : incompatible types: ParseObject cannot be converted to ParseFile

android - 如何为 Firebase 创建通知主题并使用它?

java - 安卓 : How to draw driving path with location changing in new google map services

java - 如何将字符串转换为另一种类型以在函数中使用

android:如何从某个点开始动画

android - 动画对象离开屏幕并保持在那里