Skip to content

Instantly share code, notes, and snippets.

@TheReprator
Forked from longkai/LineThroughTextView.java
Last active October 16, 2015 06:31
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save TheReprator/b0c0c5d80004b2d40eec to your computer and use it in GitHub Desktop.
Save TheReprator/b0c0c5d80004b2d40eec to your computer and use it in GitHub Desktop.
A simple line through text view. ie. ----------------- Hello, World --------------
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="LineThroughTextView">
<attr name="lineColor" format="color" />
<attr name="android_text" format="string" />
<attr name="android_textSize" format="dimension" />
<attr name="android_textColor" format="color" />
<attr name="textPadding" format="dimension" />
<attr name="lineHeight" format="dimension" />
</declare-styleable>
</resources>
<views.LineThroughTextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_context"
android:layout_weight="13"
android:text="@string/str_or"
android:textSize="35sp"
app:lineHeight="4dp"
app:android_text="---Data---"
app:textPadding="4dp"
app:android_textSize="30sp" />
package views;
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.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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment