如何在Android中的滚动视图中将图像锚定到Imageview中的中心?

6 java android android-layout android-xml android-activity

当我在Android滚动视图中滚动时,它会向下滚动.

但我希望imageview中的图像能够锚定到中心.因此,当您向上滚动时,您总会看到图像中人物的脸部.

到目前为止,我还没有完成这个任务

在下图中创建了类似的效果:

在此输入图像描述

Stockguy图片:

在此输入图像描述

到目前为止我的代码(到目前为止还没有实现):

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:scrollbars="none"
    android:background="#FAFAFA"
    android:id="@+id/cScrollview"
    >

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="1100dp"
    tools:context=".MainActivity"
    android:id="@+id/CRLayout">

        <ImageView
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:layout_width="601dp"
            android:layout_height="250dp"
            android:paddingTop="0dp"
            android:paddingLeft="0dp"
            android:paddingRight="0dp"
            android:scaleType="centerCrop"
            android:id="@+id/contactPic"
            android:src="@drawable/stockguy"/>

            ....


            </RelativeLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

Muz*_*ant 1

我会采取不同的方法。

尝试根据以下代码片段构建布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
<ImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:background="#00FF00"
    android:scaleType="centerCrop"
    android:layout_height="200dp"/>
<FrameLayout
    android:id="@+id/content"
    android:layout_below="@+id/image"
    android:background="#FF0000"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

使用 ,GestureDetector.SimpleOnGestureListener您可以检测用户何时在内容布局上滚动,并相应地更新图像大小和 Alpha 以获得您所描述的效果。