【问题标题】:Animate view to new location after another view setVisibility(GONE)在另一个视图 setVisibility(GONE) 之后将视图动画到新位置
【发布时间】:2012-06-28 14:47:18
【问题描述】:

我想在布局中显示 3 个Views,但一次只能显示 2 个。当我按下一个按钮时,我希望最左边的视图向左滑出,中间的视图随之滑动,占用左视图的起始空间,最右边的视图应该滑入屏幕。

以下是一些截图:

动画前:

在动画期间(绘画编辑^^):

动画后:

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="3" >

    <FrameLayout
        android:id="@+id/stations_stations"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="20dp"
        android:layout_weight="1"
        android:background="@drawable/fragment_border" />

    <FrameLayout
        android:id="@+id/stations_singlestation"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="20dp"
        android:layout_weight="2"
        android:background="@drawable/fragment_border" />

    <FrameLayout
        android:id="@+id/stations_trip"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="20dp"
        android:layout_weight="1"
        android:background="@drawable/fragment_border"
        android:visibility="gone" />

</LinearLayout>

所以我可以使用下面的代码将最左边的视图设置为动画:

    final View stationsContainer = findViewById(R.id.stations_stations);
    Animation an = AnimationUtils.loadAnimation(this, R.anim.slide_out_left);
    stationsContainer.startAnimation(an);
    an.setAnimationListener(new 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) {
            stationsContainer.setVisibility(View.GONE);
        }
    });

这会将最左边的视图动画化到左侧窗口之外,并将其设置为消失。我也可以将中间视图动画到新位置,但是当动画完成时,中间视图会在很短的时间内跳回其原始位置,之后它会占据正确的位置。我怎样才能避免这种跳跃?

【问题讨论】:

  • 我认为您可以使用 LinearLayout,然后按下按钮将第一个的可见性设置为 View.GONE。然后,当您想要它回来时,您可以将其设置回 View.VISIBLE。不知道,不过,这将如何与动画一起使用。
  • 这确实是我现在所拥有的,但它不是一个漂亮的景象。
  • 由于您实际上并没有显示导致您出现问题的代码,因此任何人都很难真正帮助您。

标签: android android-animation


【解决方案1】:
  • 当子视图的可见性在VISIBLEGONE 之间变化时,使用LayoutTransistion 类自动为子视图设置动画。此课程从API 11 开始提供。

    有关示例代码,请查看 sdk 附带的示例。确切路径是ApiDemos\src\com\example\android\apis\animation\LayoutAnimationsHideShow.java

  • 使用LayoutAnimationController 类在将视图添加到视图组或从视图组中删除视图时为其设置动画。在这里,您必须保留对已删除视图的引用,因为您想稍后再添加它。此类适用于所有 Android 版本。

    示例代码路径:ApiDemos\src\com\example\android\apis\view\LayoutAnimation

【讨论】:

  • 太棒了。我正在使用LayoutTransition 类,你唯一需要做的就是setLayoutTransition(new LayoutTransition())。谢谢!
【解决方案2】:

使用animation 滑入和滑出。您需要在资源中将动画定义为 xml。

See this for example

或者您可以使用ViewFlipper Try this as an example

在 SO 和 web 上有更多示例。

【讨论】:

    【解决方案3】:

    将您的 FrameLayouts 添加到 ViewFlipper 中。例如

    <ViewFlipper>
     <FrameLayout1/>
     <FrameLayout2/>
     <FrameLayout3/>
    </ViewFlipper>
    

    然后在您的代码中调用 setInAnimation(leftInAnim) 和 setOutAnimation(leftOutAnim) 到 viewFlipper,并调用 showNext(),这将导致动画应用于当前视图和下一个视图。

    leftInAnim 和 leftOutAnim 是一个 XML 动画资源,告诉系统如何动画当前视图:例如

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

    这将导致视图从右侧(视图外)滑入当前视图

    【讨论】:

    • 但是我可以同时显示 2 个视图吗?现在视图只是自己出现了。
    • 我已经编辑了我的问题。我认为ViewFlipper 并不是我所需要的
    【解决方案4】:

    只需将android:animateLayoutChanges=true 添加到包含所有视图的LinearLayout,您就会得到想要的结果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2017-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多