简体   繁体   English

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

[英]Android : Translation Animation is not working correctly

I am new to animations in android and trying to work with Property Animations. 我是Android动画中的新手,并尝试使用Property Animations。 I am trying to do a sliding effect using translating the x property. 我正在尝试使用平移x属性来产生滑动效果。 but I am not getting the result i want. 但我没有得到想要的结果。

I have a layout that is : 我的布局是:

<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>

According to layout. 根据布局。 I have two linear layouts in a parent linear layout that are horizanlty placed. 我在水平放置的父线性布局中有两个线性布局。 and only first layout is being shown and second layout is out of screen. 并且仅显示第一个布局,而第二个布局不在屏幕上。 I am trying to animate the parent x property so that using the translation i will slide the parent and first will hide and second will show. 我正在尝试为父级x属性设置动画,以便使用翻译使父级滑动,第一个将隐藏,第二个将显示。 Following is my code: 以下是我的代码:

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

and just to simulate the animation I am using the option menu. 只是为了模拟动画,我正在使用选项菜单。

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

When I am starting the application my application looks like this : 当我启动应用程序时,我的应用程序如下所示:

在此处输入图片说明

and after animation the application looks like this : 动画后,应用程序如下所示:

在此处输入图片说明

NOTE: the white area is actually part of screenshot. 注意:白色区域实际上是屏幕截图的一部分。 the background in white. 白色的背景。

after animation the instead of showing the blue layout. 动画后,而不是显示蓝色布局。 it completely slides to the left and hides itself. 它完全向左滑动并隐藏。 and does not show the blue layout. 并且不显示蓝色布局。 I have tried to take a screen shot during the animation. 我尝试在动画过程中进行屏幕截图。 it looks like this : 它看起来像这样:

在此处输入图片说明

what i am doing wrong ??? 我在做什么错??? I hope i have explained my question well.... 我希望我已经很好地解释了我的问题。

in the linear layout, if the child is not being displayed, it is not going to be laid out and not going to be the part of the measurements ( as it looks like ). 在线性布局中,如果未显示子项,则不会对其进行布局,也不会成为测量的一部分(看起来像)。 I have to use the RelativeLayout and keep the blue child hidden behind the green one, so that it would still be the part of the screen. 我必须使用RelativeLayout并将蓝色的子项隐藏在绿色的子项后面,以便它仍然是屏幕的一部分。 and it worked ... 它奏效了...

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

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