Last active
February 4, 2016 20:16
-
-
Save mnafian/f210dc11e08b07788d47 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dummy.protophonto.utilAndHelper; | |
/** | |
* Created on : January 20, 2016 | |
* Author : mnafian | |
* Name : M. Nafian | |
* Email : [email protected] | |
* GitHub : https://github.com/mnafian | |
* LinkedIn : https://id.linkedin.com/in/mnafian | |
* Company : Inagata Technosmith | |
*/ | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.util.AttributeSet; | |
import android.widget.TextView; | |
public class TextViewCustom extends TextView { | |
private static final int DEFAULT_OUTLINE_COLOR = Color.parseColor("#00FFFFFF"); | |
private static final int DEFAULT_OUTLINE_SIZE = 0; | |
private static final boolean DEFAULT_OUTLINE_STATE = true; | |
private int sizeMeasureAfterStroke; // add this | |
private int outlineColor; | |
private int outlineSize; | |
private boolean outlineState; | |
public TextViewCustom(Context context) { | |
this(context, null); | |
} | |
public TextViewCustom(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public TextViewCustom(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 int getOutlineColor() { | |
return outlineColor; | |
} | |
public void setOutlineColor(int outlineColor) { | |
this.outlineColor = outlineColor; | |
invalidate(); | |
} | |
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(); | |
sizeMeasureAfterStroke = getCompoundPaddingLeft() + outlineSize; // save value ondraw | |
} | |
getPaint().setColor(getCurrentTextColor()); | |
getPaint().setStyle(Paint.Style.FILL); | |
canvas.save(); | |
canvas.translate(outlineSize, 0); | |
super.draw(canvas); | |
canvas.restore(); | |
} | |
@Override | |
public boolean onSetAlpha(int alpha) { | |
setTextColor(getTextColors().withAlpha(alpha)); | |
setHintTextColor(getHintTextColors().withAlpha(alpha)); | |
setLinkTextColor(getLinkTextColors().withAlpha(alpha)); | |
getBackground().setAlpha(alpha); | |
return true; | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
int width = getMeasuredWidth() + sizeMeasureAfterStroke; | |
int height = getMeasuredHeight(); | |
setMeasuredDimension(width, height); | |
//set measure dimension here | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment