Android Open Source - example Fab Activity






From Project

Back to project page example.

License

The source code is released under:

Apache License

If you think the Android project example listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package me.pratama.examplematerial.ui;
//  w  w w . j  a v a 2s  . co  m
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.Interpolator;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import me.pratama.examplematerial.BaseActivity;
import me.pratama.examplematerial.R;
import me.pratama.examplematerial.interpolator.CubicBezierInterpolator;

/**
 * <p>implement fab Expand
 * <p/>
 * <p/>
 * <p>Thanks +ChrisBasha</p>
 * <p>https://plus.google.com/+ChrisBasha</p>
 * <p/>
 * <p>
 * https://plus.google.com/+ChrisBasha/posts/awch8jDHawp
 * source : https://gist.github.com/chris95x8/882b5c5d0aa2096236ba
 * </p>
 */
public class FabActivity extends BaseActivity {

    @InjectView(R.id.btnFAB)
    ImageButton btnFAB;
    @InjectView(R.id.expanded_view)
    RelativeLayout expandedView;
    @InjectView(R.id.act_collapse)
    ImageButton btnCollapse;
    @InjectView(R.id.btn_action_1)
    ImageButton btnAct1;
    @InjectView(R.id.btn_action_2)
    ImageButton btnAct2;
    @InjectView(R.id.btn_action_3)
    ImageButton btnAct3;
    @InjectView(R.id.btn_action_4)
    ImageButton btnAct4;

    public static final int FAB_STATE_COLLAPSED = 0;
    public static final int FAB_STATE_EXPANDED = 1;

    public static int FAB_CURRENT_STATE = FAB_STATE_COLLAPSED;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fab);
        ButterKnife.inject(this);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @OnClick(R.id.btnFAB)
    public void onBtnFabClick() {
        revealView(expandedView);

        FAB_CURRENT_STATE = FAB_STATE_EXPANDED;
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                btnFAB.setVisibility(View.GONE);
            }
        }, 50);

        btnCollapse.animate().rotationBy(135).setDuration(250).start();
    }

    @OnClick(R.id.act_collapse)
    public void onCollapseBtnClick() {
        hideView(expandedView);

        FAB_CURRENT_STATE = FAB_STATE_COLLAPSED;

        btnCollapse.animate().rotationBy(-135).setDuration(250).start();

    }

    private void hideView(final View expandedView) {
        int cx = (btnFAB.getLeft() + btnFAB.getRight()) / 2;
        int cy = (btnFAB.getTop() + btnFAB.getBottom()) / 2;

        int initialRadius = expandedView.getWidth();

        Animator anim = ViewAnimationUtils.createCircularReveal(expandedView, cx, cy, initialRadius, 0);
        anim.setDuration(300);
        anim.setInterpolator(getLinearOutSlowInInterpolator());

        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                expandedView.setVisibility(View.INVISIBLE);
            }
        });

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                btnFAB.setVisibility(View.VISIBLE);
            }
        }, 200);

        anim.start();

    }

    private void revealView(final View expandedView) {
        int cx = (btnFAB.getLeft() + btnFAB.getRight()) / 2;
        int cy = (btnFAB.getTop() + btnFAB.getBottom()) / 2;

        int finalRadius = Math.max(expandedView.getWidth(), expandedView.getHeight());

        Animator anim =
                ViewAnimationUtils.createCircularReveal(expandedView, cx, cy, 0, finalRadius);
        anim.setDuration(300);

        expandedView.setVisibility(View.VISIBLE);

        slideView(btnAct1);
        slideView(btnAct2);
        slideView(btnAct3);
        slideView(btnAct4);

        anim.start();
    }

    //Animation to slide the action buttons
    public void slideView(View view) {
        ObjectAnimator slide = ObjectAnimator.ofFloat(view, View.TRANSLATION_X, 112, 0);
        slide.setDuration(500);
        slide.setInterpolator(getLinearOutSlowInInterpolator());
        slide.start();
    }

    public static Interpolator getLinearOutSlowInInterpolator() {
        //Decelerate Interpolator - For elements that enter the screen
        return new CubicBezierInterpolator(0, 0, 0.2, 1);
    }


    public static Interpolator getFastInSlowOutInterpolator() {
        //Ease In Out Interpolator - For elements that change position while staying in the screen
        return new CubicBezierInterpolator(0.4, 0, 0.2, 1);
    }

}




Java Source Code List

