You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tasks/src/main/java/com/faizmalkani/floatingactionbutton/FloatingActionButton.java

96 lines
3.2 KiB
Java

package com.faizmalkani.floatingactionbutton;
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.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import org.tasks.R;
public class FloatingActionButton extends View {
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;
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_tint, 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);
}
}
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
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);
}
}