Android自定义View之继承TextView绘制背景
程序员文章站
2024-03-01 16:35:28
本文实例为大家分享了textview绘制背景的方法,供大家参考,具体内容如下
效果:
实现流程:
1.初始化:对画笔进行设置
mpaintin =...
本文实例为大家分享了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); }
以上就是本文的全部内容,希望能给大家一个参考,也希望大家多多支持。