圆角ImageView
大概就是这个样子的 四个直角改成圆角的 度数可以自定义
自定义ImageView
方法一:BitmapShader方式
首先简单了解下BitmapShader
BitmapShader是Shader的子类
Shader在三维软件中我们称之为着色器
通俗的理解,Shader的作用是给图像着色或者上色
BitmapShader允许我们载入一张图片来给图像着色
所以其实根据上面对于BitmapShader的描述
其实就可以对圆角ImageView有一定的思路了吧
画一个圆角矩形,然后把本来画上去的图像着色到圆角矩形上
这样就实现了圆角的ImageView
/*** @author lyudony* @date /7/29.* description:*/public class RoundImageView extends AppCompatImageView {//圆角大小,默认为10private int mBorderRadius = 10;private Paint mPaint;// 3x3 矩阵,主要用于缩小放大private Matrix mMatrix;//渲染图像,使用图像为绘制图形着色private BitmapShader mBitmapShader;public RoundImageView(Context context) {this(context, null);}public RoundImageView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);mMatrix = new Matrix();mPaint = new Paint();mPaint.setAntiAlias(true);}@Overrideprotected void onDraw(Canvas canvas) {if (getDrawable() == null){return;}Bitmap bitmap = drawableToBitamp(getDrawable());mBitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);float scale = 1.0f;if (!(bitmap.getWidth() == getWidth() && bitmap.getHeight() == getHeight())){// 如果图片的宽或者高与view的宽高不匹配,计算出需要缩放的比例;缩放后的图片的宽高,一定要大于我们view的宽高;所以我们这里取大值;scale = Math.max(getWidth() * 1.0f / bitmap.getWidth(),getHeight() * 1.0f / bitmap.getHeight());}// shader的变换矩阵,我们这里主要用于放大或者缩小mMatrix.setScale(scale, scale);// 设置变换矩阵mBitmapShader.setLocalMatrix(mMatrix);// 设置shadermPaint.setShader(mBitmapShader);canvas.drawRoundRect(new RectF(0,0,getWidth(),getHeight()),mBorderRadius, mBorderRadius,mPaint);}private Bitmap drawableToBitamp(Drawable drawable){if (drawable instanceof BitmapDrawable){BitmapDrawable bd = (BitmapDrawable) drawable;return bd.getBitmap();}// 当设置不为图片,为颜色时,获取的drawable宽高会有问题,所有当为颜色时候获取控件的宽高int w = drawable.getIntrinsicWidth() <= 0 ? getWidth() :drawable.getIntrinsicWidth();int h = drawable.getIntrinsicHeight() <= 0 ? getHeight() :drawable.getIntrinsicHeight();Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(bitmap);drawable.setBounds(0, 0, w, h);drawable.draw(canvas);return bitmap;}}
方法二:ClipPath方式
ClipPath是Canvas提供对画布裁剪的方法之一
除了ClipPath还有clipRect方法,画布裁剪后后面的Canvas操作
都会在对裁剪后的画布进行操作
所以,只要绘出一个圆角矩形的路径,然后用ClipPath裁剪、
那么得到的画布就是圆角矩形的,那么后面的绘制自然也就是圆角矩形的了!
public class RoundImageView extends AppCompatImageView {float width, height;public RoundImageView(Context context) {this(context, null);}public RoundImageView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);if (Build.VERSION.SDK_INT < 18) {setLayerType(View.LAYER_TYPE_SOFTWARE, null);}}@Overrideprotected void onLayout(boolean changed, int left, int top, int right, int bottom) {super.onLayout(changed, left, top, right, bottom);width = getWidth();height = getHeight();}@Overrideprotected void onDraw(Canvas canvas) {if (width > 12 && height > 12) {Path path = new Path();path.moveTo(12, 0);path.lineTo(width - 12, 0);path.quadTo(width, 0, width, 12);path.lineTo(width, height - 12);path.quadTo(width, height, width - 12, height);path.lineTo(12, height);path.quadTo(0, height, 0, height - 12);path.lineTo(0, 12);path.quadTo(0, 0, 12, 0);canvas.clipPath(path);}super.onDraw(canvas);}}
共勉
我要一步一步往上爬
在最高点乘着叶片往前飞
任风吹干流过的泪和汗
我要一步一步往上爬
等待阳光静静看着它的脸
小小的天有大大的梦想
我有属于我的天
任风吹干流过的泪和汗
总有一天我有属于我的天