Android為TextView添加字體庫和設(shè)置描邊的方法
一、使用系統(tǒng)自帶的字體
開發(fā)Android的人大多都知道,Android里面對字體的支持少得可憐,默認(rèn)情況下,TextView 的 typeface 屬性支持 sans、serif和monospace 這三種字體,如果在沒有指定字體的情況下,系統(tǒng)會使用 sans 作為文本顯示的字體。但這三種字體只支持英文,也就是說只要你顯示的文字是中文,無論你選擇這三種字體中的哪一種,顯示效果都是一樣的。
1.在XML文件中設(shè)置
<!-- 使用默認(rèn)的sans字體--> <TextView android:id="@+id/sans" android:text="Hello,World" android:textSize="20sp" android:typeface="sans" /> <!-- 使用默認(rèn)的serifs字體--> <TextView android:id="@+id/serif" android:text="Hello,World" android:textSize="20sp" android:typeface="serif" /> <!-- 使用默認(rèn)的monospace字體--> <TextView android:id="@+id/monospace" android:text="Hello,World" android:textSize="20sp" android:typeface="monospace" />
2.在Java代碼中設(shè)置
第一步: 獲取TextView實例
//獲取textView實例 TextView textView = findViewById(R.id.textview);
第二步:設(shè)置字體
//設(shè)置serif字體 textView.setTypeface(Typeface.SERIF); //設(shè)置sans字體 textView.setTypeface(Typeface.SANS_SERIF); //設(shè)置monospace字體 textView.setTypeface(Typeface.MONOSPACE);
二、為TextView添加字體庫
Android系統(tǒng)自帶有對字體的設(shè)置,這些設(shè)置是對字體的顯示方式的設(shè)置,比如加粗、傾斜、下劃線、字號等,但是并沒有提供對于字體類型的徐選擇,比如設(shè)置成楷體、隸書或雅黑等。Android系統(tǒng)只固定默認(rèn)一種字體類型,所以如果開發(fā)人員需要修改字體類型,那么就必須需自己引入字體庫。
1.引入字體庫的實現(xiàn)
第一步:在assets目錄下新建fonts目錄,并把ttf字體文件放到該目錄下。
第二步:在Java代碼中實現(xiàn)
//實例化TextView TextView textView = findViewById(R.id.textview); //得到AssetManager AssetManager mgr=getAssets(); //根據(jù)路徑得到Typeface Typeface tf=Typeface.createFromAsset(mgr, "fonts/pocknum.ttf"); //設(shè)置字體 textView.setTypeface(tf);
2.引入字體庫后的效果圖

