效果:
滑动列表的时候,要渐变图片的透明度。原本是使用高斯模糊,但是高斯模糊会有大量的计算,导致页面卡顿。现在实现思路是图片上层放一张白色完全透明图片,当滑动的时候,根据滑动的距离和图片的高度计算出滑动的比例,同时设置蒙层的透明度来达到渐变的效果。
1.布局
<android.support.design.widget.CoordinatorLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/tech_appbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true"
android:background="@android:color/transparent">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp">
<ImageView
android:id="@+id/iv_tech_blur"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax" />
<ImageView
android:id="@+id/iv_tech_origin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:background="#ffffff"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax" />
<TextView
android:id="@+id/tv_tech_copyright"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:textColor="@android:color/white"
android:textSize="14sp"
android:layout_margin="12dp"
/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/view_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>
2.appbarLayout 滑动偏移
ivOrigin.setAlpha(0f);
appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
Log.d(TAG, "onOffsetChanged: " + verticalOffset);
swipeRefresh.setEnabled(false);
int px = SystemUtil.dp2px(mContext, 256);
float rate = (float) Math.abs(verticalOffset) / px;
if (rate >= 0) {
ivOrigin.setAlpha((float) (rate));
}
}
});