The library you're using includes the shadow in it's view, which means it's rotated along with the rest of the view when using setRotation
. You can get around this by wrapping your Floating Action Button in a FrameLayout, remove the fab_icon
attribute from your FAB, and add an ImageView on top of your FAB like so:
<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>
Then, instead of rotating the FAB, you rotate the ImageView. The result is that the FAB appears to rotate, because it's a circle and the icon inside it is rotating. Please note that the value of android:layout_marginTop
must be the negative value of fab_shadow_offset
for the icon to be perfectly centered. The default value is 3dp.
What you want to do now is combine Ashtons answer with a call to setRotation(float rotation)
, please note that the rotation is in degrees (0.0f
- 360.0f
).
A solution might look like this:
@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);
}