简体   繁体   English

如何使ImageView中的图像角变圆?

[英]How to make the corner of an image inside ImageView round?

I am having this problem and I know it's common, the problem is that I have already applied the code to draw, here . 我遇到了这个问题,我知道这很常见,问题是我已经在此处应用了代码绘制。 Now, since my ImageView has set width and height which is 50dp , the rounded corner is not achieved. 现在,由于我的ImageView宽度和高度设置为50dp ,因此无法实现圆角。

So I used the xml alternative, here it is: 因此,我使用了xml替代方案,它是:

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke
        android:width="1dp"
        android:color="#999999" />
    <padding android:left="2dp"
             android:top="2dp"
             android:right="2dp"
             android:bottom="2dp"/>
    <solid android:color="#ffffff" />
    <corners android:radius="15dp" />
</shape>

I made this as background of the ImageView, the problem now is that the only affected of the round corner is the ImageView itself and not its content image. 我将其作为ImageView的背景,现在的问题是,圆角的唯一影响是ImageView本身而不是其内容图像。 The image's normal (rectangle or sharp) edge still overlapped its container which is the ImageView. 图像的法线(矩形或锐利)边缘仍与其容器ImageView重叠。 I also set this android:scaleType="centerCrop" in my ImageView . 我还在我的ImageView设置了这个android:scaleType="centerCrop"

I am now confused, so setting the ImageView's width and height forfeits the effect of the rounded corner? 我现在很困惑,因此设置ImageView的宽度和高度会失去圆角的效果吗? What should I do? 我该怎么办?

Here's a complete example: https://github.com/vinc3m1/RoundedImageView 这是一个完整的示例: https : //github.com/vinc3m1/RoundedImageView

Check it out. 看看这个。

try this... 尝试这个...

public static Bitmap getRoundCorneredBitmapFrom(Bitmap bitmap,int cornerRadius)  {
        if (bitmap == null) {
            return null;
        }
        if (cornerRadius < 0) {
            cornerRadius = 0;
        }
        // Create plain bitmap
        Bitmap canvasBitmap = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = getCanvas(canvasBitmap);

        Paint paint = getPaint();

        Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        RectF rectF = new RectF(rect);

        canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return canvasBitmap;
    }
  private static Canvas getCanvas(Bitmap bitmap) {
     Canvas canvas = new Canvas(bitmap);
     canvas.drawARGB(0, 0, 0, 0);
     return canvas;
  }
  private static Paint getPaint() {
     Paint paint = new Paint();
     paint.setAntiAlias(true);
     paint.setColor(Color.WHITE);
     return paint;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM