一咆槽、概述
TextView 以及繼承自 TextView 的 Widget 都是同樣的陈轿,設(shè)置 DrawableTop(Left/Right/Bottom),都會對其 TextView 的邊緣秦忿,而大多數(shù)情況我們需要的是顯示在中間位置麦射。
參照網(wǎng)上的 DrawableCenterTextView 進(jìn)行改進(jìn)
二、字體高度說明
在 TextView 里面的字體高度是一個自定義View 經(jīng)常會遇到的問題灯谣,在這里稍微研究一下:
字體高度通過 Paint.FontMetrics 獲取潜秋,下面是跟字體高度有關(guān)的幾個屬性
- baseline 基線,以下所有屬性都是根據(jù) baseline 而來的
自定義一個 TextView,在 onDraw 將所有線都通過 DrawLine 畫出來胎许,代碼如下:
Paint mPaint = new Paint();
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(Color.RED);
mPaint.setTextSize(sp2px(8));
Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
//baseline
canvas.drawLine(0, getBaseline(), getWidth(), getBaseline(), mPaint);
canvas.drawText("baseline", 0, getBaseline(), mPaint);
//top
canvas.drawLine(0, fontMetrics.top + getBaseline(), getWidth(), fontMetrics.top + getBaseline(), mPaint);
canvas.drawText("top", getWidth() / 5, fontMetrics.top + getBaseline(), mPaint);
//ascent
canvas.drawLine(0, fontMetrics.ascent + getBaseline(), getWidth(), fontMetrics.ascent + getBaseline(), mPaint);
canvas.drawText("ascent", getWidth() * 2 / 5, fontMetrics.ascent + getBaseline(), mPaint);
//descent
canvas.drawLine(0, fontMetrics.descent + getBaseline(), getWidth(), fontMetrics.descent + getBaseline(), mPaint);
canvas.drawText("descent", getWidth() * 3 / 5, fontMetrics.descent + getBaseline(), mPaint);
//bottom
canvas.drawLine(0, fontMetrics.bottom + getBaseline(), getWidth(), fontMetrics.bottom + getBaseline(), mPaint);
canvas.drawText("bottom", getWidth() * 4 / 5, fontMetrics.bottom + getBaseline(), mPaint);
//leading
canvas.drawLine(0, fontMetrics.leading + getBaseline(), getWidth(), fontMetrics.leading + getBaseline(), mPaint);
canvas.drawText("leading", getWidth(), fontMetrics.leading + getBaseline(), mPaint);
}
運(yùn)行的結(jié)果如下:
在這幾個值中峻呛,baseline 代表0罗售,ascent 為正值,descent 為負(fù)值
通用的字體高度:ascent - descent
只需數(shù)字的字體高低:ascent + descent
三钩述、 顯示效果
在了解字體高度后寨躁,通過 getCompoundDrawables(),獲得 Drawable 集合牙勘,可以得到 drawable 的高和寬進(jìn)行操作职恳,效果及代碼如下:
public class DrawableCenterTextView extends android.support.v7.widget.AppCompatTextView {
/** 圖片列表 */
private Drawable[] drawables;
/**
* 構(gòu)造函數(shù)
*
* @param context 上下文
*/
public DrawableCenterTextView(Context context) {
super(context);
}
/**
* 構(gòu)造函數(shù)
*
* @param context 上下文
* @param attrs 屬性
*/
public DrawableCenterTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 構(gòu)造函數(shù)
*
* @param context 上下文
* @param attrs 屬性
* @param defStyle 樣式
*/
public DrawableCenterTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
drawables = getCompoundDrawables();
if (drawables[0] != null || drawables[2] != null) {
// 左、右
setGravity(Gravity.CENTER_VERTICAL | (drawables[0] != null ? Gravity.START : Gravity.END));
} else if (drawables[1] != null || drawables[3] != null) {
// 上方面、下
setGravity(Gravity.CENTER_HORIZONTAL | (drawables[1] != null ? Gravity.TOP : Gravity.BOTTOM));
}
super.onLayout(changed, left, top, right, bottom);
}
@Override
protected void onDraw(Canvas canvas) {
int drawablePadding = getCompoundDrawablePadding();
if (drawables[0] != null) {
// 左
int drawableWidth = drawables[0].getIntrinsicWidth();
float bodyWidth;
if (TextUtils.isEmpty(getText())) {
bodyWidth = drawableWidth;
} else {
float textWidth = getPaint().measureText(getText().toString());
bodyWidth = textWidth + drawableWidth + drawablePadding;
}
canvas.translate((getWidth() - bodyWidth) / 2, 0);
} else if (drawables[2] != null) {
// 右
int drawableWidth = drawables[2].getIntrinsicWidth();
float bodyWidth;
if (TextUtils.isEmpty(getText())) {
bodyWidth = drawableWidth;
} else {
float textWidth = getPaint().measureText(getText().toString());
bodyWidth = textWidth + drawableWidth + drawablePadding;
}
canvas.translate((bodyWidth - getWidth()) / 2, 0);
} else if (drawables[1] != null) {
// 上
int drawableHeight = drawables[1].getIntrinsicHeight();
float bodyHeight;
if (TextUtils.isEmpty(getText())) {
bodyHeight = drawableHeight;
} else {
Paint.FontMetrics fm = getPaint().getFontMetrics();
float fontHeight = (float) Math.ceil(fm.descent - fm.ascent);
bodyHeight = fontHeight + drawableHeight + drawablePadding;
}
canvas.translate(0, (getHeight() - bodyHeight) / 2);
} else if (drawables[3] != null) {
// 下
int drawableHeight = drawables[3].getIntrinsicHeight();
float bodyHeight;
if (TextUtils.isEmpty(getText())) {
bodyHeight = drawableHeight;
} else {
Paint.FontMetrics fm = getPaint().getFontMetrics();
float fontHeight = (float) Math.ceil(fm.descent - fm.ascent);
bodyHeight = fontHeight + drawableHeight + drawablePadding;
}
canvas.translate(0, (bodyHeight - getHeight()) / 2);
}
super.onDraw(canvas);
}
}
轉(zhuǎn)載注意出處:http://www.reibang.com/p/f4b6374d431f
Github 地址為放钦,https://github.com/lwcye/DrawCenterTextView
如果覺得喜歡就star一下