我正在尝试实现工具栏和用户图像动画,就像在 Twitter 的用户配置文件中使用的那样。
我尝试了很多东西,但是我无法将折叠的工具栏快速固定在屏幕顶部,并在它展开时具有一些背景,并使用户图像首先位于工具栏上方,然后缩小并移动到下方滚动时的工具栏。
twitter 是如何在 User Profile 图片中做出平滑效果的?他们如何首先将这个图像放在工具栏的前面,然后在滚动到后面并在工具栏下方实现平滑的效果?
我尝试了以下所有场景:
- 具有 CollapseParallaxMultiplier 视差效果的工具栏。
- Pin Toolbar 设置高度 100dp 和 minHeight ?attr/actionBarSize。
- 2个工具栏,一个带有图像背景,另一个带有透明背景颜色。
- 缩放 UserImage 然后平滑移动 Y 位置(无法实现滚动时将 User Image 发送到工具栏下方的效果)。
以前的场景对我来说都不是很好。
XML 层次结构:
<android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.AppBarLayout>
<android.support.design.widget.CollapsingToolbarLayout>
<LinearLayout>
<!--Some TextViews and ImageViews-->
</LinearLayout>
<ImageView src="My User profile Img"/> <!--Image first above toolbar and when toolbar is collapsing scale down and then go below toolbar-->
<ImageView src="My background" app:layout_collapseMode="parallax" app:layout_collapseParallaxMultiplier="0.8"/>
<android.support.v7.widget.Toolbar app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<!--Second Part, where the ViewPager should be pinned below the Toolbar-->
<NestedScrollView app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout>
<android.support.design.widget.TabLayout/>
<android.support.v4.view.ViewPager/>
</LinearLayout>
</NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
附上你可以看到 Twitter 对用户个人资料活动的影响。