1

我想Views在我的布局中显示 3 个,但一次只能显示 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);
        }
    });

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

4

4 回答 4

3
  • 每当子视图的可见性在和之间变化时,使用LayoutTransistion该类自动为子视图设置动画。该课程从以后开始提供。VISIBLEGONEAPI 11

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

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

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

于 2012-07-02T06:39:48.103 回答
0

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

例如看这个

或者您可以使用ViewFlipper Try this 作为示例

在 SO 和 web 上有更多的例子。

于 2012-06-28T14:57:38.307 回答
0

将您的 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>

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

于 2012-06-28T15:01:06.260 回答
-1

只需添加android:animateLayoutChanges=trueLinearLayout所有视图中,您将获得所需的结果

于 2018-04-25T09:58:17.377 回答