三、為TextView添加描邊
Android的默認(rèn)控件TextView,相信大家都不會陌生,但是原生的TextView是不支持描邊效果的,但是在實際的開發(fā)過程中,經(jīng)常會遇到為TextView添加描邊的需求,因此就要對原生的TextView進(jìn)行拓展,使其支持自定義內(nèi)部和外部顏色的描邊TextView。描邊效果的實現(xiàn)原理其實很簡單,無非就是獲取到TextPaint類,先進(jìn)行一次比默認(rèn)大小的文字內(nèi)容稍微大一點的繪制,然后再進(jìn)行一次默認(rèn)大小的文字內(nèi)容的繪制,然后通過屬性設(shè)置兩種不同的顏色,這樣就產(chǎn)生出了描邊效果。
為TextView添加描邊,要用到TextPaint的幾個屬性:
TextPaint paint = outlineTextView.getPaint(); //實例化TextPaint對象 paint.setStrokeWidth(15); //設(shè)置描邊的寬度 paint.setStyle(Paint.Style.STROKE);//設(shè)置畫筆屬性為描邊 strokeTextView.setTextColor(Color.parseColor(“#000000”)); //設(shè)置描邊的顏色(不能與文本顏色一致)
其中strokeTextView為自定義TextView的實例,代碼如下:
1.在構(gòu)造函數(shù)中添加
public class StrokeTextView extends TextView {
private TextView outlineTextView = null;
public StrokeTextView(Context context) {
super(context);
outlineTextView = new TextView(context);
init();
}
public StrokeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
outlineTextView = new TextView(context, attrs);
init();
}
public StrokeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
outlineTextView = new TextView(context, attrs, defStyle);
init();
}
public void init() {
TextPaint paint = outlineTextView.getPaint();
paint.setStrokeWidth(3); //描邊寬度
paint.setStyle(Style.STROKE);
outlineTextView.setTextColor(Color.parseColor("#000000")); //描邊顏色
outlineTextView.setGravity(getGravity());
}
@Override
public void setLayoutParams (ViewGroup.LayoutParams params) {
super.setLayoutParams(params);
outlineTextView.setLayoutParams(params);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//設(shè)置輪廓文字
CharSequence outlineText = outlineTextView.getText();
if (outlineText == null || !outlineText.equals(this.getText())) {
outlineTextView.setText(getText());
postInvalidate();
}
outlineTextView.measure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
outlineTextView.layout(left, top, right, bottom);
}
@Override
protected void onDraw(Canvas canvas) {
outlineTextView.draw(canvas);
super.onDraw(canvas);
}
}
2.重寫onDraw方法
public class StrokeTextView extends TextView {
private TextView outlineTextView = null;
private TextPaint strokePaint;
public StrokeTextView(Context context) {
super(context);
outlineTextView = new TextView(context);
}
public StrokeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
outlineTextView = new TextView(context, attrs);
}
public StrokeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
outlineTextView = new TextView(context, attrs, defStyle);
}
@Override
public void setLayoutParams (ViewGroup.LayoutParams params) {
super.setLayoutParams(params);
outlineTextView.setLayoutParams(params);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
AssetManager manager = context.getAssets();
String path = "fonts/new_text.ttf";
Typeface type = Typeface.createFromAsset(manager, path);
//設(shè)置輪廓文字
CharSequence outlineText = outlineTextView.getText();
if (outlineText == null || !outlineText.equals(this.getText())) {
outlineTextView.setText(getText());
outlineTextView.setTypeface(type);
setTypeface(type);
postInvalidate();
}
outlineTextView.measure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout (boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
outlineTextView.layout(left, top, right, bottom);
}
@Override
protected void onDraw(Canvas canvas) {
AssetManager manager = context.getAssets();
String path = "fonts/new_text.ttf";
Typeface type = Typeface.createFromAsset(manager, path);
if (strokePaint == null) {
strokePaint = new TextPaint();
}
//復(fù)制原來TextViewg畫筆中的一些參數(shù)
TextPaint paint = getPaint();
strokePaint.setTextSize(paint.getTextSize());
strokePaint.setTypeface(type);
strokePaint.setFlags(paint.getFlags());
strokePaint.setAlpha(paint.getAlpha());
//自定義描邊效果
strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setColor(Color.parseColor("#000000"));
strokePaint.setStrokeWidth(4);
String text = getText().toString();
//在文本底層畫出帶描邊的文本
canvas.drawText(text, (getWidth() - strokePaint.measureText(text)) / 2,
getBaseline(), strokePaint);
super.onDraw(canvas);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Material Design系列之自定義Behavior支持所有View
這篇文章主要為大家詳細(xì)介紹了Material Design系列之自定義Behavior支持所有View,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09
Android實現(xiàn)底部圖標(biāo)與Fragment的聯(lián)動實例
本篇文章主要介紹了Android實現(xiàn)底部圖標(biāo)與Fragment的聯(lián)動實例,具有一定的參考價值,有興趣的可以了解一下2017-07-07
Android使用RecyclerView實現(xiàn)列表數(shù)據(jù)選擇操作
這篇文章主要為大家詳細(xì)介紹了Android使用RecyclerView結(jié)合CheckBox實現(xiàn)列表數(shù)據(jù)選擇操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
android重力感應(yīng)開發(fā)之微信搖一搖功能
這篇文章主要為大家詳細(xì)介紹了android重力感應(yīng)開發(fā)之微信搖一搖功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05
Android添加圖片到ListView或者RecyclerView顯示
這篇文章主要介紹了Android添加圖片到ListView或者RecyclerView顯示的相關(guān)資料,需要的朋友可以參考下2016-08-08
Flutter開發(fā)之Shortcuts快捷鍵組件的用法詳解
在桌面端的開發(fā)中,鍵盤快捷鍵是非常常見而必要的,F(xiàn)lutter?既然可以開發(fā)桌面端應(yīng)用,那必然要提供自定義快捷鍵,所以本文就來和大家講講Shortcuts組件的簡單使用吧2023-05-05

