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

时间:2021-05-20

本文实例为大家分享了TextView绘制背景的方法,供大家参考,具体内容如下

效果:

实现流程:

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); }

以上就是本文的全部内容,希望能给大家一个参考,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章