Created
January 18, 2018 10:48
-
-
Save kakai248/c2531aae54e3557c075f634637e018a4 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
package me.mesmo.app.presentation.views; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.Canvas; | |
import android.graphics.Paint; | |
import android.support.annotation.ColorInt; | |
import android.support.annotation.Nullable; | |
import android.support.v7.widget.AppCompatTextView; | |
import android.text.Layout; | |
import android.util.AttributeSet; | |
import me.mesmo.app.R; | |
import me.mesmo.app.presentation.utils.ScreenUtils; | |
public class BottomLinedTextView extends AppCompatTextView { | |
private Paint paint = new Paint(); | |
private float lineHeight; | |
private float lineHorizontalPadding; | |
public BottomLinedTextView(Context context) { | |
super(context); | |
init(context, null, 0); | |
} | |
public BottomLinedTextView(Context context, @Nullable AttributeSet attrs) { | |
super(context, attrs); | |
init(context, attrs, 0); | |
} | |
public BottomLinedTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(context, attrs, defStyleAttr); | |
} | |
private void init(Context context, AttributeSet attrs, int defStyleAttr) { | |
if (attrs != null) { | |
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BottomLinedTextView, defStyleAttr, 0); | |
try { | |
float lineHeight = ta.getDimension(R.styleable.BottomLinedTextView_lineHeight, ScreenUtils.dp2Px(context, 1)); | |
setLineHeight(lineHeight); | |
float lineHorizontalPadding = ta.getDimension(R.styleable.BottomLinedTextView_lineHorizontalPadding, 0f); | |
setLineHorizontalPadding(lineHorizontalPadding); | |
int lineColor = ta.getColor(R.styleable.BottomLinedTextView_lineColor, getCurrentTextColor()); | |
setLineColor(lineColor); | |
} finally { | |
ta.recycle(); | |
} | |
} | |
} | |
@SuppressWarnings("SuspiciousNameCombination") | |
public void setLineHeight(float lineHeight) { | |
this.lineHeight = lineHeight; | |
paint.setStrokeWidth(lineHeight); | |
invalidate(); | |
} | |
public void setLineHorizontalPadding(float lineHorizontalPadding) { | |
this.lineHorizontalPadding = lineHorizontalPadding; | |
invalidate(); | |
} | |
public void setLineColor(@ColorInt int lineColor) { | |
paint.setColor(lineColor); | |
invalidate(); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
Layout layout = getLayout(); | |
float lastLineWidth = layout.getLineWidth(layout.getLineCount() - 1); | |
float startX = (getWidth() - lastLineWidth) / 2 + lineHorizontalPadding; | |
float stopX = getWidth() - startX; | |
float y = getHeight() - lineHeight; | |
canvas.drawLine(startX, y, stopX, y, paint); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment