Android 如何使用协调行为实现视差效果?

Android 如何使用协调行为实现视差效果?,android,Android,我想实现视差效果的工具栏圆形图像一样 在这个视频中。但我想通过使用CoordinateLayout.behavior类来实现这一点。关于习惯行为有什么帮助吗 以下是我的xml: <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" x

我想实现视差效果的工具栏圆形图像一样 在这个视频中。但我想通过使用CoordinateLayout.behavior类来实现这一点。关于习惯行为有什么帮助吗

以下是我的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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.vats.vatishs.materialdesignlayouts.ScrollingActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

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

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay">

            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/text_margin"
            android:text="@string/large_text" />
    </android.support.v4.widget.NestedScrollView>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_dialog_email"
        android:layout_margin="10dp"
        app:layout_behavior="com.vats.vatishs.materialdesignlayouts.ImageViewBehaviour" />

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

这是我的代码:

public class ImageViewBehaviour extends CoordinatorLayout.Behavior<ImageView> {

public ImageViewBehaviour(Context context, AttributeSet attrs) {
}

@Override
public boolean layoutDependsOn(CoordinatorLayout parent, ImageView child, View dependency) {
    return dependency instanceof AppBarLayout;
}

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, ImageView child, View dependency) {
    /* What should i do here so that the image (that is displayed on the start of toolbar) will be moved along with toolbar expanded and fit on the center of appbar when toolbar is fully expanded? */
    return true;
}
}
公共类ImageViewBehavior扩展了CoordinatorLayout.Behavior{
公共图像视图行为(上下文、属性集属性){
}
@凌驾
public boolean layoutdependenson(协调布局父项、ImageView子项、视图依赖项){
返回AppBarLayout的依赖实例;
}
@凌驾
公共布尔onDependentViewChanged(协调布局父项、ImageView子项、视图依赖项){
/*我应该在这里做些什么,以便图像(显示在工具栏的开头)将随着工具栏的展开而移动,并在工具栏完全展开时适配在appbar的中心*/
返回true;
}
}

图像视图
放在
折叠工具栏布局
内,并将其放入
布局_collapseMode=“parallax”
。注意,
ImageView
是第一个子视图,因此它将被绘制在
工具栏的后面。使用固定的高度标注,而不是
wrap\u content
,或者可以使用
match\u parent
,因为
AppBarLayout
已经指定了固定的高度

<android.support.design.widget.CollapsingToolbarLayout ... >
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_collapseMode="parallax"
        ... />

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_collapseMode="pin"
        ... />

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


Acually我想将工具栏左上角的图像(当工具栏为操作栏大小时)移动到应用程序栏布局的中心(当工具栏展开时)@VatishSharma在这种情况下,请修改您的问题以使其更清晰。当你使用术语“视差”时,人们认为你指的是你链接到的视频中AppBar的背景。您所指的图像不是视差,而是缩放和转换。