1.res/values/下新建attrs文件,声明自定义控件需用到的属性变量
<declare-styleable name="LabelView">
<attr name="text" format="string"></attr>
<attr name="textcolor" format="color"></attr>
<attr name="textsize" format="dimension"></attr>
</declare-styleable>
2.新建自定义类继承View类,实现构造方法。注意:必须实现第二个构造方法,否则会出现构造函数不被调用报空指针异常
public class Labview extends View {
private Paint mTextPaint;
private String mText;
private int mAscent;
private float size;
private Rect rect;
public Labview(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@SuppressLint("Recycle")
public Labview(Context context, AttributeSet attrs) {
super(context, attrs,0);
// TODO Auto-generated constructor stub
TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.LabelView);//获取声明的属性变量
mText =array.getString(R.styleable.LabelView_text);//获取文字内容
mAscent=array.getColor(R.styleable.LabelView_textcolor,android.R.color.white);//获取文字颜色
size=array.getDimensionPixelSize(R.styleable.LabelView_textsize,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));//获取文字大小
System.out.println("mText "+mText+" mAscent "+mAscent+" size "+size);
mTextPaint=new Paint();//新建绘图类
mTextPaint.setTextSize(size);//设置绘图类的文字大小
rect=new Rect();
mTextPaint.getTextBounds(mText, 0, mText.length(), rect);
}
@SuppressLint("Recycle")
public Labview(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
@SuppressLint("DrawAllocation") @Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
mTextPaint.setColor(Color.BLUE); //先画View的背景,这个地方若后面设置其他颜色会和前一个颜色混合
canvas.drawRect(0, 0,getMeasuredWidth() ,getMeasuredHeight(), mTextPaint); //在画布上绘制背景颜色的大小
mTextPaint.setColor(mAscent); //绘制文本颜色
canvas.drawText(mText, getMeasuredWidth()/2-rect.width()/4+getPaddingLeft()/6, getMeasuredHeight()/2+rect.height()/2, mTextPaint); //绘制文本的位置
this. setOnClickListener(new OnClickListener() {//设置监听
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mText=randomText();
postInvalidate();//点击后进行控件重绘
}
}) ;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//设置控件的宽高
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width;
int height ;
if (widthMode == MeasureSpec.EXACTLY)
{
width = widthSize;
} else
{
mTextPaint.setTextSize(size);
float textWidth = rect.width();
int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());
width = desired;
}
if (heightMode == MeasureSpec.EXACTLY)
{
height = heightSize;
} else
{
mTextPaint.setTextSize(size);
float textHeight = rect.height();
int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom());
height = desired;
}
setMeasuredDimension(width, height);
}
private String randomText()
{
Random random = new Random();
Set<Integer> set = new HashSet<Integer>();
while (set.size() < 4)
{
int randomInt = random.nextInt(10);
set.add(randomInt);
}
StringBuffer sb = new StringBuffer();
for (Integer i : set)
{
sb.append("" + i);
}
return sb.toString();
}
}