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.结果显示

原文地址:https://www.cnblogs.com/zhangmiao14/p/9538879.html

时间: 2024-08-29 23:34:18

TextView文字描边实现的相关文章

自定义进度条\文字描边样式\文字上下滚动TextSwithcher的应用

一.自定义进度条 1.<ProgressBar         android:id="@+id/patch_progress"         style="@style/gxProgressStyle"         android:layout_width="match_parent"         android:layout_height="12dp"         android:layout_alig

文字描边--效果

package com.bn.ex12d; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint.Style; import android.text.TextPaint; import android.util.AttributeSet; import android.view.ViewGroup; import android.widget.TextView;

ps怎么给文字描边

在设计的时候,单一的文字,往往对人没有多少的吸引力,这就需要我们在文字上加一些文字特效,比如说外发光,描边,投影,等等.在这里我们详细的介绍一下文字的输入,和文字描边的怎么增加,删除的经验.(这些方法不只适用于文字,图片也可以的) 步骤阅读 百度经验:jingyan.baidu.com 工具/原料 电脑 photoshop 百度经验:jingyan.baidu.com 方法/步骤 1 首先我们要新建一个图层,文件>新建,建一个属于自己的文档. 步骤阅读 2 然后利用左侧的T(文字工具),输入自己

WPF文字描边的解决方法

 由于项目原因,今天研究了一下午WPF的文字描边,网上这方面的资料奇少,搞了半天才发现强大的WPF原来不直接支持文字描边啊.最后求助于MSDN,找到了方案,和大家分享一下: 主要思路:用FormattedText将字符串转换为Geometry,再在重写的OnRender(DrawingContext drawingContext)方法中绘制Geometry.效果如图. 组件的主要属性: Text属性设置文字 Fill属性设置文本本身的画刷 Stroke属性是描边画刷 StrokeThickn

Android TextView文字空格

 表示全角空格, <string name="aaa">你好      啊</string> http://stackoverflow.com/questions/1587056/android-string-concatenate-how-to-keep-the-spaces-at-the-end-and-or-beginnin 1.Even if you use string formatting sometimes you still need white

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

解决TextView文字显示不全的问题. 简单设置跑马灯的效果: <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:focusable="true" androi

08.05 边框图片 盒子倒影 新增属性 文本阴影 文本属性 文本换行 文本溢出 文本修饰 文字描边 其他属性

---恢复内容开始--- ### 边框图片 * border-image-source   图片地址 * border-image-slice     图片截取方式 值 浮点数/百分比 * border-iamge-width   边框图片厚度  值 长度单位 * border-image-outset   外延   值 长度单位 * borde-image-repeat    延伸方式  值 stretch/repeat/round/space * border-image border-im

iOS文字描边

当label用系统的字体干干巴巴的放在那会很难看,有时候我们需要给文字描边,让文字变得更漂亮 想用文字想要有描边的话就让你的label继承自CTWBLabe可以了,而且文字颜色和描边颜色可以自定义. 废话不多,直接上代码: // //  CTWBLabel.m //  SpeedPro // //  Created by WBapple on 16/5/17. //  Copyright © 2016年 Huawei. All rights reserved. // #import "CTWBL

TextView过长显示省略号, TextView文字中间加横线

1.TextView显示的内容过长时自己主动显示省略号: 省略号的位置: android:ellipsize="end"   省略号在结尾 android:ellipsize="start" 省略号在开头 android:ellipsize="middle"    省略号在中间 TextView显示的行数: android:singleline="true" android:lines="2" 在java文