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( ...
随机推荐
- 手工脱壳之AsPack压缩脱壳-随机基址
一.工具及壳介绍二.脱壳1.ESP定律脱壳2.单步跟踪脱壳3.基址重定位的修复 一.工具及壳介绍 使用工具:Ollydbg.PEID.ImportREC.LoadPE.010 Editor 查看待脱壳 ...
- .Net 配置的简陋解决方案
公司是做CS产品的, 最近分配给我一个活, 要求: 1. 公司程序启动时, 检测是否有配置文件, 没有的话则按默认值创建一个 2. 配置文件要加密, 不能让客户随便看到里面的参数 ...
- Java多线程中static变量的使用
轉:https://blog.csdn.net/yy304935305/article/details/52456771 鲁迅先生曾说过:“时间就像海绵里的水,只要愿挤,总还是有的”.不管肿(怎)么说 ...
- Rendering Problems Couldn't resolve resource @dimen/y20
(转)文章转自 songzi1228 的 https://blog.csdn.net/songzi1228/article/details/80255058 本人亲自试用,妥妥的解决了我的问题. R ...
- docker安装nginx和php
参考文章:https://www.cnblogs.com/boundless-sky/p/7182410.html 1.下载镜像docker pull nginxdocker pull php:7.2 ...
- HTML5学习路线导航
一.基本标签元素 1.基础标签第一篇 2.基础标签第二篇 3.表单form的使用 4.新增表单验证 二.CSS样式表 4.CSS插入样式表的三种格式 5.六大选择器 6.样式内容详细讲解 7.背景渐进 ...
- SOP - Validation
Table of Contents目录表1 Roles and Responsibilities related to validation与验证相关的1个角色和职责2 Introduction2引言 ...
- HTML表单标签
<form>标签 1.在HTML中,<form></form>标记对用来创建一个表单,即定义表单的开始和结束位置,在标记对之间的一切都属于表单的内容.每个表单元素开 ...
- [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compile (default-compile) on project taotao-manager-pojo: Compilation failure
运行maven项目时报错 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.2:compi ...
- Exp9 Web安全基础
Exp9 Web安全基础 20154305 齐帅 一.实验要求 本实践的目标理解常用网络攻击技术的基本原理. Webgoat实践下相关实验: [目录] [第一部分 WebGoat 8.0] 1.Web ...