繁体   English   中英

Android:翻译动画无法正常工作

[英]Android : Translation Animation is not working correctly

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

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

在此处输入图片说明

动画后,应用程序如下所示:

在此处输入图片说明

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

动画后,而不是显示蓝色布局。 它完全向左滑动并隐藏。 并且不显示蓝色布局。 我尝试在动画过程中进行屏幕截图。 它看起来像这样:

在此处输入图片说明

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM