字体描边效果研究

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现 TextView 字体描边的方法有多种,以下是一种常见的方法: 1. 在 res/values 文件夹下新建一个名为 attrs.xml 的文件,添加以下代码: ``` <resources> <attr name="strokeWidth" format="dimension"/> <attr name="strokeColor" format="color"/> </resources> ``` 2. 在 TextView 标签中添加以下属性: ``` <com.example.myapp.MyTextView android:id="@+id/myTextView" android:text="Hello World!" android:textSize="24sp" app:strokeWidth="2dp" app:strokeColor="#000000" android:textColor="#ffffff"/> ``` 其中,MyTextView 是自定义的 TextView 类,继承自 android.widget.TextView。 3. 在 MyTextView 类中重写 onDraw 方法,实现字体描边效果: ``` public class MyTextView extends TextView { private float strokeWidth; private int strokeColor; public MyTextView(Context context) { super(context); } public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyTextView); strokeWidth = a.getDimension(R.styleable.MyTextView_strokeWidth, 0); strokeColor = a.getColor(R.styleable.MyTextView_strokeColor, Color.BLACK); a.recycle(); } @Override protected void onDraw(Canvas canvas) { TextPaint paint = getPaint(); // 设置描边效果 paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(strokeWidth); paint.setColor(strokeColor); // 画出描边效果 super.onDraw(canvas); // 恢复原有的颜色和样式 paint.setStyle(Paint.Style.FILL); paint.setColor(getCurrentTextColor()); super.onDraw(canvas); } } ``` 注意,在重写 onDraw 方法时,需要先设置描边效果,再调用父类的 onDraw 方法画出描边效果,最后恢复原有的颜色和样式再次调用父类的 onDraw 方法,以保证正常显示字体

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值