如何制作带有圆角的 ImageView? [英] How to make an ImageView with rounded corners?

查看:38
本文介绍了如何制作带有圆角的 ImageView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Android 中,ImageView 默认是一个矩形.如何在 ImageView 中使其成为圆角矩形(将位图的所有 4 个角剪掉为圆角矩形)?

解决方案

这已经很晚了,但对于正在寻找此问题的其他人,您可以执行以下代码来手动圆角图像.

http://www.ruibm.com/?p=184>

这不是我的代码,但我已经使用过它并且效果很好.我将它用作 ImageHelper 类中的一个助手,并稍微扩展了它以传递给定图像所需的羽化量.

最终代码如下:

package com.company.app.utils;导入 android.graphics.Bitmap;导入 android.graphics.Canvas;导入 android.graphics.Paint;导入 android.graphics.PorterDuffXfermode;导入 android.graphics.Rect;导入 android.graphics.RectF;导入 android.graphics.Bitmap.Config;导入 android.graphics.PorterDuff.Mode;公共类 ImageHelper {公共静态位图 getRoundedCornerBitmap(位图位图,整数像素){位图输出 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);Canvas canvas = new Canvas(output);最终 int 颜色 = 0xff424242;最终油漆油漆 = 新油漆();final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());最终 RectF rectF = 新 RectF(rect);最终浮点 roundPx = 像素;Paint.setAntiAlias(true);canvas.drawARGB(0, 0, 0, 0);Paint.setColor(颜色);canvas.drawRoundRect(rectF, roundPx, roundPx, 油漆);Paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));canvas.drawBitmap(位图,矩形,矩形,油漆);返回输出;}}

In Android, an ImageView is a rectangle by default. How can I make it a rounded rectangle (clip off all 4 corners of my Bitmap to be rounded rectangles) in the ImageView?

解决方案

This is pretty late in response, but for anyone else that is looking for this, you can do the following code to manually round the corners of your images.

http://www.ruibm.com/?p=184

This isn't my code, but I've used it and it's works wonderfully. I used it as a helper within an ImageHelper class and extended it just a bit to pass in the amount of feathering I need for a given image.

Final code looks like this:

package com.company.app.utils;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;

public class ImageHelper {
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

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

        return output;
    }
}

这篇关于如何制作带有圆角的 ImageView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