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

Add CircleButtonText (same as CircleButton but extends TextView) #14

Open
wants to merge 3 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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,15 @@ Example Usage:

</FrameLayout>
```

You can use text for the button's content too.
```
<at.markushi.ui.CircleButtonText
android:layout_width="64dip"
android:layout_height="64dip"
android:text="A"
android:textSize="25sp"
android:textColor="#000000"
app:cb_color="#99CC00"
app:cb_pressedRingWidth="8dip" />
```
148 changes: 148 additions & 0 deletions library/src/main/java/at/markushi/ui/CircleButtonText.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package at.markushi.ui;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.TextView;

import at.markushi.circlebutton.R;

public class CircleButtonText extends TextView {

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 ANIMATION_TIME_ID = android.R.integer.config_shortAnimTime;

private int centerY;
private int centerX;
private int outerRadius;
private int pressedRingRadius;

private Paint circlePaint;
private Paint focusPaint;

private float animationProgress;

private int pressedRingWidth;
private int defaultColor = Color.BLACK;
private int pressedColor;
private ObjectAnimator pressedAnimator;

public CircleButtonText(Context context) {
super(context);
init(context, null);
}

public CircleButtonText(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}

public CircleButtonText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}

@Override
public void setPressed(boolean pressed) {
super.setPressed(pressed);

if (circlePaint != null) {
circlePaint.setColor(pressed ? pressedColor : defaultColor);
}

if (pressed) {
showPressedRing();
} else {
hidePressedRing();
}
}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawCircle(centerX, centerY, pressedRingRadius + animationProgress, focusPaint);
canvas.drawCircle(centerX, centerY, outerRadius - pressedRingWidth, circlePaint);
super.onDraw(canvas);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
centerX = w / 2;
centerY = h / 2;
outerRadius = Math.min(w, h) / 2;
pressedRingRadius = outerRadius - pressedRingWidth - pressedRingWidth / 2;
}

public float getAnimationProgress() {
return animationProgress;
}

public void setAnimationProgress(float animationProgress) {
this.animationProgress = animationProgress;
this.invalidate();
}

public void setColor(int color) {
this.defaultColor = color;
this.pressedColor = getHighlightColor(color, PRESSED_COLOR_LIGHTUP);

circlePaint.setColor(defaultColor);
focusPaint.setColor(defaultColor);
focusPaint.setAlpha(PRESSED_RING_ALPHA);

this.invalidate();
}

private void hidePressedRing() {
pressedAnimator.setFloatValues(pressedRingWidth, 0f);
pressedAnimator.start();
}

private void showPressedRing() {
pressedAnimator.setFloatValues(animationProgress, pressedRingWidth);
pressedAnimator.start();
}

private void init(Context context, AttributeSet attrs) {
this.setFocusable(true);
this.setGravity(Gravity.CENTER);
setClickable(true);

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

focusPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
focusPaint.setStyle(Paint.Style.STROKE);

pressedRingWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DEFAULT_PRESSED_RING_WIDTH_DIP, getResources()
.getDisplayMetrics());

int color = Color.BLACK;
if (attrs != null) {
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);
a.recycle();
}

setColor(color);

focusPaint.setStrokeWidth(pressedRingWidth);
final int pressedAnimationTime = getResources().getInteger(ANIMATION_TIME_ID);
pressedAnimator = ObjectAnimator.ofFloat(this, "animationProgress", 0f, 0f);
pressedAnimator.setDuration(pressedAnimationTime);
}

private int getHighlightColor(int color, int amount) {
return Color.argb(Math.min(255, Color.alpha(color)), Math.min(255, Color.red(color) + amount),
Math.min(255, Color.green(color) + amount), Math.min(255, Color.blue(color) + amount));
}
}