字体描边 终极版
字体
2023-09-27 14:29:19 时间
package com.joyodream.common.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint.Style; import android.text.TextPaint; import android.util.AttributeSet; import android.view.ViewGroup; import android.widget.TextView; /** * * 描边,默认是灰色描边 * * @author lipeilong * */ public class JDStrokeTextView extends TextView { private TextView borderText = null; private final int STROKE_WIDTH = 6; // 线宽度 public JDStrokeTextView(Context context) { super(context); borderText = new TextView(context); init(); } public JDStrokeTextView(Context context, AttributeSet attrs) { super(context, attrs); borderText = new TextView(context,attrs); init(); } public JDStrokeTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); borderText = new TextView(context,attrs,defStyle); init(); } public void init(){ TextPaint tp1 = borderText.getPaint(); tp1.setStrokeWidth(STROKE_WIDTH); tp1.setStyle(Style.STROKE); borderText.setTextColor(0xff333333); borderText.setGravity(getGravity()); } @Override public void setLayoutParams (ViewGroup.LayoutParams params){ super.setLayoutParams(params); borderText.setLayoutParams(params); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { CharSequence tt = borderText.getText(); if(tt== null || !tt.equals(this.getText())){ borderText.setText(getText()); this.postInvalidate(); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); borderText.measure(widthMeasureSpec, heightMeasureSpec); } protected void onLayout (boolean changed, int left, int top, int right, int bottom){ super.onLayout(changed, left, top, right+STROKE_WIDTH, bottom); borderText.layout(left, top, right+STROKE_WIDTH, bottom);// 总要,否则会截断部分文字 } @Override protected void onDraw(Canvas canvas) { borderText.draw(canvas); super.onDraw(canvas); } }
网上找的几种解决方案中,这个是最靠谱的了。使用系统自带的阴影也是可以实现部分效果,看情况选择使用
相关文章
- 讲一讲一种新型的字体渲染方式
- C#学习记录——统一窗体中控件的字体设置
- 【独立开发人员er Cocos2d-x实战 008】BMFont生成位图字体工具和Cocos2dx使用载入fnt文件
- vim,编辑文件后最简单的消除~ 和 .un~后缀文件生成的操作 ,重点为红色字体部分
- Aspose Linux下字体找不到报错
- 《众妙之门——网页排版设计制胜秘诀》——第3章 设置网页字体的5 项原则和方法3.1 系统地进行网页版式设计
- 《HTML5+CSS3网页设计入门必读》——6.4 使用Web字体
- ubuntu安装微软雅黑和Consolas字体
- sublime设置默认字体样式
- 《响应式Web设计:HTML5和CSS3实践指南》——2.12节为字体添加阴影效果
- uniapp nvue开发注意事项:字体设置不生效、背景颜色设置不生效、默认的flex布局、只支持简单class选择器、文本溢出及多行溢出、如何支持border属性
- CSS3的content属性详解及CSS中content使用字体图标
- FontForge 字体编辑器 word wps 处理上下标的不同
- Android 修改 Menu字体颜色
- 在 PdfSharp 中使用私有字体
- cocos2d-x 中 TTF 字体文件的位置