Skip to content

Instantly share code, notes, and snippets.

@pengrad
Last active October 16, 2015 22:01
Show Gist options
  • Save pengrad/9db82de705b58b10c5e9 to your computer and use it in GitHub Desktop.
Save pengrad/9db82de705b58b10c5e9 to your computer and use it in GitHub Desktop.
Rotating FAB without rotating shadow
package com.github.pengrad.podcasts.ui;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import com.github.pengrad.podcasts.R;
import butterknife.Bind;
import butterknife.ButterKnife;
/**
* Stas Parshin
* 15 October 2015
*/
public class RotatingFab extends FrameLayout {
@Bind(R.id.fab) View mFab;
@Bind(R.id.fabImage) View mFabImage;
public RotatingFab(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
ButterKnife.bind(this);
mFabImage.setVisibility(GONE);
}
@Override
public void startAnimation(Animation animation) {
mFab.setEnabled(false);
if (Build.VERSION.SDK_INT >= 21) {
mFab.startAnimation(animation);
} else {
mFabImage.setVisibility(VISIBLE);
// need post() to image could measure its size
mFabImage.post(() -> mFabImage.startAnimation(animation));
}
}
@Override
public void clearAnimation() {
if (Build.VERSION.SDK_INT >= 21) {
mFab.clearAnimation();
} else {
mFabImage.clearAnimation();
mFabImage.setVisibility(GONE);
}
mFab.setEnabled(true);
}
}
<com.github.pengrad.podcasts.ui.RotatingFab
android:id="@+id/rotatingFab"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:src="@drawable/ic_autorenew_white_24dp"/>
<ImageView
android:id="@+id/fabImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/accent"
android:src="@drawable/ic_autorenew_white_24dp"
android:visibility="visible"/>
</com.github.pengrad.podcasts.ui.RotatingFab>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment