最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2017年09月05日 09:03:03   作者:數(shù)碼微訊  
本篇文章主要介紹了Android為TextView添加字體庫(kù)和設(shè)置描邊的方法,具有一定的參考價(jià)值,有興趣的可以了解一下

一、使用系統(tǒng)自帶的字體

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

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)單,無(wú)非就是獲取到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ù)制原來(lái)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)文章

最新評(píng)論

朔州市| 秦皇岛市| 尼勒克县| 广州市| 垫江县| 垫江县| 固阳县| 南皮县| 石家庄市| 巴马| 通州市| 德令哈市| 维西| 绥江县| 灯塔市| 扶沟县| 文山县| 隆安县| 宝鸡市| 南丰县| 枣强县| 集贤县| 犍为县| 葫芦岛市| 兴义市| 黑龙江省| 德江县| 黎城县| 镇原县| 甘德县| 洪雅县| 苏州市| 丰都县| 柳江县| 黄浦区| 黔东| 和政县| 双流县| 南平市| 顺平县| 凯里市|