How to rotate a float button without shadow shadow?

I rotate the FAB in this simple way:

fab.startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate)); 

rotate.xml:

 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="1000"/> </set> 

This works, but with the FAB its shadow rotates. But I only need FAB to rotate (or even an src image if there is any difference).

+7
android rotation android-animation floating-action-button
source share
3 answers

Have you tried to use the animate method provided by the Compat library? I also had the same problem when using the Animation utility

 final OvershootInterpolator interpolator = new OvershootInterpolator(); ViewCompat.animate(fab). rotation(135f). withLayer(). setDuration(300). setInterpolator(interpolator). start(); 
+18
source share
 public void rotateFabForward() { ViewCompat.animate(fab) .rotation(135.0F) .withLayer() .setDuration(300L) .setInterpolator(new OvershootInterpolator(10.0F)) .start(); } public void rotateFabBackward() { ViewCompat.animate(fab) .rotation(0.0F) .withLayer() .setDuration(300L) .setInterpolator(new OvershootInterpolator(10.0F)) .start(); } 
+8
source share

There is a completely different approach that works flawlessly for me (the one suggested in the accepted answer creates a cropped shadow on pre-L). Create an XML drawing and wrap your FAB icon in a <rotate> as follows:

 <?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="45"> <bitmap android:src="@drawable/ic_add_white_24dp"/> </rotate> 

Install this code on your FAB and animate either its level directly or the imageLevel property of the FAB itself; it goes from 0 to 10000. If you want an OvershootInterpolator , then set toDegrees to 90 and animate the level to 5000 so that it doesn't go out of bounds.

+2
source share

All Articles