Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple line through text view. ie. ----------------- Hello, World --------------
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 longkai
*
* The software shall be used for good, not evil.
*/
package android.support.widget;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.support.R;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
/**
* A simple line through text view. ie. ----------------- Hello, World --------------
* <p>
* NOTE: only support one line text(not that long), support multiple lines do it by yourself.
*
* @author longkai
*/
public class LineThroughTextView extends View {
private String text;
private int textColor;
private float textSize;
private int textPadding;
private int lineHeight;
private int lineColor;
private Paint mTextPaint;
public LineThroughTextView(Context context) {
this(context, null);
}
public LineThroughTextView(Context context, AttributeSet attrs) {
super(context, attrs);
bootstrap(context, attrs, 0, 0);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public LineThroughTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
bootstrap(context, attrs, defStyleAttr, 0);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public LineThroughTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
bootstrap(context, attrs, defStyleAttr, defStyleRes);
}
private void bootstrap(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LineThroughTextView, defStyleAttr, defStyleRes);
text = a.getString(R.styleable.LineThroughTextView_android_text);
textColor = a.getColor(R.styleable.LineThroughTextView_android_textColor, Color.BLACK);
DisplayMetrics metrics = getResources().getDisplayMetrics();
textSize = a.getDimension(R.styleable.LineThroughTextView_android_textSize,
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, metrics) / metrics.density);
textPadding = a.getDimensionPixelSize(R.styleable.LineThroughTextView_textPadding, 0);
lineHeight = a.getDimensionPixelSize(R.styleable.LineThroughTextView_lineHeight, 2); // default to one pixel
lineColor = a.getColor(R.styleable.LineThroughTextView_lineColor, Color.DKGRAY);
a.recycle();
mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
}
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!TextUtils.isEmpty(text)) {
canvas.save();
mTextPaint.setTextSize(textSize);
mTextPaint.setColor(textColor);
float textWidth = mTextPaint.measureText(text);
int x = (int) ((getWidth() - textWidth) / 2);
int y = (int) ((getHeight() - (mTextPaint.descent() + mTextPaint.ascent())) / 2);
// draw text
canvas.drawText(text, x, y, mTextPaint);
// line y
mTextPaint.setColor(lineColor);
y = getHeight() / 2;
// draw lines
canvas.drawRect(getPaddingLeft(), y - lineHeight / 2, x - textPadding, y + lineHeight / 2, mTextPaint);
canvas.drawRect(
x + textWidth + textPadding,
y - lineHeight / 2,
getWidth() - getPaddingRight(),
y + lineHeight / 2,
mTextPaint
);
canvas.restore();
}
}
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// if text is empty, skip it
if (TextUtils.isEmpty(text)) {
setMeasuredDimension(0, 0);
return;
}
// we only need to measure the text height
int height = (int) (textSize + getPaddingTop() + getPaddingBottom());
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), resolveSize(height, heightMeasureSpec));
}
public int getLineColor() {
return lineColor;
}
public void setLineColor(int lineColor) {
this.lineColor = lineColor;
invalidate();
}
public int getLineHeight() {
return lineHeight;
}
public void setLineHeight(int lineHeight) {
this.lineHeight = lineHeight;
invalidate();
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
invalidate();
}
public int getTextColor() {
return textColor;
}
public void setTextColor(int textColor) {
this.textColor = textColor;
invalidate();
}
public int getTextPadding() {
return textPadding;
}
public void setTextPadding(int textPadding) {
this.textPadding = textPadding;
invalidate();
}
public float getTextSize() {
return textSize;
}
public void setTextSize(float textSize) {
this.textSize = textSize;
requestLayout();
}
}
@TheReprator

This comment has been minimized.

Copy link

@TheReprator TheReprator commented Aug 10, 2015

a good tutorial but lack attr...i am forking it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment