2014-09-25 14:19:23 +02:00
|
|
|
package com.faizmalkani.floatingactionbutton;
|
2014-10-12 18:11:19 +02:00
|
|
|
|
2014-09-25 14:19:23 +02:00
|
|
|
import android.content.Context;
|
2014-10-12 18:11:19 +02:00
|
|
|
import android.content.res.TypedArray;
|
2014-09-25 14:19:23 +02:00
|
|
|
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;
|
2014-10-12 18:11:19 +02:00
|
|
|
import android.os.Build;
|
2014-09-25 14:19:23 +02:00
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.Display;
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.WindowManager;
|
2014-10-12 18:11:19 +02:00
|
|
|
import android.view.animation.AccelerateDecelerateInterpolator;
|
|
|
|
import android.view.animation.Interpolator;
|
2014-09-25 14:19:23 +02:00
|
|
|
import android.widget.AbsListView;
|
|
|
|
|
2014-10-12 18:11:19 +02:00
|
|
|
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);
|
2014-09-25 14:19:23 +02:00
|
|
|
}
|
|
|
|
|
2014-10-12 18:11:19 +02:00
|
|
|
public FloatingActionButton(Context context, AttributeSet attributeSet) {
|
|
|
|
this(context, attributeSet, 0);
|
2014-09-25 14:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-12 18:11:19 +02:00
|
|
|
public FloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
|
|
super(context, attrs, defStyleAttr);
|
2014-09-25 14:19:23 +02:00
|
|
|
|
2014-10-12 18:11:19 +02:00
|
|
|
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.FloatingActionButton);
|
|
|
|
mColor = a.getColor(R.styleable.FloatingActionButton_color, Color.WHITE);
|
2014-09-25 14:19:23 +02:00
|
|
|
mButtonPaint.setStyle(Paint.Style.FILL);
|
2014-10-12 18:11:19 +02:00
|
|
|
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);
|
2014-09-25 14:19:23 +02:00
|
|
|
|
2014-10-12 18:11:19 +02:00
|
|
|
WindowManager mWindowManager = (WindowManager)
|
|
|
|
context.getSystemService(Context.WINDOW_SERVICE);
|
2014-09-25 14:19:23 +02:00
|
|
|
Display display = mWindowManager.getDefaultDisplay();
|
|
|
|
Point size = new Point();
|
2014-10-12 18:11:19 +02:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
|
|
|
|
display.getSize(size);
|
|
|
|
mYHidden = size.y;
|
|
|
|
} else mYHidden = display.getHeight();
|
|
|
|
}
|
2014-09-25 14:19:23 +02:00
|
|
|
|
2014-10-12 18:11:19 +02:00
|
|
|
public static int darkenColor(int color) {
|
|
|
|
float[] hsv = new float[3];
|
|
|
|
Color.colorToHSV(color, hsv);
|
|
|
|
hsv[2] *= 0.8f;
|
|
|
|
return Color.HSVToColor(hsv);
|
2014-09-25 14:19:23 +02:00
|
|
|
}
|
|
|
|
|
2014-10-12 18:11:19 +02:00
|
|
|
public void setColor(int color) {
|
|
|
|
mColor = color;
|
|
|
|
mButtonPaint.setColor(mColor);
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setDrawable(Drawable drawable) {
|
|
|
|
mBitmap = ((BitmapDrawable) drawable).getBitmap();
|
|
|
|
invalidate();
|
2014-09-25 14:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-10-12 18:11:19 +02:00
|
|
|
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);
|
2014-09-25 14:19:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-12 18:11:19 +02:00
|
|
|
@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) {
|
2014-09-25 14:19:23 +02:00
|
|
|
|
2014-10-12 18:11:19 +02:00
|
|
|
mYDisplayed = ViewHelper.getY(this);
|
2014-09-25 14:19:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-12 18:11:19 +02:00
|
|
|
@Override
|
|
|
|
public boolean onTouchEvent(MotionEvent event) {
|
|
|
|
int color;
|
|
|
|
if (event.getAction() == MotionEvent.ACTION_UP) {
|
|
|
|
color = mColor;
|
|
|
|
} else {
|
|
|
|
color = darkenColor(mColor);
|
2014-09-25 14:19:23 +02:00
|
|
|
}
|
2014-10-12 18:11:19 +02:00
|
|
|
mButtonPaint.setColor(color);
|
|
|
|
invalidate();
|
|
|
|
return super.onTouchEvent(event);
|
2014-09-25 14:19:23 +02:00
|
|
|
}
|
|
|
|
|
2014-10-12 18:11:19 +02:00
|
|
|
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();
|
|
|
|
}
|
2014-09-25 14:19:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void listenTo(AbsListView listView) {
|
|
|
|
if (null != listView) {
|
|
|
|
listView.setOnScrollListener(new DirectionScrollListener(this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|