Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added shadow layer to emulate FAB behavior. #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions library/src/main/java/at/markushi/ui/CircleButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class CircleButton extends ImageView {
private static final int PRESSED_COLOR_LIGHTUP = 255 / 25;
private static final int PRESSED_RING_ALPHA = 75;
private static final int DEFAULT_PRESSED_RING_WIDTH_DIP = 4;
private static final int DEFAULT_SHADOW_X_OFFSET_DIP = 1;
private static final int DEFAULT_SHADOW_Y_OFFSET_DIP = 2;
private static final int ANIMATION_TIME_ID = android.R.integer.config_shortAnimTime;

private int centerY;
Expand All @@ -29,6 +31,15 @@ public class CircleButton extends ImageView {

private float animationProgress;

private float density = 3.0f;
private int shadowColor = Color.GRAY;
private int shadowXOffset = (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_SHADOW_X_OFFSET_DIP,
getResources().getDisplayMetrics());
private int shadowYOffset = (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_SHADOW_Y_OFFSET_DIP,
getResources().getDisplayMetrics());

private int pressedRingWidth;
private int defaultColor = Color.BLACK;
private int pressedColor;
Expand Down Expand Up @@ -66,6 +77,8 @@ public void setPressed(boolean pressed) {

@Override
protected void onDraw(Canvas canvas) {
circlePaint.setShadowLayer(getShadowRadius(), shadowXOffset, shadowYOffset, shadowColor);

canvas.drawCircle(centerX, centerY, pressedRingRadius + animationProgress, focusPaint);
canvas.drawCircle(centerX, centerY, outerRadius - pressedRingWidth, circlePaint);
super.onDraw(canvas);
Expand Down Expand Up @@ -117,6 +130,7 @@ private void init(Context context, AttributeSet attrs) {

circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
circlePaint.setStyle(Paint.Style.FILL);
setLayerType(LAYER_TYPE_SOFTWARE, circlePaint);

focusPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
focusPaint.setStyle(Paint.Style.STROKE);
Expand All @@ -129,6 +143,9 @@ private void init(Context context, AttributeSet attrs) {
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleButton);
color = a.getColor(R.styleable.CircleButton_cb_color, color);
pressedRingWidth = (int) a.getDimension(R.styleable.CircleButton_cb_pressedRingWidth, pressedRingWidth);
shadowColor = a.getColor(R.styleable.CircleButton_cb_shadowColor, shadowColor);
shadowXOffset = (int) a.getDimension(R.styleable.CircleButton_cb_shadowXOffset, shadowXOffset);
shadowYOffset = (int) a.getDimension(R.styleable.CircleButton_cb_shadowYOffset, shadowYOffset);
a.recycle();
}

Expand All @@ -138,6 +155,15 @@ private void init(Context context, AttributeSet attrs) {
final int pressedAnimationTime = getResources().getInteger(ANIMATION_TIME_ID);
pressedAnimator = ObjectAnimator.ofFloat(this, "animationProgress", 0f, 0f);
pressedAnimator.setDuration(pressedAnimationTime);

density = getResources().getDisplayMetrics().density;
}

private float getShadowRadius() {
if(isPressed()){
return 0.0f;
}
return density * 2.0f;
}

private int getHighlightColor(int color, int amount) {
Expand Down
3 changes: 3 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
<declare-styleable name="CircleButton">
<attr name="cb_color" format="color" />
<attr name="cb_pressedRingWidth" format="dimension" />
<attr name="cb_shadowColor" format="color" />
<attr name="cb_shadowXOffset" format="dimension" />
<attr name="cb_shadowYOffset" format="dimension" />
</declare-styleable>
</resources>