带描边的TextView

来源:互联网 发布:mac动态壁纸 编辑:程序博客网 时间:2024/05/16 08:51

描边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>


 

0 0
原创粉丝点击