参考鸿扬大神的博客博客地址
具体步骤与前面类似
- 1、自定义View的属性
- 2、在View的构造方法中获得我们自定义的属性
- 3、重写onMesure
- 4、重写onDraw
1.新建view类,这里不用布局文件,图像由view自己绘制
package com.xunku.basetest.customView;
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.util.AttributeSet;
import android.view.View;
import com.xunku.basetest.R;
/**
* Created 郑贤鑫 on 2017/2/10.
*/
public class CustomText extends View {
/**
* 文本
*/
private String mTitleText;
/**
* 文本的颜色
*/
private int mTitleTextColor;
/**
* 文本的大小
*/
private int mTitleTextSize;
/**
* 文本的背景颜色
*/
private int bgColor;
/**
* 绘制时控制文本绘制的范围
*/
private Rect mBound;
private Paint mPaint;
public CustomText(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray attributes=context.obtainStyledAttributes(attrs, R.styleable.CustomText);
if(attributes != null){
mTitleText=attributes.getString(R.styleable.CustomText_text);
mTitleTextColor=attributes.getColor(R.styleable.CustomText_textColor, Color.BLACK);
mTitleTextSize=attributes.getDimensionPixelSize(R.styleable.CustomText_textSize,16);
bgColor=attributes.getColor(R.styleable.CustomText_bgColor,Color.GREEN);
}
/**
* 获得绘制文本的宽和高
*/
mPaint = new Paint();
mPaint.setTextSize(mTitleTextSize);
mPaint.setColor(mTitleTextColor);
mBound = new Rect();
mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint.setColor(bgColor);
canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint);
mPaint.setColor(mTitleTextColor);
canvas.drawText(mTitleText,getWidth()/2 - mBound.width()/2,getHeight()/2 - mBound.width()/2,mPaint);
}
@Override
public void layout(int l, int t, int r, int b) {
super.layout(l, t, r, b);
}
}
2.style文件里面加入属性值
<declare-styleable name="CustomText">
<attr name="text" format="reference|string"/>
<attr name="textSize" format="reference|dimension"/>
<attr name="textColor" format="reference|color"/>
<attr name="bgColor" format="reference|color"/>
</declare-styleable>
3.使用
<com.xunku.basetest.customView.CustomText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:text="55555"
app:textSize="30dp"/>
关于onMeasure()函数
在ScrollView里面嵌套ListView的时候会重写这个方法
@Override
/**
* 重写该方法,达到使ListView适应ScrollView的效果
*/
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
鸿扬大神在博客里说到过
系统帮我们测量的高度和宽度都是MATCH_PARNET,当我们设置明确的宽度和高度时,系统帮我们测量的结果就是我们设置的结果,当我们设置为WRAP_CONTENT,或者MATCH_PARENT系统帮我们测量的结果就是MATCH_PARENT的长度。
所以,当设置了WRAP_CONTENT时,我们需要自己进行测量,即重写onMesure方法”:
重写之前先了解MeasureSpec的specMode,一共三种类型:
- [ ] EXACTLY:一般是设置了明确的值或者是MATCH_PARENT
- [ ] AT_MOST:表示子布局限制在一个最大值内,一般为WARP_CONTENT
- [ ] UNSPECIFIED:表示子布局想要多大就多大,很少使用
有关于这个函数的具体介绍,可以参考博客onMesure()详解