Android Open Source - turbo-editor Floating Action Button






From Project

Back to project page turbo-editor.

License

The source code is released under:

GNU General Public License

If you think the Android project turbo-editor 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 com.faizmalkani.floatingactionbutton;
// w  w  w  .j a  v a 2s.  c  o  m
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.AbsListView;

import com.nineoldandroids.animation.ObjectAnimator;
import com.nineoldandroids.view.ViewHelper;

public class FloatingActionButton extends View {

    private final Interpolator mInterpolator = new AccelerateDecelerateInterpolator();
    private final Paint mButtonPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private final Paint mDrawablePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Bitmap mBitmap;
    private int mColor;
    private boolean mHidden = false;
    /**
     * The FAB button's Y position when it is displayed.
     */
    private float mYDisplayed = -1;
    /**
     * The FAB button's Y position when it is hidden.
     */
    private float mYHidden = -1;

    public FloatingActionButton(Context context) {
        this(context, null);
    }

    public FloatingActionButton(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }


    public FloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.FloatingActionButton);
        mColor = a.getColor(R.styleable.FloatingActionButton_colour, Color.WHITE);
        mButtonPaint.setStyle(Paint.Style.FILL);
        mButtonPaint.setColor(mColor);
        float radius, dx, dy;
        radius = a.getFloat(R.styleable.FloatingActionButton_shadowRadius, 10.0f);
        dx = a.getFloat(R.styleable.FloatingActionButton_shadowDx, 0.0f);
        dy = a.getFloat(R.styleable.FloatingActionButton_shadowDy, 3.5f);
        int color = a.getInteger(R.styleable.FloatingActionButton_shadowColor, Color.argb(100, 0, 0, 0));
        mButtonPaint.setShadowLayer(radius, dx, dy, color);

        Drawable drawable = a.getDrawable(R.styleable.FloatingActionButton_drawable);
        if (null != drawable) {
            mBitmap = ((BitmapDrawable) drawable).getBitmap();
        }
        setWillNotDraw(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        WindowManager mWindowManager = (WindowManager)
                context.getSystemService(Context.WINDOW_SERVICE);
        Display display = mWindowManager.getDefaultDisplay();
        Point size = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            display.getSize(size);
            mYHidden = size.y;
        } else mYHidden = display.getHeight();
    }

    public static int darkenColor(int color) {
        float[] hsv = new float[3];
        Color.colorToHSV(color, hsv);
        hsv[2] *= 0.8f;
        return Color.HSVToColor(hsv);
    }

    public void setColor(int color) {
        mColor = color;
        mButtonPaint.setColor(mColor);
        invalidate();
    }

    public void setDrawable(Drawable drawable) {
        mBitmap = ((BitmapDrawable) drawable).getBitmap();
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, (float) (getWidth() / 2.6), mButtonPaint);
        if (null != mBitmap) {
            canvas.drawBitmap(mBitmap, (getWidth() - mBitmap.getWidth()) / 2,
                    (getHeight() - mBitmap.getHeight()) / 2, mDrawablePaint);
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        // Perform the default behavior
        super.onLayout(changed, left, top, right, bottom);

        // Store the FAB button's displayed Y position if we are not already aware of it
        if (mYDisplayed == -1) {

            mYDisplayed = ViewHelper.getY(this);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int color;
        if (event.getAction() == MotionEvent.ACTION_UP) {
            color = mColor;
        } else {
            color = darkenColor(mColor);
        }
        mButtonPaint.setColor(color);
        invalidate();
        return super.onTouchEvent(event);
    }

    public void hide(boolean hide) {
        // If the hidden state is being updated
        if (mHidden != hide) {

            // Store the new hidden state
            mHidden = hide;

            // Animate the FAB to it's new Y position
            ObjectAnimator animator = ObjectAnimator.ofFloat(this, "y", mHidden ? mYHidden : mYDisplayed).setDuration(500);
            animator.setInterpolator(mInterpolator);
            animator.start();
        }
    }

    public void listenTo(AbsListView listView) {
        if (null != listView) {
            listView.setOnScrollListener(new DirectionScrollListener(this));
        }
    }
}




Java Source Code List

