|
|
@@ -31,13 +31,14 @@ public class SwitchTrackTextDrawable extends Drawable {
|
|
|
private String leftText;
|
|
|
private String rightText;
|
|
|
private Paint textPaint;
|
|
|
+ private Rect padding;
|
|
|
private boolean isChecked;
|
|
|
|
|
|
- public SwitchTrackTextDrawable(@NonNull Context context, @StringRes int leftTextId, @StringRes int rightTextId, boolean isChecked) {
|
|
|
+ public SwitchTrackTextDrawable(@NonNull Context context, @StringRes int leftTextId, @StringRes int rightTextId, boolean isChecked, Rect padding) {
|
|
|
this.context = context;
|
|
|
this.isChecked = isChecked;
|
|
|
- textPaint = doTextPaint();
|
|
|
- textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
|
|
|
+ textPaint = getTextPaint();
|
|
|
+ this.padding = padding;
|
|
|
|
|
|
if (isChecked) {
|
|
|
//switch on
|
|
|
@@ -52,12 +53,12 @@ public class SwitchTrackTextDrawable extends Drawable {
|
|
|
|
|
|
}
|
|
|
|
|
|
- private Paint doTextPaint() {
|
|
|
+ private Paint getTextPaint() {
|
|
|
Paint textPaint = new Paint();
|
|
|
textPaint.setAntiAlias(true);
|
|
|
textPaint.setStyle(Paint.Style.FILL);
|
|
|
- textPaint.setTextAlign(Paint.Align.CENTER);
|
|
|
textPaint.setTextSize(ResourceUtil.dpToPx(9));
|
|
|
+ textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
|
|
|
return textPaint;
|
|
|
}
|
|
|
|
|
|
@@ -65,26 +66,43 @@ public class SwitchTrackTextDrawable extends Drawable {
|
|
|
@Override
|
|
|
public void draw(@NonNull Canvas canvas) {
|
|
|
final Rect textBounds = new Rect();
|
|
|
+ int paddingTop = padding.top;
|
|
|
+ int paddingBottom = padding.bottom;
|
|
|
+ int paddingStart = padding.left;
|
|
|
+ int paddingEnd = padding.right;
|
|
|
+ Rect bound = canvas.getClipBounds();
|
|
|
int heightBaseline;
|
|
|
- int widthQuarter = canvas.getClipBounds().width() / 4;
|
|
|
+
|
|
|
+ float left = paddingStart;
|
|
|
+ float top = paddingTop + 3; // thumb 과 track 의 높이 차이 2/1 을 빼준다. => ResourceUtil.dpToFloat(1) 를 빼면 track 의 위치가 잘 맞지 않아서 3를 직접 빼줌
|
|
|
+ float right = left + (bound.width() - paddingStart - paddingEnd);
|
|
|
+ float bottom = bound.height() - paddingBottom - 3; // track 의 높이 18dp => 이지만 높이 계산이 잘 맞지 않는 경우가 있어서 bottom 으로부터 계산
|
|
|
|
|
|
if (isChecked) {
|
|
|
+
|
|
|
+ // track
|
|
|
+ Drawable d = ResourceUtil.getDrawable(R.drawable.switch_track_on);
|
|
|
+ d.setBounds((int) left, (int) top, (int) right, (int) bottom);
|
|
|
+ d.draw(canvas);
|
|
|
+
|
|
|
//on 텍스트 흰색 변경
|
|
|
textPaint.getTextBounds(leftText, 0, leftText.length(), textBounds);
|
|
|
- heightBaseline = canvas.getClipBounds().height() / 2 + textBounds.height() / 2;
|
|
|
- textPaint.setColor(context.getResources().getColor(android.R.color.white));
|
|
|
- canvas.drawText(leftText, 0, leftText.length(),
|
|
|
- widthQuarter + 15, heightBaseline, textPaint);
|
|
|
+ textPaint.setColor(ResourceUtil.getColor(R.color.CFFFFFF));
|
|
|
+ heightBaseline = ((bound.height() - paddingTop - paddingBottom) >> 1) - (textBounds.height() >> 1) + paddingTop - textBounds.top;
|
|
|
+ canvas.drawText(leftText, 0, leftText.length(), paddingStart + ResourceUtil.dpToFloat(8) - textBounds.left, heightBaseline, textPaint);
|
|
|
}
|
|
|
else {
|
|
|
+
|
|
|
+ // track
|
|
|
+ Drawable d = ResourceUtil.getDrawable(R.drawable.switch_track_off);
|
|
|
+ d.setBounds((int) left, (int) top, (int) right, (int) bottom);
|
|
|
+ d.draw(canvas);
|
|
|
+
|
|
|
//off 텍스트 검정 변경
|
|
|
textPaint.getTextBounds(rightText, 0, rightText.length(), textBounds);
|
|
|
- widthQuarter = canvas.getClipBounds().width() / 4;
|
|
|
- heightBaseline = canvas.getClipBounds().height() / 2 + textBounds.height() / 2;
|
|
|
- textPaint.setColor(context.getResources().getColor(android.R.color.darker_gray));
|
|
|
- canvas.drawText(rightText, 0, rightText.length(),
|
|
|
- widthQuarter * 3 - 20, heightBaseline, textPaint);
|
|
|
- ;
|
|
|
+ textPaint.setColor(ResourceUtil.getColor(R.color.C999999));
|
|
|
+ heightBaseline = ((bound.height() - paddingTop - paddingBottom) >> 1) - (textBounds.height() >> 1) + paddingTop - textBounds.top;
|
|
|
+ canvas.drawText(rightText, 0, rightText.length(), right - textBounds.width() - ResourceUtil.dpToFloat(6) - textBounds.left, heightBaseline, textPaint);
|
|
|
}
|
|
|
|
|
|
|