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