com.faizmalkani.floatingactionbutton.BuildConfig.java
com.faizmalkani.floatingactionbutton.BuildConfig.java
com.faizmalkani.floatingactionbutton.DirectionScrollListener.java
com.faizmalkani.floatingactionbutton.FloatingActionButton.java
com.maskyn.fileeditor.AdsHelper.java
com.maskyn.fileeditor.ApplicationTest.java
com.maskyn.fileeditor.HomeActivity.java
com.maskyn.fileeditorpro.ApplicationTest.java
com.maskyn.fileeditorpro.HomeActivity.java
org.sufficientlysecure.rootcommands.Mount.java
org.sufficientlysecure.rootcommands.Remounter.java
org.sufficientlysecure.rootcommands.RootCommands.java
org.sufficientlysecure.rootcommands.Shell.java
org.sufficientlysecure.rootcommands.SystemCommands.java
org.sufficientlysecure.rootcommands.Toolbox.java
org.sufficientlysecure.rootcommands.command.Command.java
org.sufficientlysecure.rootcommands.command.ExecutableCommand.java
org.sufficientlysecure.rootcommands.command.SimpleCommand.java
org.sufficientlysecure.rootcommands.command.SimpleExecutableCommand.java
org.sufficientlysecure.rootcommands.util.BrokenBusyboxException.java
org.sufficientlysecure.rootcommands.util.Log.java
org.sufficientlysecure.rootcommands.util.RootAccessDeniedException.java
org.sufficientlysecure.rootcommands.util.UnsupportedArchitectureException.java
org.sufficientlysecure.rootcommands.util.Utils.java
sharedcode.turboeditor.ApplicationTest.java
sharedcode.turboeditor.activity.MainActivity.java
sharedcode.turboeditor.activity.SelectFileActivity.java
sharedcode.turboeditor.adapter.AdapterDetailedList.java
sharedcode.turboeditor.adapter.AdapterDrawer.java
sharedcode.turboeditor.adapter.AdapterTwoItem.java
sharedcode.turboeditor.application.MyApp.java
sharedcode.turboeditor.dialogfragment.AboutDialog.java
sharedcode.turboeditor.dialogfragment.ChangelogDialog.java
sharedcode.turboeditor.dialogfragment.EditTextDialog.java
sharedcode.turboeditor.dialogfragment.EncodingDialog.java
sharedcode.turboeditor.dialogfragment.FileInfoDialog.java
sharedcode.turboeditor.dialogfragment.FindTextDialog.java
sharedcode.turboeditor.dialogfragment.NewFileDetailsDialog.java
sharedcode.turboeditor.dialogfragment.NumberPickerDialog.java
sharedcode.turboeditor.dialogfragment.SaveFileDialog.java
sharedcode.turboeditor.iab.DonationAdapter.java
sharedcode.turboeditor.iab.DonationFragment.java
sharedcode.turboeditor.iab.DonationItems.java
sharedcode.turboeditor.iab.Donation.java
sharedcode.turboeditor.iab.utils.Base64DecoderException.java
sharedcode.turboeditor.iab.utils.Base64.java
sharedcode.turboeditor.iab.utils.IabException.java
sharedcode.turboeditor.iab.utils.IabHelper.java
sharedcode.turboeditor.iab.utils.IabResult.java
sharedcode.turboeditor.iab.utils.Inventory.java
sharedcode.turboeditor.iab.utils.Purchase.java
sharedcode.turboeditor.iab.utils.Security.java
sharedcode.turboeditor.iab.utils.SkuDetails.java
sharedcode.turboeditor.preferences.PreferenceHelper.java
sharedcode.turboeditor.preferences.SettingsFragment.java
sharedcode.turboeditor.root.LinuxShell.java
sharedcode.turboeditor.root.RootUtils.java
sharedcode.turboeditor.task.SaveFileTask.java
sharedcode.turboeditor.texteditor.EditTextPadding.java
sharedcode.turboeditor.texteditor.FileUtils.java
sharedcode.turboeditor.texteditor.LineUtils.java
sharedcode.turboeditor.texteditor.PageSystemButtons.java
sharedcode.turboeditor.texteditor.PageSystem.java
sharedcode.turboeditor.texteditor.Patterns.java
sharedcode.turboeditor.texteditor.SearchResult.java
sharedcode.turboeditor.util.AccessStorageApi.java
sharedcode.turboeditor.util.AlphanumComparator.java
sharedcode.turboeditor.util.AnimationUtils.java
sharedcode.turboeditor.util.AppInfoHelper.java
sharedcode.turboeditor.util.Build.java
sharedcode.turboeditor.util.Device.java
sharedcode.turboeditor.util.EventBusEvents.java
sharedcode.turboeditor.util.IHomeActivity.java
sharedcode.turboeditor.util.MimeTypes.java
sharedcode.turboeditor.util.PixelDipConverter.java
sharedcode.turboeditor.util.ProCheckUtils.java
sharedcode.turboeditor.util.ThemeUtils.java
sharedcode.turboeditor.util.ToastUtils.java
sharedcode.turboeditor.util.ViewUtils.java
sharedcode.turboeditor.util.systemui.SystemUiHelperImplHC.java
sharedcode.turboeditor.util.systemui.SystemUiHelperImplICS.java
sharedcode.turboeditor.util.systemui.SystemUiHelperImplJB.java
sharedcode.turboeditor.util.systemui.SystemUiHelperImplKK.java
sharedcode.turboeditor.util.systemui.SystemUiHelper.java
sharedcode.turboeditor.views.CustomDrawerLayout.java
sharedcode.turboeditor.views.DialogHelper.java
sharedcode.turboeditor.views.GoodScrollView.java