Android XML中的视差效果

时间:2018-02-06 06:04:44

标签: android xml native parallax

我希望在Android XML中实现视差效果,并使用一些折叠的线性布局,如图所示。我怎样才能做到这一点?

enter image description here

任何人都可以帮忙。

2 个答案:

答案 0 :(得分:0)

在您的活动xml文件中,

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsingToolbarLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <Relative Layout  // Your Desired Parallax Layout Comes Here
                    android:id="@+id/ivParallax"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="parallax"
                    app:layout_collapseParallaxMultiplier="0.7" >

                    // put your image or what ever that should be parallax while scrolling

                   </RelativeLayout>

             // Your remaining widgets comes here

    </android.support.design.widget.CollapsingToolbarLayout>

   </android.support.design.widget.AppBarLayout>

</android.support.design.widget.CoordinatorLayout>

答案 1 :(得分:-1)

使用app:layout_collapseMode =“parallax”

<ImageView
        android:id="@+id/toolbarImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:scaleType="centerCrop"
        app:layout_collapseMode="parallax"/>