Created
November 27, 2017 01:00
-
-
Save pyeongho/859d82d7f30a4005ad7fd1e38ddd6512 to your computer and use it in GitHub Desktop.
This file contains 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
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Paint; | |
import android.graphics.Rect; | |
import android.support.annotation.NonNull; | |
import android.util.AttributeSet; | |
/** | |
* Created by phkim on 2017-11-24. | |
*/ | |
public class NPTextView extends android.support.v7.widget.AppCompatTextView { | |
private Paint mPaint;// = new Paint(); | |
private final Rect mBounds = new Rect(); | |
public NPTextView(Context context) { | |
super(context); | |
initPaint(); | |
} | |
public NPTextView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
initPaint(); | |
} | |
public NPTextView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
initPaint(); | |
} | |
private void initPaint(){ | |
mPaint = getPaint(); | |
} | |
@Override | |
protected void onDraw(@NonNull Canvas canvas) { | |
final String text = calculateTextParams(); | |
final int left = mBounds.left; | |
final int bottom = mBounds.bottom; | |
mBounds.offset(-mBounds.left, -mBounds.top); | |
mPaint.setAntiAlias(true); | |
mPaint.setColor(getCurrentTextColor()); | |
canvas.drawText(text, -left, mBounds.bottom - bottom, mPaint); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
calculateTextParams(); | |
setMeasuredDimension(mBounds.width() + 1, -mBounds.top + 5); | |
} | |
private String calculateTextParams() { | |
final String text = getText().toString(); | |
final int textLength = text.length(); | |
mPaint.setTextSize(getTextSize()); | |
mPaint.getTextBounds(text, 0, textLength, mBounds); | |
if (textLength == 0) { | |
mBounds.right = mBounds.left; | |
} | |
return text; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment