新聞中心
這篇文章主要介紹Android如何自定義TextView實現(xiàn)文字圖片居中顯示,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)專注于濟源企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計,成都商城網(wǎng)站開發(fā)。濟源網(wǎng)站建設(shè)公司,為濟源等地區(qū)提供建站服務(wù)。全流程按需策劃,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
我們先來分析下TextView的源碼,因為TextView有上下左右四個方向的圖片,上下咱就先不考慮了,因為一般來說圖片垂直居中是沒有問題的,我們就只處理這個left,和right方向上的圖片, 我們直接看TextView的ondraw方法,因為TextView 也是繼承自View,所有的繪制都將會在這里操作
int vspace = bottom - top - compoundPaddingBottom - compoundPaddingTop;
int hspace = right - left - compoundPaddingRight - compoundPaddingLeft;
// IMPORTANT: The coordinates computed are also used in invalidateDrawable()
// Make sure to update invalidateDrawable() when changing this code.
if (dr.mShowing[Drawables.LEFT] != null) {
canvas.save();
canvas.translate(scrollX + mPaddingLeft + leftOffset,
scrollY + compoundPaddingTop +
(vspace - dr.mDrawableHeightLeft) / 2);
dr.mShowing[Drawables.LEFT].draw(canvas);
canvas.restore();
}
// IMPORTANT: The coordinates computed are also used in invalidateDrawable()
// Make sure to update invalidateDrawable() when changing this code.
if (dr.mShowing[Drawables.RIGHT] != null) {
canvas.save();
canvas.translate(scrollX + right - left - mPaddingRight
- dr.mDrawableSizeRight - rightOffset,
scrollY + compoundPaddingTop + (vspace - dr.mDrawableHeightRight) / 2);
dr.mShowing[Drawables.RIGHT].draw(canvas);
canvas.restore();
}
從上面可以看到有個canvas.translate方法,大概意思是,save后,將畫布向X軸和Y軸分別平移了scrollX ..和scrollY,平移后,將left方向的圖片繪制上去,最后restore還原到上個畫布中,Right同理。
那這樣,咱基本上就明白原理,TextView的四個方向都是通過Canvas的translate來繪制到文字的上下左右了,那咱們就只改這個scrollX 和 scrollY就可以實現(xiàn)咱的需求了吧。
具體實現(xiàn)
1.下面寫有注釋,不是特別麻煩,適配drawableLeft 和 drawableRight圖片,PS,xml中不要設(shè)置Gravity,這樣就可以居中了,代碼如下:
package com.chaoxing.email.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.TextView;
/**
* use in xml
* use in code
*/
public class EmailCenterTextView extends TextView {
public EmailCenterTextView(Context context) {
super(context);
}
public EmailCenterTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EmailCenterTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
Drawable[] drawables = getCompoundDrawables();
if (null != drawables) {
Drawable drawableLeft = drawables[0];
Drawable drawableRight = drawables[2];
float textWidth = getPaint().measureText(getText().toString());
if (null != drawableLeft) {
setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
float contentWidth = textWidth + getCompoundDrawablePadding() + drawableLeft.getIntrinsicWidth();
if (getWidth() - contentWidth > 0) {
canvas.translate((getWidth() - contentWidth - getPaddingRight() - getPaddingLeft()) / 2, 0);
}
}
if (null != drawableRight) {
setGravity(Gravity.END | Gravity.CENTER_VERTICAL);
float contentWidth = textWidth + getCompoundDrawablePadding() + drawableRight.getIntrinsicWidth();
if (getWidth() - contentWidth > 0) {
canvas.translate(-(getWidth() - contentWidth - getPaddingRight() - getPaddingLeft()) / 2, 0);
}
}
if (null == drawableRight && null == drawableLeft) {
setGravity(Gravity.CENTER);
}
}
super.onDraw(canvas);
}
}
更新效果圖(因為之前有看到網(wǎng)友回復(fù),最近又用到了再更新下這個博客)
title是用的就是EmailCenterTextView,那個箭頭上下的就是設(shè)置的drawableRight,演示的未讀和垃圾箱EmailCenterTextView沒有設(shè)置圖片
以上是“Android如何自定義TextView實現(xiàn)文字圖片居中顯示”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
本文題目:Android如何自定義TextView實現(xiàn)文字圖片居中顯示
文章鏈接:http://fisionsoft.com.cn/article/jjcsog.html