Skip to content

Instantly share code, notes, and snippets.

@hrules6872
Created April 13, 2015 14:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrules6872/2f28920b55776a5ebd62 to your computer and use it in GitHub Desktop.
Save hrules6872/2f28920b55776a5ebd62 to your computer and use it in GitHub Desktop.
Outline TextView
public class OutlineTextView extends TextView {
private static final int DEFAULT_OUTLINE_COLOR = 0xFF000000;
private static final int DEFAULT_OUTLINE_SIZE = 7;
private static final boolean DEFAULT_OUTLINE_STATE = true;
private int outlineColor;
private int outlineSize;
private boolean outlineState;
public OutlineTextView(Context context) {
this(context, null);
}
public OutlineTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public OutlineTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
outlineColor = DEFAULT_OUTLINE_COLOR;
outlineSize = DEFAULT_OUTLINE_SIZE;
outlineState = DEFAULT_OUTLINE_STATE;
}
public void setOutlineColor(int outlineColor) {
this.outlineColor = outlineColor;
invalidate();
}
public int getOutlineColor() {
return outlineColor;
}
public int getOutlineSize() {
return outlineSize;
}
public void setOutlineSize(int outlineSize) {
this.outlineSize = outlineSize;
invalidate();
}
public boolean getOutlineState() {
return outlineState;
}
public void setOutlineState(boolean state) {
outlineState = state;
invalidate();
}
@Override
public void draw(Canvas canvas) {
if (outlineState) {
getPaint().setColor(outlineColor);
getPaint().setStyle(Paint.Style.STROKE);
getPaint().setStrokeWidth(outlineSize);
canvas.save();
canvas.translate(getCompoundPaddingLeft() + outlineSize, getCompoundPaddingTop());
getLayout().draw(canvas);
canvas.restore();
}
getPaint().setColor(getCurrentTextColor());
getPaint().setStyle(Paint.Style.FILL);
canvas.save();
canvas.translate(outlineSize, 0);
super.draw(canvas);
canvas.restore();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment