Skip to content

Instantly share code, notes, and snippets.

@mengdd
Created January 26, 2019 09:06
Show Gist options
  • Save mengdd/d27f3aff6dfea0465cb30fe9c8267680 to your computer and use it in GitHub Desktop.
Save mengdd/d27f3aff6dfea0465cb30fe9c8267680 to your computer and use it in GitHub Desktop.
Custom Switch Widget to draw the textOff too
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SwitchTab">
<attr name="switch_tab_unchecked_text_color" format="reference|color"/>
<attr name="switch_tab_unchecked_text_size" format="reference|dimension"/>
</declare-styleable>
</resources>
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.SwitchCompat;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
public class SwitchTab extends SwitchCompat {
private TextPaint mTextPaint;
private int uncheckedTextColor;
private int uncheckedTextSize;
private Layout mOnLayout;
private Layout mOffLayout;
public SwitchTab(Context context) {
super(context);
init(context, null);
}
public SwitchTab(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public SwitchTab(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
TypedArray typedArray = null;
if (attrs != null) {
typedArray = context.obtainStyledAttributes(attrs, R.styleable.SwitchTab);
}
mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
final Resources res = getResources();
mTextPaint.density = res.getDisplayMetrics().density;
uncheckedTextColor = optColor(typedArray,
R.styleable.SwitchTab_switch_tab_unchecked_text_color,
Color.WHITE);
mTextPaint.setColor(uncheckedTextColor);
uncheckedTextSize = optPixelSize(typedArray, R.styleable.SwitchTab_switch_tab_unchecked_text_size, 0);
mTextPaint.setTextSize(uncheckedTextSize);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mOnLayout == null) {
mOnLayout = makeLayout(getTextOn());
}
if (mOffLayout == null) {
mOffLayout = makeLayout(getTextOff());
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawTextOff(canvas);
}
private void drawTextOff(Canvas canvas) {
final int saveCount = canvas.save();
final Layout switchText = isChecked() ? mOffLayout : mOnLayout;
if (switchText != null) {
final int cX;
if (getThumbDrawable() != null) {
final Rect bounds = getThumbDrawable().getBounds();
cX = bounds.left + bounds.right;
} else {
cX = getWidth();
}
int left = cX / 2 - switchText.getWidth() / 2;
if (isChecked()) {
left -= getWidth() / 2;
} else {
left += getWidth() / 2;
}
final int top = getHeight() / 2 - switchText.getHeight() / 2;
canvas.translate(left, top);
switchText.draw(canvas);
}
canvas.restoreToCount(saveCount);
}
private Layout makeLayout(CharSequence text) {
final CharSequence transformed = text;
return new StaticLayout(transformed, mTextPaint,
transformed != null ?
(int) Math.ceil(Layout.getDesiredWidth(transformed, mTextPaint)) : 0,
Layout.Alignment.ALIGN_NORMAL, 1.f, 0, true);
}
private static int optColor(TypedArray typedArray,
int index,
int def) {
if (typedArray == null) {
return def;
}
return typedArray.getColor(index, def);
}
private static int optPixelSize(TypedArray typedArray,
int index,
int def) {
if (typedArray == null) {
return def;
}
return typedArray.getDimensionPixelSize(index, def);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment