对现有控件进行扩展
1、绘制如下所示的两层背景的TextView
创建BorderTextView继承TextView
在构造函数中初始化一些基本数据
//外边框
mPaint1 = new Paint();
mPaint1.setColor(getResources().getColor(android.R.color.holo_blue_bright));
//画笔的样式,充满
mPaint1.setStyle(Paint.Style.FILL);
//内边框
mPaint2 = new Paint();
mPaint2.setColor(Color.YELLOW);
mPaint2.setStyle(Paint.Style.FILL);
重写onDraw()方法,在此方法中主要通过canvas对象,来进行绘画。
@Override
protected void onDraw(Canvas canvas) {
//绘制外层矩形
canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),mPaint1);
//绘制内层矩形
canvas.drawRect(10,10,getMeasuredWidth()-10,getMeasuredHeight()-10,mPaint2);
canvas.save();//保存之前的状态
//水平平移10px
canvas.translate(10,10);
//回调父类方法之前,实现自己的逻辑,则