您的位置:首页 > 移动开发 > Android开发

自定义View之继承TextView绘制背景

2016-05-21 12:26 537 查看

自定义View之继承TextView绘制背景

本文是详解Android群英传学习记录自定义View篇的使用原有控件扩展的简单Demo

效果:



实现流程



1.初始化:对画笔进行设置

mPaintIn = new Paint();
mPaintIn.setAntiAlias(true);
mPaintIn.setDither(true);
mPaintIn.setStyle(Paint.Style.FILL);       mPaintIn.setColor(getResources().getColor(R.color.colorPrimary));

mPaintOut = new Paint();
mPaintOut.setAntiAlias(true);
mPaintOut.setDither(true);
mPaintOut.setStyle(Paint.Style.FILL);     mPaintOut.setColor(getResources().getColor(R.color.colorAccent));


2.绘制外框,内框,文字

获取组件宽高

int width = getMeasureWidth();
int height = getMeasureHeight();


绘制

@Override
protected void onDraw(Canvas canvas) {
//绘制背景,在绘制文字之前绘制
canvas.drawRect(new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight()), mPaintIn);
canvas.drawRect(new Rect(10, 10, getMeasuredWidth()-10, getMeasuredHeight()-10), mPaintOut);

super.onDraw(canvas);
}


完毕

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android textview