1.首先在res的values文件下新建一个名为attrs.xml文件
在该xml文件中编写我们需要的属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyTextView">
<attr name="radius" format="integer" />
<attr name="color" format="color" />
<attr name="frameColor" format="color"/>
<attr name="isOver" format="boolean"/>
<attr name="frameWidth" format="integer"/>
</declare-styleable>
</resources>
declare-styleable后面的name必须要与接下来要自定义的view名一致。
attr 后面的name表示需要自定义的属性,format表示这些属性的类型
2.新建一个类继承textview
public class MyTextView extends TextView {
private Paint paint;
private float radius ;
private boolean isOver = false;
private float frameWidth ;
private int color ;
private Paint framePaint = new Paint();
private int frameColor;
private boolean isClick;
private RectF rectF;
public MyTextView(Context context) {
this(context, null);
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
setClickable(true);
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.MyTextView);
radius = ta.getInteger(R.styleable.MyTextView_radius,10);
color = ta.getColor(R.styleable.MyTextView_color,Color.parseColor("#00000000"));
frameColor = ta.getColor(R.styleable.MyTextView_frameColor,Color.parseColor("#008577"));
isOver = ta.getBoolean(R.styleable.MyTextView_isOver,false);
frameWidth = ta.getInteger(R.styleable.MyTextView_frameWidth,0);
ta.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(color);
rectF = new RectF(frameWidth/2,frameWidth/2,getWidth()-frameWidth/2,getHeight()-frameWidth/2);
canvas.drawRoundRect(rectF,radius,radius,paint);
if(isOver){
framePaint.setStyle(Paint.Style.STROKE);
framePaint.setStrokeWidth(frameWidth);
framePaint.setAntiAlias(true);
framePaint.setDither(true);
framePaint.setColor(frameColor);
RectF frameRectF = new RectF(frameWidth/2,frameWidth/2,getWidth()-frameWidth/2,getHeight()-frameWidth/2);
canvas.drawRoundRect(frameRectF,radius,radius,framePaint);
}
if (isClick) {
paint.setColor(Color.parseColor("#22000000"));
canvas.drawRoundRect(rectF,radius,radius,paint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == event.ACTION_DOWN|| event.getAction() == event.ACTION_MOVE) {
isClick = true;
} else {
isClick = false;
}
invalidate();
return super.onTouchEvent(event);
}
}
其中以下代码用于获取自定义属性的值
TypedArray ta = context.obtainStyledAttributes(attrs,
R.styleable.MyTextView);
radius = ta.getInteger(R.styleable.MyTextView_radius,10);
color = ta.getColor(R.styleable.MyTextView_color,Color.parseColor("#00000000"));
frameColor = ta.getColor(R.styleable.MyTextView_frameColor,Color.parseColor("#008577"));
isOver = ta.getBoolean(R.styleable.MyTextView_isOver,false);
frameWidth = ta.getInteger(R.styleable.MyTextView_frameWidth,0);
ta.recycle();
3.在Activity中使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent">
<com.mytest.MyTextView
android:id="@+id/text"
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_marginLeft="100dp"
app:radius="10"
app:isOver="true"
app:color="#FF00"
app:frameWidth="10"
android:text="1213141441"/>
</LinearLayout>