带描边的TextView

描边TextView

(1)在attrs.xml中添加StrokeText属性

<declare-styleable name="StrokeText">
       <attr name="StrokeColor" format="reference|color"/>
       <attr name="TextColor" format="reference|color"/>
</declare-styleable>


(2)新建类继承TextView

public class StrokeTextView extends TextView{
       private int strokeColor;
       private int textColor;

       public StrokeTextView(Context context){
            super(context);
       }

       @SuppressLint("NewApi")
       public StrokeTextView(Context context,AttributeSet attrs,int defStyleAttr,int defStyleRes){
              super(context,attrs,defStyleAttr)
       }

       public StrokeTextView(Context context,AttributeSet attrs,int defStyleAttr){
              super(context,attrs,defStyleAttr)
       }

       public StrokeTextView(Context context,AttributeSet attrs){
              super(context,attrs);
              TypedArray typeArray=context.obtainStyledAttributes(attrs,R.styleable.StrokeText);

              textColor=typeArray.getColor(R.styleable.StrokeText_TextColor,0XFFFFFFFF);

              strokeColor=typeArray.getColor(R.styleable.StrokeText_StrokeColor,0XFFFFFFFF);

       }

       @Override
       protected void onDraw(Canvas canvas){
            //描外边
             setTextColorUseReflection(strokeColor);
             getPaint().setStrokeWidth(3);
             getPaint().setStyle(Style.FILL_AND_STROKE);
             getPaint().setFakeBoldText(true);
             getPaint().setShadowLayer(1,0,0,0);
              super.onDraw(canvas);

            //描内边
             setTextColorUseReflection(textColor);
             getPaint().setStrokeWidth(0);
             getPaint().setStyle(Style.FILL_AND_STROKE);
             getPaint().setFakeBoldText(false);
             getPaint().setShadowLayer(0,0,0,0);
              super.onDraw(canvas);
      }

       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();
              }
              getPaint().setColor(color);
       }
}


(3)layout.xml中调用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:myspace="http://schemas.android.com/apk/res/com.crelead.ordersys"
         android:layout_width="match_parent"
         android:layout_height="match_parent" >

         <com.rainbow.sys.view.StrokeTextView
                 android:id="@+id/strokeText"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:layout_alignLeft="@+id/textView"
                 android:layout_alifnParentTop="true"
                 android:text="StrokeTextView"
                 myspace:StrokeColor="@color/black_color"
                 myspace:TextColor="@color/golden_color" />       

</RelativeLayout>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值