您正在使用的库在其视图中包含阴影,这意味着在使用setRotation
. 您可以通过将浮动操作按钮包装在 FrameLayout 中来解决此问题,fab_icon
从您的 FAB 中删除该属性,并在您的 FAB 顶部添加一个 ImageView,如下所示:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp">
<com.getbase.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fab_colorNormal="?attr/colorAccent"
app:fab_colorPressed="?attr/colorAccentHighlight"/>
<ImageView
android:id="@+id/fab_icon_overlay"
android:layout_width="@dimen/fab_icon_size"
android:layout_height="@dimen/fab_icon_size"
android:layout_gravity="center"
android:layout_marginTop="-3dp"
android:src="@drawable/ic_content_add"
android:tint="@android:color/white"/>
</FrameLayout>
然后,不是旋转 FAB,而是旋转 ImageView。结果是 FAB 似乎在旋转,因为它是一个圆圈,并且其中的图标正在旋转。请注意,要使图标完全居中, 的值android:layout_marginTop
必须是 的负值。fab_shadow_offset
默认值为 3dp。
您现在要做的是将 Ashtons 的回答与对 的调用结合起来setRotation(float rotation)
,请注意旋转以度数 ( 0.0f
- 360.0f
) 为单位。
解决方案可能如下所示:
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// positionOffset ranges from 0.0f to 1.0f, multiply it by 180.0f to rotate the
// icon clockwise, making the icon appear flipped when scrolled to the last page.
// Multiply by -180.0f to rotate counter-clockwise.
fabIconOverlay.setRotation(positionOffset * 180.0f);
}