android 圆角 ImageView

时间:2023-03-09 02:53:34
android 圆角 ImageView

android中Imageview 内的图片圆角的实现方式

此文针对的是 imageview中图片的圆角, 即忽略掉图片的ScaleType, 均对图片采取圆角.  而不是对Imageview本身的圆角.

处理方式分两大类(暂不讨论具体的实现方式)

  1. 传入图片前将图片圆角化.
  2. 传入图片之后在绘制过程中进行圆角化处理.

两种实现方式各有优劣.

  第一种实现方式在于每次调用之前需要对原图进行圆角化处理, 图片处理一般是比较耗时的操作(大图片比较明显), 而且处理完成后原图就本身带圆角, 不利于获取原来的图片, 此方法同时也是优势.....(无视那些保存原图的小伙伴.....)

  第二种方式. 优势在于不需要对图片额外处理, 只需要执行正常的调用逻辑,  是在图片绘制中进行额外处理, 所以时间耗费比方法一少. 不过此种方法处理过的图形本身不带圆角, 只是显示中才有圆角. 而且此种方法实现过程中难点在于获取图片在imageview中的显示位置.

方法二:

  步骤一, 对图片的绘制过程进行控制, 需要计算出 imageview 中的图片的显示位置, 然后根据位置进行圆角绘制.

  根据imageview中图片的scaletype 不一样, 动态的计算图像在imageview中的位置.  核心代码如下

  

     private RectF genBounds() {
RectF ret = new RectF(0, 0, 0, 0); if (getDrawable() == null) {
return ret;
} int dwidth = getDrawable().getIntrinsicWidth();
int dheight = getDrawable().getIntrinsicHeight(); int vwidth = getWidth() - getPaddingLeft() - getPaddingRight();
int vheight = getHeight() - getPaddingTop() - getPaddingBottom(); boolean fits = (dwidth < 0 || vwidth == dwidth) &&
(dheight < 0 || vheight == dheight); if (dwidth <= 0 || dheight <= 0 || ScaleType.FIT_XY == getScaleType()) {
ret.right = vwidth;
ret.bottom = vheight;
} else {
ret = new RectF(0, 0, dwidth, dheight); if (ScaleType.MATRIX == getScaleType()) {
// Use the specified matrix as-is.
} else if (fits) {
} else if (ScaleType.CENTER == getScaleType()) {
ret.offset((int) ((vwidth - dwidth) * 0.5f + 0.5f),
(int) ((vheight - dheight) * 0.5f + 0.5f));
} else if (ScaleType.CENTER_CROP == getScaleType()) { float scale;
float dx = 0, dy = 0; if (dwidth * vheight > vwidth * dheight) {
scale = (float) vheight / (float) dheight;
dx = (vwidth - dwidth * scale) * 0.5f;
} else {
scale = (float) vwidth / (float) dwidth;
dy = (vheight - dheight * scale) * 0.5f;
} ret.offset((int) (dx + 0.5f), (int) (dy + 0.5f));
} else if (ScaleType.CENTER_INSIDE == getScaleType()) {
float scale;
int dx;
int dy; if (dwidth <= vwidth && dheight <= vheight) {
scale = 1.0f;
} else {
scale = Math.min((float) vwidth / (float) dwidth,
(float) vheight / (float) dheight);
} dx = (int) ((vwidth - dwidth * scale) * 0.5f + 0.5f);
dy = (int) ((vheight - dheight * scale) * 0.5f + 0.5f); ret.offset(dx, dy);
}
} if (ret.left < getPaddingLeft())
ret.left = getPaddingLeft(); if (ret.right > vwidth + getPaddingLeft())
ret.right = vwidth + getPaddingLeft(); if (ret.top < getPaddingTop()) ret.top = getPaddingTop(); if (ret.bottom > vheight + getPaddingTop())
ret.bottom = vheight + getPaddingTop(); return ret;
}

  步骤二: 对图片的绘制进行处理.

  

     private int cornerColor = Color.WHITE;
private boolean usrRoundCorner = true;
private Path mMaskPath = new Path(); private void initPaint() {
if (mPaint == null) {
mPaint = new Paint();
mPaint.setColor(getCornerColor());
mPaint.setStrokeWidth(3);
mPaint.setAntiAlias(true);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
}
} Paint mPaint;
float mCornerRadius = getResources().getDisplayMetrics().density * 3 + 0.5f;
float[] radii = new float[]{mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius, mCornerRadius}; @Override
protected void onDraw(Canvas canvas) {
int saveCount = canvas.saveLayerAlpha(0F, 0F, canvas.getWidth(), canvas.getHeight(), 255, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
super.onDraw(canvas); if (usrRoundCorner) {
RectF bounds = genBounds(); mMaskPath.reset();
mMaskPath.addRoundRect(bounds, radii, Path.Direction.CW);
mMaskPath.setFillType(Path.FillType.INVERSE_WINDING); canvas.drawPath(mMaskPath, mPaint);
}
canvas.restoreToCount(saveCount);
}

核心代码都在上面了.  大家有其他方法也可以留言告诉我.