一、使用系统自带的字体

开发Android的人大多都知道,Android里面对字体的支持少得可怜,默认情况下,TextView 的 typeface 属性支持 sans、serif和monospace 这三种字体,如果在没有指定字体的情况下,系统会使用 sans 作为文本显示的字体。但这三种字体只支持英文,也就是说只要你显示的文字是中文,无论你选择这三种字体中的哪一种,显示效果都是一样的。

1.在XML文件中设置

android:id="@+id/sans"

android:text="Hello,World"

android:textSize="20sp"

android:typeface="sans" />

android:id="@+id/serif"

android:text="Hello,World"

android:textSize="20sp"

android:typeface="serif" />

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代码中实现 //实例化TextView

TextView textView = findViewById(R.id.textview);

//得到AssetManager

AssetManager mgr=getAssets();

//根据路径得到Typeface

Typeface tf=Typeface.createFromAsset(mgr, "fonts/pocknum.ttf");

//设置字体

textView.setTypeface(tf);

2.引入字体库后的效果图

三、为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);

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持聚米学院。

python字体描边_Android为TextView添加字体库和设置描边的方法相关推荐

  1. java字体描边_Android为TextView添加字体库和设置描边

    一.使用系统自带的字体 开发Android的人大多都知道,Android里面对字体的支持少得可怜,默认情况下,TextView 的 typeface 属性支持 sans.serif和monospace ...

  2. 增加字库 安卓_Android为TextView添加字体库和设置描边的方法

    一.使用系统自带的字体 开发Android的人大多都知道,Android里面对字体的支持少得可怜,默认情况下,TextView 的 typeface 属性支持 sans.serif和monospace ...

  3. Python视频编辑神器:全面分析ffmpeg-python库的安装与使用方法

    Python视频编辑神器:全面分析ffmpeg-python库的安装与使用方法 在Python中进行视频编辑是一件令人兴奋的事情,它能够让你深入了解和控制你想要的任何一帧视频.而在Python视频编辑 ...

  4. python爬虫网络请求超时_python总urllib,伪装,超时设置,异常处理的方法

    python爬虫之urllib,伪装,超时设置,异常处理的方法 Urllib 1. Urllib.request.urlopen().read().decode() 返回一个二进制的对象,对这个对象进 ...

  5. html5字体修改webview,WebView中修改字体

    一般情况下我们做加载网页都是返回一个url,我们进行加载.各种各样的样式都已经在网页中做了处理.但是,也会碰到一些情况,需要我们加载代码片段.还有修改一些样式,比如:修改网页中的字体.当然,本文中的修 ...

  6. java 画图 文字 描边_Android开发:文字描边

    1.[代码][Java]代码 package com.example.testproject; import android.content.Context; import android.graph ...

  7. PHP生成海报 文字描边,海报字体 怎么为海报字体添加描边样式?海报文字描边怎么做?海报字体描边样式制作...

    今天是十月的最后一天啦,转眼2019年只剩下两个月了,这时间啊,走得真快,还没好好感受呢,都快要2020年了.而小编倒好,还觉得现在是2018年呢~哈哈.好啦,不说废话了,还是来看看今天的教程方案吧! ...

  8. TextView系列:TextView卡通字体描边效果

    今天做一个儿歌MV,蓝湖设计稿如下图: 可以看到需要做描边效果,同时用到对应的字体,字体需要下载或者找设计师要. 原理是在红色字体后面再放一个白色的TextView 主要的坑是:红色的TextView ...

  9. delphi7 中文注释字体_使用nerd-font/font-patcher为字体添加字体图标

    Nerd-fonts常用来在终端下显示各种图标,这个项目的github repo下提供了许多Nerd Font字体,图标使用效果如图. 由于目前Windows Terminal还不支持设置第二字体,要 ...

最新文章

  1. 最短路径-Dijkstra算法与Floyd算法
  2. Python基础教程:线程操作(oncurrent模块)详解
  3. Python脚本配合Linux计划任务工作
  4. python中str是什么函数_python str函数怎么用
  5. TCP/IP、Http的区别
  6. “dedeCMS 提示信息!”跳转页,如何修改文字?
  7. linux 系统yum下安装vnc
  8. 基于Python的电子教室软件中远程关机功能的原理与实现
  9. 程序员渴望的“无代码世界”要来了!
  10. 机器学习正面临着可重现性危机!
  11. caffe数据格式(Google Protocol Buffers)
  12. 在本地视频播放中硬解与软解的能耗比体现
  13. 探索的乐趣(物理笔记)
  14. 文件上传到ftp服务器命令,ftp上传文件到服务器命令
  15. (原創) 如何控制TRDB-LTM輸出時某座標的顏色? (SOC) (DE2-70) (TRDB-LTM)
  16. 大脑神经网络记忆原理图,记忆力机制的神经网络
  17. 中国大学mooc慕课python答案_中国大学MOOC(慕课)Python编程基础答案
  18. 钉钉windows端多开软件_电脑便签需要下载吗?电脑上用什么桌面便签软件工具好...
  19. Go Context 原理详解
  20. 读《沟通的方法》推荐序有感

热门文章

  1. jmeter性能案例一登录百度
  2. 把一个学生的信息(包括学号、姓名、性别、住址)放在一个结构体变量中,然后输出这个学生的信息
  3. 没有机房的计算机课,没有电脑,信息技术课怎么上?
  4. 树莓派空气质量检测之——GP2Y1010AU0F粉尘传感器模块的使用记录
  5. vlc播放器或者web实现rtmp拉流
  6. 开源的 智能卡 COS系统源码
  7. OpenAI Gym--Classical Control 环境详解
  8. java接口设计规范_关于团队API接口规范设计
  9. css什么是hack,CSS中hack是什么意思
  10. 【老生常谈】一些见解和经验之谈收录