国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android為TextView添加字體庫和設置描邊的方法

2019-10-22 18:28:31
字體:
來源:轉載
供稿:網友

一、使用系統自帶的字體

開發Android的人大多都知道,Android里面對字體的支持少得可憐,默認情況下,TextView 的 typeface 屬性支持 sans、serif和monospace 這三種字體,如果在沒有指定字體的情況下,系統會使用 sans 作為文本顯示的字體。但這三種字體只支持英文,也就是說只要你顯示的文字是中文,無論你選擇這三種字體中的哪一種,顯示效果都是一樣的。

1.在XML文件中設置

<!-- 使用默認的sans字體--><TextView  android:id="@+id/sans"  android:text="Hello,World"  android:textSize="20sp"  android:typeface="sans" /><!-- 使用默認的serifs字體--><TextView  android:id="@+id/serif"  android:text="Hello,World"  android:textSize="20sp"  android:typeface="serif" /><!-- 使用默認的monospace字體--><TextView  android:id="@+id/monospace"  android:text="Hello,World"  android:textSize="20sp"  android:typeface="monospace" />

2.在Java代碼中設置

第一步: 獲取TextView實例

//獲取textView實例TextView textView = findViewById(R.id.textview);

第二步:設置字體

 //設置serif字體 textView.setTypeface(Typeface.SERIF); //設置sans字體 textView.setTypeface(Typeface.SANS_SERIF); //設置monospace字體 textView.setTypeface(Typeface.MONOSPACE);

二、為TextView添加字體庫

Android系統自帶有對字體的設置,這些設置是對字體的顯示方式的設置,比如加粗、傾斜、下劃線、字號等,但是并沒有提供對于字體類型的徐選擇,比如設置成楷體、隸書或雅黑等。Android系統只固定默認一種字體類型,所以如果開發人員需要修改字體類型,那么就必須需自己引入字體庫。

1.引入字體庫的實現

第一步:在assets目錄下新建fonts目錄,并把ttf字體文件放到該目錄下。

第二步:在Java代碼中實現

//實例化TextViewTextView textView = findViewById(R.id.textview);//得到AssetManagerAssetManager mgr=getAssets();//根據路徑得到TypefaceTypeface tf=Typeface.createFromAsset(mgr, "fonts/pocknum.ttf");//設置字體textView.setTypeface(tf);

2.引入字體庫后的效果圖

Android添加字體庫,Android設置描邊

三、為TextView添加描邊

Android的默認控件TextView,相信大家都不會陌生,但是原生的TextView是不支持描邊效果的,但是在實際的開發過程中,經常會遇到為TextView添加描邊的需求,因此就要對原生的TextView進行拓展,使其支持自定義內部和外部顏色的描邊TextView。描邊效果的實現原理其實很簡單,無非就是獲取到TextPaint類,先進行一次比默認大小的文字內容稍微大一點的繪制,然后再進行一次默認大小的文字內容的繪制,然后通過屬性設置兩種不同的顏色,這樣就產生出了描邊效果。

為TextView添加描邊,要用到TextPaint的幾個屬性:

TextPaint paint = outlineTextView.getPaint(); //實例化TextPaint對象paint.setStrokeWidth(15); //設置描邊的寬度paint.setStyle(Paint.Style.STROKE);//設置畫筆屬性為描邊strokeTextView.setTextColor(Color.parseColor(“#000000”)); //設置描邊的顏色(不能與文本顏色一致)

其中strokeTextView為自定義TextView的實例,代碼如下:

1.在構造函數中添加

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);        //設置輪廓文字     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);    //設置輪廓文字    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();    }    //復制原來TextViewg畫筆中的一些參數    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);  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 太湖县| 二手房| 海门市| 吴忠市| 马边| 明水县| 白山市| 阿尔山市| 阳高县| 射阳县| 黄骅市| 阜阳市| 霞浦县| 永寿县| 临颍县| 高邑县| 海口市| 临沭县| 泰兴市| 湖北省| 武穴市| 芦溪县| 竹北市| 定兴县| 甘德县| 台湾省| 淳化县| 东丽区| 蓬溪县| 平昌县| 米泉市| 阿拉尔市| 萨迦县| 绍兴市| 微山县| 伊春市| 辽阳市| 宜宾县| 吉林市| 易门县| 那坡县|