作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当我在 Android ScrollView 中滚动时,它会按预期向下滚动。
但我希望 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>
最佳答案
要实现像您发布的图像那样的视差效果,请尝试使用以下代码。这是一种非常简单的方法。
布局:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/rlWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/ivContactPhoto"
android:layout_width="match_parent"
android:layout_height="@dimen/contact_photo_height"
android:scaleType="centerCrop"
android:src="@drawable/stockguy" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/contact_photo_height"
android:layout_marginTop="250dp">
<!-- Other Views -->
</LinearLayout>
</RelativeLayout>
</ScrollView>
LinearLayout
的上边距等于 ImageViews
的高度。
监听 ScrollView
的滚动位置和改变 ImageView
的位置:
@Override
protected void onCreate(Bundle savedInstanceState) {
<...>
mScrollView = (ScrollView) findViewById(R.id.scrollView);
mPhotoIV = (ImageView) findViewById(R.id.ivContactPhoto);
mWrapperRL = (RelativeLayout) findViewById(R.id.rlWrapper);
mScrollView.getViewTreeObserver().addOnScrollChangedListener(new ScrollPositionObserver());
<...>
}
private class ScrollPositionObserver implements ViewTreeObserver.OnScrollChangedListener {
private int mImageViewHeight;
public ScrollPositionObserver() {
mImageViewHeight = getResources().getDimensionPixelSize(R.dimen.contact_photo_height);
}
@Override
public void onScrollChanged() {
int scrollY = Math.min(Math.max(mScrollView.getScrollY(), 0), mImageViewHeight);
// changing position of ImageView
mPhotoIV.setTranslationY(scrollY / 2);
// alpha you can set to ActionBar background
float alpha = scrollY / (float) mImageViewHeight;
}
}
希望对您有所帮助。
关于java - 如何在 Android 的 ScrollView 中将图像锚定到 Imageview 的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28043291/
我是一名优秀的程序员,十分优秀!