Android特殊字体引入,以及描边和投影

Android特殊字体引入,以及描边和投影

标签(空格分隔): Android 字体 描边


这篇文章并非纯原创,因为项目原因,忘记记载出处,还望原作者海涵。

主要功能就是实现了特殊字体的描边

字体文件放在Asset文件夹中
这里写图片描述

public class GoldTextView extends TextView {

    TextPaint m_TextPaint;
    int mInnerColor;
    int mOuterColor;

    public GoldTextView(Context context,int outerColor,int innnerColor) {
        super(context);
        m_TextPaint = this.getPaint();
        this.mInnerColor = innnerColor;
        this.mOuterColor = outerColor;
    }

    public GoldTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        m_TextPaint = this.getPaint();
        //获取自定义的XML属性名称
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StrokeTextView);
        //获取对应的属性值
        this.mInnerColor = a.getColor(R.styleable.StrokeTextView_innnerColor,0xffffff);
        this.mOuterColor = a.getColor(R.styleable.StrokeTextView_outerColor,0xffffff);

    }

    public GoldTextView(Context context, AttributeSet attrs, int defStyle,int outerColor,int innnerColor) {
        super(context, attrs, defStyle);
        m_TextPaint = this.getPaint();
        this.mInnerColor = innnerColor;
        this.mOuterColor = outerColor;
        // TODO Auto-generated constructor stub
    }

    private boolean m_bDrawSideLine = true; // 默认采用描边

    /**
     *
     */
    @Override
    protected void onDraw(Canvas canvas) {
        if (m_bDrawSideLine) {
            // 描外层
            // super.setTextColor(Color.BLUE); // 不能直接这么设,如此会导致递归
            setTextColorUseReflection(mOuterColor);
            m_TextPaint.setStrokeWidth(8); // 描边宽度
            m_TextPaint.setStyle(Paint.Style.FILL_AND_STROKE); // 描边种类
            m_TextPaint.setFakeBoldText(true); // 外层text采用粗体
            m_TextPaint.setShadowLayer(1, 0, 0, 0); // 字体的阴影效果,可以忽略
            super.onDraw(canvas);

            // 描内层,恢复原先的画笔

            // super.setTextColor(Color.BLUE); // 不能直接这么设,如此会导致递归
            setTextColorUseReflection(mInnerColor);
            m_TextPaint.setStrokeWidth(0);
            m_TextPaint.setStyle(Paint.Style.FILL_AND_STROKE);
            m_TextPaint.setFakeBoldText(false);
            m_TextPaint.setShadowLayer(0, 0, 0, 0);

        }
        super.onDraw(canvas);
    }

    /**
     * 使用反射的方法进行字体颜色的设置
     * @param color
     */
    private void setTextColorUseReflection(int color) {
        Field textColorField;
        try {
            textColorField = TextView.class.getDeclaredField("mCurTextColor");
            textColorField.setAccessible(true);
            textColorField.set(this, color);
            textColorField.setAccessible(false);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        m_TextPaint.setColor(color);
    }

}

xml文件

<GoldTextView
        android:layout_marginTop="@dimen/y125"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="仅仅是个测试"
        android:shadowColor="#9d4e0c"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="10"
        android:textSize="19sp"
        android:layout_gravity="center_horizontal|top"
        android:textColor="#ffe226"
        app:outerColor="#9d4e0c"
        app:innnerColor="#ffe226"
        android:paddingTop="@dimen/x520"
        android:id="@+id/gold_info_tv"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值