打赏

android textview描边1

复制代码
package com.example.myapplication1.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * @ClassName StrokeTextView2
 * @Description TODO
 * @Author ZhangXueTao
 * @Date 2020/9/15 14:05
 * @Version 1.0 模板14-3
 */

/**
 * 模板13-3
 */
@SuppressLint("AppCompatCustomView")
public class StrokeTextView13_32 extends TextView {


    private TextView outlineTextView = null;
    private TextPaint strokePaint;
    Context context;
    public StrokeTextView13_32(Context context) {
        super(context);
        this.context = context;
        outlineTextView = new TextView(context);
    }

    public StrokeTextView13_32(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.context = context;
        outlineTextView = new TextView(context, attrs);
    }

    public StrokeTextView13_32(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        this.context = context;
        outlineTextView = new TextView(context, attrs, defStyle);
    }

    @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);

        AssetManager manager = context.getAssets();
        String path = "font/arlrdbd.TTF";
        Typeface type = Typeface.createFromAsset(manager, path);

        //设置轮廓文字
        CharSequence outlineText = outlineTextView.getText();
        if (outlineText == null || !outlineText.equals(this.getText())) {
            outlineTextView.setText(getText());
            outlineTextView.setTypeface(type);
            setTypeface(type);
            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) {
        AssetManager manager = context.getAssets();
        String path = "font/arlrdbd.TTF";
        Typeface type = Typeface.createFromAsset(manager, path);

        if (strokePaint == null) {
            strokePaint = new TextPaint();
        }
        //复制原来TextViewg画笔中的一些参数
        TextPaint paint = getPaint();
        strokePaint.setTextSize(paint.getTextSize());
        strokePaint.setTypeface(type);
        strokePaint.setFlags(paint.getFlags());
        strokePaint.setAlpha(paint.getAlpha());

        //自定义描边效果
        strokePaint.setStyle(Paint.Style.STROKE);
        strokePaint.setColor(Color.parseColor("#46982E"));
        strokePaint.setStrokeWidth(10);

        String text = getText().toString();

        //在文本底层画出带描边的文本
        canvas.drawText(text, (getWidth() - strokePaint.measureText(text)) / 2,
                getBaseline(), strokePaint);
        super.onDraw(canvas);
    }
}
复制代码

 

posted @   YY2000  阅读(275)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示