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. MySQL数据库时间查询

    /*当前时间加1毫秒*/ SELECT DATE_ADD(NOW(),INTERVAL 1 MICROSECOND); /*当前时间减1毫秒*/ SELECT DATE_ADD(NOW(),INTER ...

  2. CF Good Bye 2018

    前言:这次比赛爆炸,比赛时各种想多,导致写到\(D\)题时思路已经乱了,肝了\(1\)个多小时都没肝出来,\(B\)题中途因为没开\(long\ long\)又被\(HACK\)了..\(C\)题因为 ...

  3. Oracle 开机自动启动设置

    步骤: 1:查看ORACLE_HOME是否设置 $ echo $ORACLE_HOME /u01/app/oracle/product//dbhome_1 2:执行dbstart 数据库自带启动脚本 ...

  4. MR执行环境有两种

    本地测试环境(windows): 在windows的hadoop目录bin目录有一个winutils.exe 1.在windows下配置hadoop的环境变量 2.拷贝debug工具(winutils ...

  5. IDEA集成git方法

    一.IDEA集成git方法 首先idea集成git我们需要先下载一个小软件,git bash  地址:https://git-scm.com/downloads  .下载好了之后直接下一步下一步傻瓜试 ...

  6. JAVA实训第二次作业

    一维数组的创建和遍历. 声明并创建存放4个人考试成绩的一维数组,并使用for循环遍历数组并打印分数.要求: (1) 首先按"顺序"遍历,即打印顺序为:从第一个人到第四个人: (2) ...

  7. javascript和c#aes加密方法互解

    关键信息如下. javascript function Encrypt() { var key = CryptoJS.enc.Utf8.parse('8080808080808080'); var i ...

  8. Xshell连接不上阿里云服务器

    心血来潮买了一台1核2g内存,外加40g系统盘的阿里云ecs服务器,在配置xshell连接服务器一直无法连接,试了很多种方法,各种心累,不过最后还是找到了原因,是因为在服务器上没有配置安全组规则,附上 ...

  9. SVG制作可爱小页面

    很久都没有在博客园上发表一些自己学的新东西了,只是在有空的时候逛一逛博客园而已,看来我不是一个真正的程序员,哈哈! 但是今天非常想和大家分享一个小东西,那是前两天在一个网页上看到了这个东西 我好奇中间 ...

  10. 安装jdk+tomcat

     linux安装高版本jdk后不起作用的解决办法 1.安装jdk1.8. 2.修改/etc/profile中的JAVA_HOME为新的jdk路径,并执行source /etc/profile. 3.执 ...