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

本文介绍了如何在Android中为TextView添加自定义字体库,包括在XML和Java代码中设置系统字体,以及如何在assets目录下引入ttf字体文件。此外,还详细讲解了如何为TextView实现描边效果,通过创建自定义的StrokeTextView类,利用TextPaint属性设置描边宽度、颜色和风格。
摘要由CSDN通过智能技术生成

一、使用系统自带的字体

开发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.引入字体库后的效果图

bceb0fc6aa48fe83680234a817d42d1c.png

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

}

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值