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 text, textSize, textColor attr support and subclass from ImageButton #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
71 changes: 62 additions & 9 deletions library/src/main/java/at/markushi/ui/CircleButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import at.markushi.circlebutton.R;

public class CircleButton extends ImageView {
public class CircleButton extends ImageButton {

private static final int PRESSED_COLOR_LIGHTUP = 255 / 25;
private static final int PRESSED_RING_ALPHA = 75;
Expand All @@ -34,6 +34,9 @@ public class CircleButton extends ImageView {
private int pressedColor;
private ObjectAnimator pressedAnimator;

private String text;
private Paint textPaint;

public CircleButton(Context context) {
super(context);
init(context, null);
Expand Down Expand Up @@ -68,6 +71,10 @@ public void setPressed(boolean pressed) {
protected void onDraw(Canvas canvas) {
canvas.drawCircle(centerX, centerY, pressedRingRadius + animationProgress, focusPaint);
canvas.drawCircle(centerX, centerY, outerRadius - pressedRingWidth, circlePaint);
if (textPaint != null) {
final float baselineOffset = (textPaint.descent() + textPaint.ascent()) / 2;
canvas.drawText(text, centerX, centerY - baselineOffset, textPaint);
}
super.onDraw(canvas);
}

Expand Down Expand Up @@ -125,14 +132,23 @@ private void init(Context context, AttributeSet attrs) {
.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);
float textSize = 22.0f;
int textColor = Color.WHITE;
if (attrs != null) {
final TypedArray a = context.obtainStyledAttributes(attrs, ATTRS);
color = a.getColor(attrValueIndex(R.attr.cb_color), color);
pressedRingWidth = (int) a.getDimension(attrValueIndex(R.attr.cb_pressedRingWidth), pressedRingWidth);
this.text = a.getString(attrValueIndex(android.R.attr.text));
textSize = a.getDimension(attrValueIndex(android.R.attr.textSize), textSize);
textColor = a.getColor(attrValueIndex(android.R.attr.textColor), textColor);
a.recycle();
}

setColor(color);

if (this.text != null) {
setText(text, textSize, textColor);
}

focusPaint.setStrokeWidth(pressedRingWidth);
final int pressedAnimationTime = getResources().getInteger(ANIMATION_TIME_ID);
Expand All @@ -144,4 +160,41 @@ 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));
}

private static final int[] ATTRS = new int[]{
android.R.attr.text,
android.R.attr.textColor,
android.R.attr.textSize,
R.attr.cb_color,
R.attr.cb_pressedRingWidth
};

static {
Arrays.sort(ATTRS);
}

private static int attrValueIndex(int attrIndex) {
for (int i = 0; i < ATTRS.length; i++) {
if (ATTRS[i] == attrIndex) {
return i;
}
}
return -1;
}

public String getText() {
return this.text;
}

public void setText(String text, float textSize, int textColor) {
this.textPaint = new Paint();
textPaint.setColor(textColor);
textPaint.setTextSize(textSize);
textPaint.setAntiAlias(true);
textPaint.setFakeBoldText(true);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setTextAlign(Paint.Align.CENTER);

this.invalidate();
}
}