软件编程
位置:首页>> 软件编程>> Android编程>> Android自定义View之继承TextView绘制背景

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

作者:Brioal  发布时间:2021-11-05 11:16:06 

标签:Android,自定义,View,TextView,背景

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

效果:

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

实现流程:

Android自定义View之继承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);
 }

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com