本文實例為大家分享了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); }以上就是本文的全部內容,希望能給大家一個參考,也希望大家多多支持武林網。
新聞熱點
疑難解答