0

我是 android 动画的新手,并尝试使用 Property Animations。我正在尝试使用平移 x 属性来制作滑动效果。但我没有得到我想要的结果。

我的布局是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

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

        <LinearLayout
            android:id="@+id/first"
            android:layout_width="320dp"
            android:layout_height="200dp"
            android:background="#00ff00" >
        </LinearLayout>

        <LinearLayout
            android:id="@+id/second"
            android:layout_width="320dp"
            android:layout_height="200dp"
            android:background="#0000ff" >
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

根据布局。我在水平放置的父线性布局中有两个线性布局。并且只显示第一个布局,第二个布局不在屏幕上。我正在尝试为父 x 属性设置动画,以便使用翻译我将滑动父级,第一个将隐藏,第二个将显示。以下是我的代码:

    LinearLayout parent_;
    LinearLayout first_;
    LinearLayout second_;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    parent_ = (LinearLayout) findViewById(R.id.parent);
    first_ = (LinearLayout) parent_.findViewById(R.id.first);
    second_ = (LinearLayout) parent_.findViewById(R.id.second);
}

并且只是为了模拟我正在使用选项菜单的动画。

static boolean a = true;
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if(a){
        ObjectAnimator animate = ObjectAnimator.ofFloat(parent_, "x", -parent_.getWidth());
        animate.setDuration(500);
        animate.start();
        animate.addListener(this);
        animate.addUpdateListener(this);
        a = false;
    } else {
        ObjectAnimator animate = ObjectAnimator.ofFloat(parent_, "x", -parent_.getWidth(), 0);
        animate.setDuration(500);
        animate.start();

        a = true;
    }
    return super.onOptionsItemSelected(item);
}

当我启动应用程序时,我的应用程序如下所示:

在此处输入图像描述

在动画之后,应用程序看起来像这样:

在此处输入图像描述

注意:白色区域实际上是屏幕截图的一部分。背景为白色。

在动画之后而不是显示蓝色布局。它完全向左滑动并隐藏起来。并且不显示蓝色布局。我试图在动画期间拍摄屏幕截图。它看起来像这样:

在此处输入图像描述

我做错了什么???我希望我已经很好地解释了我的问题......

4

1 回答 1

2

在线性布局中,如果未显示子项,则不会对其进行布局,也不会成为测量的一部分(看起来像)。我必须使用 RelativeLayout 并将蓝色孩子隐藏在绿色孩子后面,这样它仍然是屏幕的一部分。它奏效了......

于 2012-11-25T16:41:29.980 回答