Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.tempos21.bluekiwi.ui.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;
/**
* Created by Bernat Borrás Paronella on 14/08/2014.
*/
public class LineTextView extends TextView {
private Paint mPaint;
private Rect mRect;
public LineTextView(Context context) {
super(context);
init();
}
public LineTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public LineTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
isInEditMode();
setGravity(Gravity.CENTER);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeWidth(1f);
mRect = new Rect();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(getCurrentTextColor());
canvas.getClipBounds(mRect);
float centerX = mRect.centerX();
float centerY = mRect.centerY();
float measuredText = mPaint.measureText(getText().toString());
canvas.drawLine(mRect.left, centerY, centerX - measuredText - 30, centerY, mPaint);
canvas.drawLine(centerX + measuredText + 30, centerY, mRect.right, centerY, mPaint);
}
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
super.onTextChanged(text, start, lengthBefore, lengthAfter);
invalidate();
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
invalidate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment