记录自定义TextVIew:
这个主要是为了熟悉自定义View流程,实战意义不大
效果图:
1.实现构造方法:
public MyTextView(Context context) {
this(context, null);//重要
}
public MyTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);//重要
}
public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
2.在style中实现需要参数:
<declare-styleable name="MyTextView">
<attr name="width_size" format="dimension"></attr>
<attr name="height_size" format="dimension"></attr>
<attr name="text_size" format="dimension"></attr>
</declare-styleable>
3.完整代码
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;
@SuppressLint("AppCompatCustomView")
public class MyTextView extends TextView {
private int widthDefault;
private int heightDefault;
private int textSizeDefault;
private Rect recf;
public MyTextView(Context context) {
this(context, null);
}
public MyTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);
widthDefault = a.getDimensionPixelSize(R.styleable.MyTextView_width_size,
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics()));
heightDefault = a.getDimensionPixelSize(R.styleable.MyTextView_width_size,
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics()));
textSizeDefault = a.getDimensionPixelSize(R.styleable.MyTextView_text_size,
(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics()));
a.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int wideModel = MeasureSpec.getMode(widthMeasureSpec);
int heightModel = MeasureSpec.getMode(heightMeasureSpec);
int wideSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (wideModel == MeasureSpec.EXACTLY) {
widthDefault = wideSize;
}
if (heightModel == MeasureSpec.EXACTLY) {
heightDefault = heightSize;
}
setMeasuredDimension(widthDefault, heightDefault);
}
@Override
protected void onDraw(Canvas canvas) {
int r = getMeasuredWidth() / 2;
int centerX = getLeft() + r;
int centerY = getTop() + r;
Paint paint = new Paint();
paint.setColor(Color.BLUE);
recf = new Rect();
paint.getTextBounds(getText().toString(), 0, getText().toString().length(), recf);
if (recf.width() > r) {
paint.setTextSize(
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 13, getResources().getDisplayMetrics()));
paint.getTextBounds(getText().toString(), 0, getText().toString().length(), recf);
}
canvas.drawText(getText().toString(), r - recf.width() / 2, centerY, paint);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLACK);
canvas.drawCircle(centerX, centerY, r, paint);
}
}