TextView文字描边实现
TextView文字描边实现
需求描述
文字显示在图片的上面,图片的内容是不确定了,为了防止文字与图片的颜色相近导致用户看不到或者看不清文字的问题,所以显示文字描边,避免问题。
实现
实现思想
使用TextPaint绘制相同文字在TextView的底部,TextPaint的字显示要比原始的字大一些,这样看起来就像是有描边的文字。
代码
1.attrs.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 有描边的自定义TextView-->
<declare-styleable name="StrokeTextView">
<!--描边的颜色 -->
<attr name="stroke_color" format="color" />
<!-- 描边的宽度 -->
<attr name="stroke_width" format="dimension" />
</declare-styleable>
</resources>
2.StrokeTextView的实现
package com.zm.autostroketextview;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* 文字内容有描边的TextView
* Author: zhangmiao
* Date: 2018/4/13
*/
public class StrokeTextView extends TextView {
private TextView outlineTextView = null;
public StrokeTextView(Context context) {
this(context, null);
}
public StrokeTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public StrokeTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
outlineTextView = new TextView(context, attrs, defStyleAttr);
init(attrs);
}
private void init(AttributeSet attrs) {
//1.获取参数
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.StrokeTextView);
int stroke_color = ta.getColor(R.styleable.StrokeTextView_stroke_color, Color.WHITE);
float stroke_width = ta.getDimension(R.styleable.StrokeTextView_stroke_width, 2);
//2.初始化TextPaint
TextPaint paint = outlineTextView.getPaint();
paint.setStrokeWidth(stroke_width);
paint.setStyle(Paint.Style.STROKE);
outlineTextView.setTextColor(stroke_color);
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(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);
}
}
3.布局文件中StrokeTextView的使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">
<com.zm.autostroketextview.StrokeTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:stroke_color="@android:color/white"
app:stroke_width="2dp" />
</LinearLayout>
4.结果显示
TextView文字描边实现的更多相关文章
- Android TextView文字描边的实现!!
Android开发:文字描边 转自:http://www.oschina.net/code/snippet_586849_37287 1. [代码][Java]代码 1 2 3 4 5 6 7 8 9 ...
- Android:TextView文字跑马灯的效果实现
解决TextView文字显示不全的问题. 简单设置跑马灯的效果: <TextView android:id="@+id/textView" android:layout_wi ...
- 用CSS3实现文字描边
CSS3作为新兴的前端技术可以实现很多复杂变化的效果,比如文字描边. 这里主要用到text-shadow属性,顾名思义就是为文字加上阴影效果.例: text-shadow:10px 5px 2px # ...
- cocos2d-x 利用CCLabelTTF制作文字描边与阴影效果的实现方法
// // myttf.h// // Created by 王天宇 on 14-6-12. // // #ifndef ____SLG__myttf__ #define ____SLG__myttf_ ...
- CSS3文字描边 CSS3字体外部描边
给需要实现文字描边的元素添加如下CSS3的属性 text-shadow:#000 1px 0 0,#000 0 1px 0,#000 -1px 0 0,#000 0 -1px 0; -webkit-t ...
- Android TextView 文字居中
有2种方法可以设置TextView文字居中: 一:在xml文件设置:android:gravity="center" 二:在程序中设置:m_TxtTitle.setGravity( ...
- 常用CSS3效果:用text-shadow做CSS3 文字描边
思路: 利用CSS3的text-shadow属性,对文字的四个边均用阴影. 最终效果: 单纯的为了实现效果.未作任何美化. 实现代码: HTML: <div>文字描边效果</div& ...
- ps怎么给文字描边
在设计的时候,单一的文字,往往对人没有多少的吸引力,这就需要我们在文字上加一些文字特效,比如说外发光,描边,投影,等等.在这里我们详细的介绍一下文字的输入,和文字描边的怎么增加,删除的经验.(这些方法 ...
- 设置TextView文字居中
有2种方法可以设置TextView文字居中: 一:在xml文件设置:android:gravity="center" 二:在程序中设置:m_TxtTitle.setGravity( ...
随机推荐
- 纯css3单选框/复选框美化样式代码
纯CSS 单/复选框 美化请选择iPhone 型号 iPhone 6s iPhone 6s Plus iPhone 7 iPhone 7 Plus 选择兴趣爱好 女 绘画 摄影 骑行 原理在这 ...
- java对象之----(PO,VO,DAO,BO,POJO)
转自http://www.cnblogs.com/bluestorm/archive/2012/09/26/2703234.html 一.PO :(persistant object ),持久对象 可 ...
- LR回放https协议脚本失败:[GENERAL_MSG_CAT_SSL_ERROR]connect to host "XXX" failed:[10054] Connection reset by peer [MsgId:MERR-27780]
Loadrunner默认发送是通过sockets(将http转换为sockets)发送的,而sockets默认SSL的版本为SSL2和SSL3.HTTPS协议录制的脚本以SSL3版本回放时会使sock ...
- Python-docx 读取word.docx内容
第一次写博客,也不知道要写点儿什么好,所以就把我在学习Python的过程中遇到的问题记录下来,以便之后查看,本人小白,写的不好,如有错误,还请大家批评指正! 中文编码问题总是让人头疼,想要用Pytho ...
- pd16.5增加字段备注
EXEC sp_addextendedproperty 'MS_Description', %.q:COLNNAME%, 'user', dbo, 'table', %TABLE%, 'column' ...
- Python Day 9
阅读目录: 内容回顾 内存管理 ##内容回顾 #文件处理 #1.操作文件的三步骤 -- 打开文件:硬盘的空间被操作系统持有 | 文件对象被应用程序持续 -- 操作文件:读写操作 -- 释放文件:释放操 ...
- python Flask框架mysql数据库配置
我是一个没有笔记习惯的低级程序员,但是我还是喜欢编程,从小学就开始跟着玩电脑,对抓鸡,ddos,跳板刷钻开始了自己的IT 旅程,之后学习了各种语言,但是可惜都不没有达到精通,都是略懂一二,现在想把Py ...
- zepto 源码 $.contains 学习笔记
$.contains(parent,node) 返回值为一个布尔值 ==> boolean parent,node我们需要检查的节点检查父节点是否包含给定的dom节点,如果两者是相同的节点,返 ...
- go的基本数据类型
一,数据类型的介绍 在go语言中,数据类型是用于声明函数和变量的:数据类型是为了把数据分成所需内存不同大小的数据,除了在需要使用大数据的时候才会申请大内存,这样就会充分的使用内存 Go 语言按类别有以 ...
- centos7安装配置nfs
操作系统版本:3.10.0-123.el7.x86_64 192.168.137.11 nfs服务端 192.168.137.10 nfs客户端 一.安装nfs服务端(在192.168.137.1 ...