创建自定义的view
private String mText;
private int mTextSize = 15;
private int mTextColor = Color.BLACK;
private Paint mPaint;
public myView(Context context) {
this(context,null);
// new的时候调用
}
public myView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
// xml中调用
}
public myView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// 获取自定义属性
TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.myView);
mText = array.getString(R.styleable.myView_mtext);
mTextColor = array.getColor(R.styleable.myView_mtextColor,mTextColor);
mTextSize = array.getDimensionPixelSize(R.styleable.myView_mtextSize,sp2px(mTextSize));
array.recycle();
mPaint = new Paint();
// 抗锯齿
mPaint.setAntiAlias(true);
mPaint.setTextSize(mTextSize);
mPaint.setColor(mTextColor);
}
private int sp2px(int sp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,
getResources().getDisplayMetrics());
}
/**
* 自定义view的测量方法
*
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 获取宽高的模式
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
// MeasureSpec.AT_MOST: wrap_content
// MeasureSpec.EXACTLY: // 具体的值 match_parent
// MeasureSpec.UNSPECIFIED : 尽可能大,很少用到 ListView ScrollView
// widthMeasureSpec和heightMeasureSpec包含32位值 前两位是模式 后两位是值
// 获取宽高的值
int width = MeasureSpec.getSize(widthMeasureSpec);
if(widthMode == MeasureSpec.AT_MOST){
// 计算宽度 与字体的长度有关
Rect bounds = new Rect();
mPaint.getTextBounds(mText,0,mText.length(),bounds);
width = bounds.width();
}
int height = MeasureSpec.getSize(heightMeasureSpec);
if(heightMode == MeasureSpec.AT_MOST){
// 计算高度 与字体的高度有关
Rect bounds = new Rect();
mPaint.getTextBounds(mText,0,mText.length(),bounds);
height = bounds.height();
}
//最后设置宽高
setMeasuredDimension(width + getPaddingLeft() + getPaddingRight(),height + getPaddingBottom() + getPaddingTop());
}
/**
* 用于绘制
*
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//drawText 中存在四个参数分别是: 要显示的文本,基线x方向的起始点,基线y方向的起始点,画笔
Paint.FontMetricsInt fontMetricsInt = mPaint.getFontMetricsInt();
//botto 为正值 top 为负值(看图中画的坐标系)
int dy = (fontMetricsInt.bottom - fontMetricsInt.top)/2 - fontMetricsInt.bottom;
int baseLine = getHeight()/2 + dy;
float dx = getPaddingLeft(); //修改的地方
canvas.drawText(mText,dx,baseLine,mPaint); //修改的地方
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
//手指抬起
break;
case MotionEvent.ACTION_MOVE:
//手指移动
break;
case MotionEvent.ACTION_DOWN:
//手指按下
break;
}
return super.onTouchEvent(event);
}
}
2.在res下的values下面新建attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="myView">
<attr name="text" format="string"/>
<attr name="textColor" format="color"/>
<attr name="textSize" format="dimension"/>
<attr name="background" format="reference|color"/>
</declare-styleable>
</resources>
3 在布局中使用
<LinearLayout 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"
android:orientation="vertical">
<com.example.view.myView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:mtext="按钮按钮按钮按钮按钮按钮按钮按钮"
app:mtextColor="@color/white"
app:mtextSize="10sp"
android:background="#ff0000"/>
</LinearLayout>
运行后的结果
好了这是简单实现的textview 下一章实现炫酷的view 收藏相关文章