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文件

"@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"/>

更多相关文章

  1. NPM 和webpack 的基础使用
  2. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  3. android kernel+busybox
  4. android ZygoteInit.java文件解析 从main(String args[])开始
  5. Android(安卓)-Recovery
  6. Android(安卓)ApiDemos 系列解析【View-ImageView/ImageButton】
  7. 在 Eclipse 中导入 Android(安卓)示例程序
  8. android nfc中MifareClassic格式的读写
  9. android背景选择器selector用法汇总

随机推荐

  1. android长度单位
  2. android telephony 之 UICC 卡数据读写及
  3. Android_android签名证书的sha1值获取
  4. android Application Component研究之Con
  5. [置顶] 编译自己的Windows Android(安卓)
  6. Android消息处理机制(二):循环和消息队列的
  7. Android支付——支付宝支付总结
  8. Android(安卓)Touch系统简介(二):实例详解o
  9. LinearLayout中组件右对齐
  10. AOSP源码编译 --全部编译