com.pratamawijaya.example_gson.ApplicationTest.java
com.pratamawijaya.example_gson.MainActivity.java
com.pratamawijaya.examplebuildflavor.ApplicationTest.java
com.pratamawijaya.examplebuildflavor.MainActivity.java
com.pratamawijaya.exampledynamicedittext.ApplicationTest.java
com.pratamawijaya.exampledynamicedittext.MainActivity.java
com.pratamawijaya.examplefragment.ApplicationTest.java
com.pratamawijaya.examplefragment.MyActivity.java
com.pratamawijaya.examplefragment.MyFragment.java
com.pratamawijaya.examplefusedlocation.ApplicationTest.java
com.pratamawijaya.examplefusedlocation.MainActivity.java
com.pratamawijaya.exampleinstagram.ApplicationTest.java
com.pratamawijaya.exampleinstagram.BaseActivity.java
com.pratamawijaya.exampleinstagram.BaseApplication.java
com.pratamawijaya.exampleinstagram.MainActivity.java
com.pratamawijaya.exampleinstagram.authentication.InstagramApp.java
com.pratamawijaya.exampleinstagram.authentication.InstagramWebviewAuthActivity.java
com.pratamawijaya.examplepallete.ApplicationTest.java
com.pratamawijaya.examplepallete.MainActivity.java
com.pratamawijaya.examplepallete.adapter.CoverAdapter.java
com.pratamawijaya.examplepallete.model.CoverItem.java
com.pratamawijaya.exampleparse.ApplicationTest.java
com.pratamawijaya.exampleparse.BaseApplication.java
com.pratamawijaya.exampleparse.MainActivity.java
com.pratamawijaya.examplerealmio.ApplicationTest.java
com.pratamawijaya.examplerealmio.InsertDataActivity.java
com.pratamawijaya.examplerealmio.MainActivity.java
com.pratamawijaya.examplerealmio.ReadDataActivity.java
com.pratamawijaya.examplerealmio.User.java
com.pratamawijaya.exampleretrofit.ApplicationTest.java
com.pratamawijaya.exampleretrofit.MyActivity.java
id.pratama.example.cardslib.MainActivity.java
id.pratama.example.facebooklogin.MainActivity.java
id.pratama.example.streamingaudio.CobaSaja.java
id.pratama.example.streamingaudio.MainActivity.java
id.pratama.example.streamingaudio.Test.java
id.pratama.example.streamingaudio.service.StreamService.java
id.pratama.example.streamingaudio.utils.Utils.java
id.pratama.example.volley.MainActivity.java
it.gmariotti.cardslib.library.Constants.java
it.gmariotti.cardslib.library.internal.CardArrayAdapter.java
it.gmariotti.cardslib.library.internal.CardArrayMultiChoiceAdapter.java
it.gmariotti.cardslib.library.internal.CardCursorAdapter.java
it.gmariotti.cardslib.library.internal.CardExpand.java
it.gmariotti.cardslib.library.internal.CardGridArrayAdapter.java
it.gmariotti.cardslib.library.internal.CardGridCursorAdapter.java
it.gmariotti.cardslib.library.internal.CardHeader.java
it.gmariotti.cardslib.library.internal.CardThumbnail.java
it.gmariotti.cardslib.library.internal.Card.java
it.gmariotti.cardslib.library.internal.ViewToClickToExpand.java
it.gmariotti.cardslib.library.internal.base.BaseCardArrayAdapter.java
it.gmariotti.cardslib.library.internal.base.BaseCardCursorAdapter.java
it.gmariotti.cardslib.library.internal.base.BaseCard.java
it.gmariotti.cardslib.library.internal.base.CardUIInferface.java
it.gmariotti.cardslib.library.internal.multichoice.DefaultOptionMultiChoice.java
it.gmariotti.cardslib.library.internal.multichoice.MultiChoiceAdapterHelperBase.java
it.gmariotti.cardslib.library.internal.multichoice.MultiChoiceAdapter.java
it.gmariotti.cardslib.library.internal.multichoice.OptionMultiChoice.java
it.gmariotti.cardslib.library.internal.overflowanimation.BaseCardOverlayAnimation.java
it.gmariotti.cardslib.library.internal.overflowanimation.BaseOverflowAnimation.java
it.gmariotti.cardslib.library.internal.overflowanimation.TwoCardOverlayAnimation.java
it.gmariotti.cardslib.library.utils.BitmapUtils.java
it.gmariotti.cardslib.library.utils.CacheUtil.java
it.gmariotti.cardslib.library.view.BaseCardView.java
it.gmariotti.cardslib.library.view.CardGridView.java
it.gmariotti.cardslib.library.view.CardListView.java
it.gmariotti.cardslib.library.view.CardView.java
it.gmariotti.cardslib.library.view.base.CardViewInterface.java
it.gmariotti.cardslib.library.view.component.CardHeaderView.java
it.gmariotti.cardslib.library.view.component.CardShadowView.java
it.gmariotti.cardslib.library.view.component.CardThumbnailView.java
it.gmariotti.cardslib.library.view.listener.SwipeDismissListViewTouchListener.java
it.gmariotti.cardslib.library.view.listener.SwipeDismissViewTouchListener.java
it.gmariotti.cardslib.library.view.listener.UndoBarController.java
it.gmariotti.cardslib.library.view.listener.UndoCard.java
me.pratama.examplebetterpickers.ApplicationTest.java
me.pratama.examplebetterpickers.MainActivity.java
me.pratama.examplelayoutforandroiddev.ApplicationTest.java
me.pratama.examplelayoutforandroiddev.MainActivity.java
me.pratama.examplelayoutforandroiddev.SoalActivity.java
me.pratama.examplelayoutforandroiddev.entity.Soal.java
me.pratama.examplematerial.ApplicationTest.java
me.pratama.examplematerial.BaseActivity.java
me.pratama.examplematerial.MainActivity.java
me.pratama.examplematerial.interpolator.CubicBezierInterpolator.java
me.pratama.examplematerial.model.Singer.java
me.pratama.examplematerial.ui.FabActivity.java
me.pratama.examplematerial.ui.RecyclerViewActivity.java
me.pratama.examplematerial.ui.adapter.RecyclerAdapter.java