Android為TextView添加字體庫(kù)和設(shè)置描邊的方法
一、使用系統(tǒng)自帶的字體
開發(fā)Android的人大多都知道,Android里面對(duì)字體的支持少得可憐,默認(rèn)情況下,TextView 的 typeface 屬性支持 sans、serif和monospace 這三種字體,如果在沒有指定字體的情況下,系統(tǒng)會(huì)使用 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實(shí)例
//獲取textView實(shí)例 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添加字體庫(kù)
Android系統(tǒng)自帶有對(duì)字體的設(shè)置,這些設(shè)置是對(duì)字體的顯示方式的設(shè)置,比如加粗、傾斜、下劃線、字號(hào)等,但是并沒有提供對(duì)于字體類型的徐選擇,比如設(shè)置成楷體、隸書或雅黑等。Android系統(tǒng)只固定默認(rèn)一種字體類型,所以如果開發(fā)人員需要修改字體類型,那么就必須需自己引入字體庫(kù)。
1.引入字體庫(kù)的實(shí)現(xiàn)
第一步:在assets目錄下新建fonts目錄,并把ttf字體文件放到該目錄下。
第二步:在Java代碼中實(shí)現(xiàn)
//實(shí)例化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.引入字體庫(kù)后的效果圖
三、為TextView添加描邊
Android的默認(rèn)控件TextView,相信大家都不會(huì)陌生,但是原生的TextView是不支持描邊效果的,但是在實(shí)際的開發(fā)過程中,經(jīng)常會(huì)遇到為TextView添加描邊的需求,因此就要對(duì)原生的TextView進(jìn)行拓展,使其支持自定義內(nèi)部和外部顏色的描邊TextView。描邊效果的實(shí)現(xiàn)原理其實(shí)很簡(jiǎn)單,無非就是獲取到TextPaint類,先進(jìn)行一次比默認(rèn)大小的文字內(nèi)容稍微大一點(diǎn)的繪制,然后再進(jìn)行一次默認(rèn)大小的文字內(nèi)容的繪制,然后通過屬性設(shè)置兩種不同的顏色,這樣就產(chǎn)生出了描邊效果。
為TextView添加描邊,要用到TextPaint的幾個(gè)屬性:
TextPaint paint = outlineTextView.getPaint(); //實(shí)例化TextPaint對(duì)象 paint.setStrokeWidth(15); //設(shè)置描邊的寬度 paint.setStyle(Paint.Style.STROKE);//設(shè)置畫筆屬性為描邊 strokeTextView.setTextColor(Color.parseColor(“#000000”)); //設(shè)置描邊的顏色(不能與文本顏色一致)
其中strokeTextView為自定義TextView的實(shí)例,代碼如下:
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); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android項(xiàng)目基本結(jié)構(gòu)詳解
這篇文章主要為大家詳細(xì)介紹了Android項(xiàng)目基本結(jié)構(gòu),從最基本的內(nèi)容講起,帶你逐步進(jìn)入用C#進(jìn)行Android應(yīng)用開發(fā)的樂園,感興趣的小伙伴們可以參考一下2016-06-06詳解Android中常見的內(nèi)存優(yōu)化及內(nèi)存泄露場(chǎng)景
本文主要給大家介紹了Android中常見的內(nèi)存優(yōu)化及Android開發(fā)中容易造成內(nèi)存泄露的場(chǎng)景,對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-08-08Android編程單擊圖片實(shí)現(xiàn)切換效果的方法
這篇文章主要介紹了Android編程單擊圖片實(shí)現(xiàn)切換效果的方法,以實(shí)例形式分析了Android布局及切換功能的具體實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10解析如何在android中增加gsensor驅(qū)動(dòng)(MMA7660)
本篇文章是對(duì)在android中增加gsensor驅(qū)動(dòng)(MMA7660)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06android基礎(chǔ)總結(jié)篇之一:Activity生命周期
本篇文章主要介紹了android基礎(chǔ)總結(jié)篇之一:Activity生命周期,想要學(xué)習(xí)的可以了解一下。2016-11-11Android自定義View實(shí)現(xiàn)圓形切圖效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)圓形切圖效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12android sdk安裝及開發(fā)環(huán)境部署
本文給大家詳細(xì)講解了android sdk安裝方法以及android開發(fā)環(huán)境部署方法,非常的細(xì)致全面,有需要的小伙伴務(wù)必詳細(xì)研究下。2015-11-11