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. android webview字体大小的控制
  2. android中添加arial字体(非android默认字体)
  3. TextView字体逐渐变淡点以及程序特定listView一个item以及 背景
  4. Android TextView 一些字体颜色、大小设置属性
  5. Android Studio设置界面风格和字体大小
  6. android根据字体大小设置控件高度
  7. Android 开发中怎么使用自定义字体?
  8. Android 增加中文字体
  9. android TextView的字体颜色设置的多种方法(续)

随机推荐

  1. xpages多附件接收
  2. 关于JQuery jqGrid colModel 遇到的一个
  3. 在线请教调用Jquery错误:TypeError: a is
  4. zTree -- jQuery 树插件 使用小结
  5. js/jquery操作div显示或隐藏
  6. Java程序员的JavaScript学习笔记(12——jQ
  7. 尝试隐藏表中的行时jQuery性能
  8. ASP.Net MVC与HTML + KnockoutJS + WebAP
  9. JSP中如何使用JQuery?
  10. Dreamweaver Cs4 jQuery自动提示插件绿色