Skip to content

Instantly share code, notes, and snippets.

@vinhdn
Last active June 27, 2017 09:14
Show Gist options
  • Save vinhdn/d01b9809a5e45c14aeac7a95772068b0 to your computer and use it in GitHub Desktop.
Save vinhdn/d01b9809a5e45c14aeac7a95772068b0 to your computer and use it in GitHub Desktop.
UnderlineTextView
package jp.mediaid.nge.library.categories.ui;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.TextView;
import static android.graphics.Paint.ANTI_ALIAS_FLAG;
//
// TextviewUnderlineFill - UnderlineFillTextView
//
// Created by Vin on 6/5/17.
// Copyright (c) 2017 Ominext. All rights reserved.
//
public class UnderlineFillTextView extends TextView {
private Path path;
private int colorLine = Color.GRAY;
private Rect bound;
private Paint paint;
float density;
public UnderlineFillTextView(Context context) {
super(context);
initViews();
}
public UnderlineFillTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initViews();
}
public UnderlineFillTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initViews();
}
private void initViews() {
density = getResources().getDisplayMetrics().density;
path = new Path();
paint = new Paint(ANTI_ALIAS_FLAG);
paint.setStrokeWidth(1f * density);
paint.setARGB(255, 0, 0, 0);
paint.setStyle(Paint.Style.STROKE);
colorLine = getHintTextColors().getDefaultColor();
paint.setColor(colorLine);
paint.setPathEffect(new DashPathEffect(new float[]{15, 10}, 0));
paint.setTextSize(getTextSize());
paint.setTypeface(getTypeface());
paint.setAlpha(120);
setLineSpacing(getLineHeight() / 3f, 1);
bound = new Rect();
}
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
super.onTextChanged(text, start, lengthBefore, lengthAfter);
}
@Override
protected void onDraw(Canvas canvas) {
int lines = getLineCount();
int paddingTop = getPaddingTop();
int width = getMeasuredWidth();
int lineHeight = getLineHeight();
float lineSpecExtra = 0f;
if (getText().toString().length() > 0) {
paint.getTextBounds(getText().toString(), 0, getText().toString().length(), bound);
lineSpecExtra = lineHeight - bound.height();
}
float spec = 0f;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
spec = getLineSpacingExtra();
}
for (int i = 1; i < lines + 1; i++) {
Path path = new Path();
path.moveTo(0, lineHeight * i + paddingTop - lineSpecExtra / (lines == i ? 2f : 2f) + spec / 3f);
path.lineTo(width, lineHeight * i + paddingTop - lineSpecExtra / (lines == i ? 2f : 2f) + spec / 3f);
canvas.drawPath(path, paint);
path.reset();
}
setHeight((int) (lineHeight * (lines) + paddingTop - lineSpecExtra/2f + 1f * density + 0.5f + spec / 3f));
super.onDraw(canvas);
}
}
//
package jp.mediaid.nge.library.categories.ui;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.text.Layout;
import android.text.style.LineBackgroundSpan;
import android.text.style.LineHeightSpan;
import android.widget.TextView;
import jp.mediaid.nge.R;
import jp.mediaid.nge.application.App;
public class DashedUnderlineSpan implements LineBackgroundSpan, LineHeightSpan {
private Paint paint;
private TextView textView;
private float offsetY;
private float spacingExtra;
public DashedUnderlineSpan(TextView textView, int color, float thickness, float dashPath,
float offsetY, float spacingExtra) {
this.paint = new Paint();
this.paint.setColor(color);
this.paint.setStyle(Paint.Style.STROKE);
this.paint.setPathEffect(new DashPathEffect(new float[] { dashPath, dashPath }, 0));
this.paint.setStrokeWidth(thickness);
this.textView = textView;
this.offsetY = offsetY;
this.spacingExtra = spacingExtra;
}
public DashedUnderlineSpan(TextView textView, int offsetY) {
this.paint = new Paint();
this.paint.setColor(App.get().getResources().getColor(R.color.grayLine));
this.paint.setStyle(Paint.Style.STROKE);
this.paint.setPathEffect(new DashPathEffect(new float[] { App.get().getResources().getDimension(R.dimen.dimen5dp), App.get().getResources().getDimension(R.dimen.dimen5dp) }, 0));
this.paint.setStrokeWidth(App.get().getResources().getDimension(R.dimen.dimen1dp));
this.textView = textView;
this.offsetY = offsetY;
this.spacingExtra = App.get().getResources().getDimension(R.dimen.dimen2dp);
}
@Override
public void chooseHeight(CharSequence text, int start, int end, int spanstartv, int v,
Paint.FontMetricsInt fm) {
fm.ascent -= spacingExtra;
fm.top -= spacingExtra;
fm.descent += spacingExtra;
fm.bottom += spacingExtra;
}
@Override
public void drawBackground(Canvas canvas, Paint p, int left, int right, int top, int baseline,
int bottom, CharSequence text, int start, int end, int lnum) {
int lineNum = textView.getLineCount();
for (int i = 0; i < lineNum; i++) {
Layout layout = textView.getLayout();
Path path = new Path();
path.moveTo(left, layout.getLineBottom(i) - spacingExtra + offsetY);
path.lineTo(right, layout.getLineBottom(i) - spacingExtra + offsetY);
canvas.drawPath(path, this.paint);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment