Android:ImageView圆角已实现,彩色边框绘制失败求助
解决ImageView圆角+彩色边框的问题
嘿,我知道你已经搞定了圆角效果,但边框一直不生效的问题确实挺烦人的!咱们先说说为什么你之前用setPadding和setBackgroundColor的方法没用:
你的圆角Bitmap本身是矩形载体,只是内容被裁剪成了圆角——也就是说Bitmap的四个角区域其实是透明的?不对,准确来说你创建的bitmapRounded是正方形的,里面只有圆角的内容,其余区域是透明的。但当你把它设置给ImageView后,再设置背景色和padding,ImageView的背景会显示在Bitmap下面,但因为Bitmap的透明区域是直角的,背景色只会从四个直角角落出来,不是你要的环绕圆角的边框;再加上如果ImageView的scaleType设置不当,Bitmap可能会撑满整个ImageView,把padding的空间完全吃掉,背景就更看不到了。
那怎么正确加彩色边框呢?给你两种靠谱的方法:
方法一:直接在Canvas里绘制边框(最直接可控)
在生成圆角Bitmap的时候,咱们先画一层彩色的圆角边框,再把内容画在中间。这样生成的Bitmap本身就带着边框,不管ImageView怎么设置都能正常显示:
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bm, 150, 150, false); int min = Math.min(resizedBitmap.getWidth(), resizedBitmap.getHeight()); int borderWidth = 4; // 这里设置你想要的边框宽度,可自行调整 // 创建一个比原内容大2倍边框宽度的Bitmap,用来装边框+内容 Bitmap bitmapWithBorder = Bitmap.createBitmap( min + borderWidth * 2, min + borderWidth * 2, resizedBitmap.getConfig() ); Canvas canvas = new Canvas(bitmapWithBorder); // 第一步:画彩色边框 Paint borderPaint = new Paint(); borderPaint.setAntiAlias(true); borderPaint.setColor(getResources().getColor(R.color.negro)); // 替换成你的目标边框颜色 // 画整个画布大小的圆角矩形作为边框,圆角半径和内容匹配 canvas.drawRoundRect( new RectF(0, 0, bitmapWithBorder.getWidth(), bitmapWithBorder.getHeight()), (min + borderWidth * 2) / 2f, (min + borderWidth * 2) / 2f, borderPaint ); // 第二步:画圆角内容,缩放到边框内部 Paint contentPaint = new Paint(); contentPaint.setAntiAlias(true); contentPaint.setShader(new BitmapShader(resizedBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)); // 画中间的圆角矩形,距离画布边缘留一个边框宽度的空隙 canvas.drawRoundRect( new RectF( borderWidth, borderWidth, bitmapWithBorder.getWidth() - borderWidth, bitmapWithBorder.getHeight() - borderWidth ), min / 2f, // 内容的圆角半径,和你原来的设置一致 min / 2f, contentPaint ); // 最后设置给ImageView avatar.setImageBitmap(bitmapWithBorder);
方法二:用LayerDrawable组合边框和圆角内容
这种方法不用修改Bitmap本身,而是通过Drawable层的组合来实现,适合需要动态修改边框颜色的场景:
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bm, 150, 150, false); int min = Math.min(resizedBitmap.getWidth(), resizedBitmap.getHeight()); int borderWidth = 4; // 1. 创建圆角内容的Drawable RoundedBitmapDrawable roundedContent = RoundedBitmapDrawableFactory.create(getResources(), resizedBitmap); roundedContent.setCornerRadius(min / 2f); roundedContent.setAntiAlias(true); // 2. 创建边框的ShapeDrawable ShapeDrawable borderDrawable = new ShapeDrawable( new RoundRectShape( new float[]{ min/2 + borderWidth, min/2 + borderWidth, min/2 + borderWidth, min/2 + borderWidth, min/2 + borderWidth, min/2 + borderWidth, min/2 + borderWidth, min/2 + borderWidth }, null, null ) ); borderDrawable.getPaint().setColor(getResources().getColor(R.color.negro)); borderDrawable.getPaint().setStrokeWidth(borderWidth); borderDrawable.getPaint().setStyle(Paint.Style.STROKE); // 设置为描边模式,只画边框 // 3. 组合成LayerDrawable,把边框放在底层,内容放在上层 LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{borderDrawable, roundedContent}); // 给上层内容设置内边距,留出边框的显示空间 layerDrawable.setLayerInset(1, borderWidth, borderWidth, borderWidth, borderWidth); // 设置给ImageView avatar.setImageDrawable(layerDrawable);
这两种方法都能完美实现你要的圆角+彩色边框效果,推荐第一种,因为生成的Bitmap自带边框,不会受ImageView的scaleType或者其他属性影响~
内容的提问来源于stack exchange,提问作者Mimmetico
相关产品推荐
相关产品推荐

