关于绘制圆角矩形的方式( api 21 以下)

Android 码拜 9年前 (2015-11-29) 1006次浏览
在api 21开始可以使用drawRoundRect实现圆角矩形, 
但是在底于这个版本的系统中怎么绘制呢?
本人想过一种使用一个半圆与矩形拼接的方式, 但在有的系统中, 两个图总不能完美的连接,  (原因是要与父容器背景透明叠加), 两个拼接处假如没有坐标偏移(连续绘制), 则会出现叠加,颜色变深, 估计是原因是有抗锯齿的处理导致的, 假如偏移1个px,则会出现一个断开的缝.
后面在网上找了一种变通的方法,  就是使用画线的方式. (只能实现线的两端是半圆).
接口 setStrokeCap   设置参数Cap.ROUND
设置好线宽, 就可以得到两端是半圆的矩形区域.
但这个方法也不是通用的.  在MIUI 的android4.1.2上,   Cap.ROUND这个参数似乎无效,  还是显示为矩形直线.
讨教大家, 有没有好的实现方式. 谢谢了, 
解决方案:10分
圆角矩形直接在资源文件定义不行吗,自定义view先画一个再画一个圆取交集
解决方案:40分
drawRoundRect这个方法低版本是支持的,你看看是不是api21添加了一个新的drawRoundRect的原因,
public void  drawRoundRect (RectF rect, float rx, float ry, Paint paint) 
Added in API level 1
Draw the specified round-rect using the specified paint. The roundrect will be filled or framed based on the Style in the paint.
Parameters
rect 
The rectangular bounds of the roundRect to be drawn 
rx 
The x-radius of the oval used to round the corners 
ry 
The y-radius of the oval used to round the corners 
paint 
The paint used to draw the roundRect  
解决方案:50分
在官网上说的
drawRoundRect( 0 , 0, width, height, mRadius, mRadius, mPaint) 这个方法已经是api21的
可以用这个代替
drawRoundRect(new RectF(0, 0, width, height), mRadius, mRadius, mPaint)

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于绘制圆角矩形的方式( api 21 以下)
喜欢 (0)
[1034331897@qq.com]
分享 (0)