溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2020-10-17 12:19:57 來源:腳本之家 閱讀:162 作者:數(shù)碼微訊 欄目:移動開發(fā)

一、使用系統(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.引入字體庫后的效果圖

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

三、為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í)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI