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文字描边实现的更多相关文章

  1. Android TextView文字描边的实现!!

    Android开发:文字描边 转自:http://www.oschina.net/code/snippet_586849_37287 1. [代码][Java]代码 1 2 3 4 5 6 7 8 9 ...

  2. Android:TextView文字跑马灯的效果实现

    解决TextView文字显示不全的问题. 简单设置跑马灯的效果: <TextView android:id="@+id/textView" android:layout_wi ...

  3. 用CSS3实现文字描边

    CSS3作为新兴的前端技术可以实现很多复杂变化的效果,比如文字描边. 这里主要用到text-shadow属性,顾名思义就是为文字加上阴影效果.例: text-shadow:10px 5px 2px # ...

  4. cocos2d-x 利用CCLabelTTF制作文字描边与阴影效果的实现方法

    // // myttf.h// // Created by 王天宇 on 14-6-12. // // #ifndef ____SLG__myttf__ #define ____SLG__myttf_ ...

  5. CSS3文字描边 CSS3字体外部描边

    给需要实现文字描边的元素添加如下CSS3的属性 text-shadow:#000 1px 0 0,#000 0 1px 0,#000 -1px 0 0,#000 0 -1px 0; -webkit-t ...

  6. Android TextView 文字居中

    有2种方法可以设置TextView文字居中: 一:在xml文件设置:android:gravity="center" 二:在程序中设置:m_TxtTitle.setGravity( ...

  7. 常用CSS3效果:用text-shadow做CSS3 文字描边

    思路: 利用CSS3的text-shadow属性,对文字的四个边均用阴影. 最终效果: 单纯的为了实现效果.未作任何美化. 实现代码: HTML: <div>文字描边效果</div& ...

  8. ps怎么给文字描边

    在设计的时候,单一的文字,往往对人没有多少的吸引力,这就需要我们在文字上加一些文字特效,比如说外发光,描边,投影,等等.在这里我们详细的介绍一下文字的输入,和文字描边的怎么增加,删除的经验.(这些方法 ...

  9. 设置TextView文字居中

    有2种方法可以设置TextView文字居中: 一:在xml文件设置:android:gravity="center" 二:在程序中设置:m_TxtTitle.setGravity( ...

随机推荐

  1. 基于角色的访问控制 (RBAC)权限管理

    RBAC(Role-Based Access Control,基于角色的访问控制),就是用户通过角色与权限进行关联.简单地说,一个用户拥有若干角色,每一个角色拥有若干权限.这样,就构造成“用户-角色- ...

  2. linux 网络虚拟化: network namespace 简介

    linux 网络虚拟化: network namespace 简介 network namespace 是实现网络虚拟化的重要功能,它能创建多个隔离的网络空间,它们有独自的网络栈信息.不管是虚拟机还是 ...

  3. mysql约束以及数据库的修改

    一.约束 1.约束保证数据完整性和一致性. 2.约束分为表级约束和列级约束. (1)表级约束(约束针对于两个或两个以上的字段使用) (2)列级约束(针对于一个字段使用) 3.约束类型有: (1)NOT ...

  4. GarageBand mac怎么剪切音频片段? GarageBand mac使用教程

    garageband mac智能控制轻松修饰声音资源库中任何乐器的音色,让你在世界各地都可以开始你的创意,让世界听到你的歌声.GarageBand mac剪切音频片段的操作小伙伴们也是需要掌握的,Ga ...

  5. ssh 免密登录阿里云主机

    在网上找了好几篇教程,都不好使. 终于在这篇找到了答案 解决方案: 在 sshd_config 里面将这一项: AuthorizedKeysFile .ssh/authorized_keys 被我修改 ...

  6. 《Miracle_House》团队项目系统设计改进

    一.团队项目系统设计改进: 1.分析项目系统设计说明书初稿的不足,特别是软件系统结构模型建模不完善内容: 对于原文档中,设计图中存在的错误以及文字描述不准确的地方进行了修改. 2. 团队项目Githu ...

  7. python的语法小结之生成器和迭代器

    生成器: 首先介绍一下列表生成式:a=[x for x in range(10)]               >>>>>>[0, 1, 2, 3, 4, 5, 6 ...

  8. oracle表空间自增

    https://blog.csdn.net/windylfm/article/details/78085669

  9. JAVA Bean和XML之间的相互转换 - XStream简单入门

    JAVA Bean和XML之间的相互转换 - XStream简单入门 背景介绍 XStream的简介 注解简介 应用实例 背景介绍 我们在工作中经常 遇到文件解析为数据或者数据转化为xml文件的情况, ...

  10. 第45章:MongoDB-集群--Sharding(分片)--分片的管理

    ①列出所有的Shard db.runCommand({“listshards”:1}); ②查看分片信息 db.printShardingStatus(); ③判断是否分片 db.runCommand ...