自定义属性:
<declare-styleable name="MyCornerTextView">
<attr name="borderWidth" format="dimension"/>
<attr name="borderWidthColor" format="color"/>
<attr name="cornersize" format="dimension"/>
<!-- 1:边框的宽度 2:边框的颜色 4:边角的大小 -->
</declare-styleable>
自定义View:
package com.example.textviewtest;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Created by mars on 2017/6/22.
*/
public class MyCornerTextView extends TextView {
private int mBorderWidth = 1;
private int mBorderWidthColor = Color.YELLOW;
private int mCornersize = 8; //我们默认以px为单位
private Paint mCornerPaint; //边框画笔 文字我们只用系统的 TextView
@Override
public boolean removeCallbacks(Runnable action) {
return super.removeCallbacks(action);
}
public MyCornerTextView(Context context) {
this(context,null);
}
public MyCornerTextView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public MyCornerTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttrs(context,attrs);
}
private void initAttrs(Context context, AttributeSet attrs) {
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyCornerTextView);
mBorderWidth = (int) array.getDimension(R.styleable.MyCornerTextView_borderWidth,mBorderWidth);
mBorderWidthColor = array.getColor(R.styleable.MyCornerTextView_borderWidthColor,mBorderWidthColor);
mCornersize = (int) array.getDimension(R.styleable.MyCornerTextView_cornersize,mCornersize);
array.recycle();
mCornerPaint = new Paint();
mCornerPaint.setAntiAlias(true);
mCornerPaint.setDither(true);
mCornerPaint.setStrokeWidth(mBorderWidth);
mCornerPaint.setStyle(Paint.Style.FILL_AND_STROKE); //实心 只画边框也画心
mCornerPaint.setColor(mBorderWidthColor);
}
//这里我们不去测量
// @Override
// protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// }
@Override
protected void onDraw(Canvas canvas) {
RectF rectF = new RectF(mBorderWidth/2,mBorderWidth/2,getMeasuredWidth()-mBorderWidth,getMeasuredHeight()-mBorderWidth);
canvas.drawRoundRect(rectF,mCornersize,mCornersize,mCornerPaint);
super.onDraw(canvas);
}
/**
* 设置 corner 边角
* @param size
*/
public void setCornerSize(int size) { //设置的单位默认是px
mCornersize =size;
// invalidate();
}
//设置颜色
public MyCornerTextView setfilColor (int color) {
this.mBorderWidthColor = color;
mCornerPaint.setColor(mBorderWidthColor);
return this;
// invalidate();
}
}
布局中使用:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.textviewtest.MyCornerTextView
android:layout_centerHorizontal="true"
android:id="@+id/corner_view"
android:layout_margin="5dp"
android:gravity="center"
android:layout_width="120dp"
android:layout_height="40dp"
android:text="登陆" >
</com.example.textviewtest.MyCornerTextView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/corner_view"
android:orientation="horizontal"
>
<TextView
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是一个兵"
/>
</LinearLayout>
</RelativeLayout>
代码中使用:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyCornerTextView corner_view = (MyCornerTextView) findViewById(R.id.corner_view);
// corner_view.setCornerSize(10);
corner_view.setfilColor(Color.RED).setCornerSize(15);
corner_view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "圆角TextView测试", 0).show();
}
});
}