欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中图片圆角三种实现方法

程序员文章站 2021-12-03 08:59:16
目录方法一方法二方法三android 开发中,经常需要对图片进行二次处理,比如添加圆角效果 或 显示圆形图片;方法一通过第三方框架 glide 设置圆角效果;写法1:requestoptions op...

android 开发中,经常需要对图片进行二次处理,比如添加圆角效果 或 显示圆形图片;

方法一

通过第三方框架 glide 设置圆角效果;

写法1:

requestoptions options = new requestoptions().error(r.drawable.img_load_failure).bitmaptransform(new roundedcorners(30));//图片圆角为30
glide.with(this).load(url) //图片地址
                .apply(options)
                .into(imagview);

写法2:

requestoptions requestoptions = new requestoptions();
requestoptions.placeholder(r.drawable.ic_launcher_background);
requestoptions.circlecroptransform();
requestoptions.transforms( new roundedcorners(30));
glide.with(this).load(url) //图片地址
                .apply(options)
                .into(imagview);

写法3:

requestoptions options = new requestoptions().centercrop() .transform(new roundtransform(this,30)); 
glide.with(this).load(url) //图片地址
                .apply(options)
                .into(imagview);
public class roundtransform extends bitmaptransformation { 
    private static float radius = 0f; 
    public roundtransform(context context) { 
        this(context, 4); 
    } 
   
    public roundtransform(context context, int dp) { 
        super(context); 
        this.radius = resources.getsystem().getdisplaymetrics().density * dp; 
    } 
   
    @override 
    protected bitmap transform(bitmappool pool, bitmap totransform, int outwidth, int outheight) { 
        bitmap bitmap = transformationutils.centercrop(pool, totransform, outwidth, outheight); 
        return roundcrop(pool, bitmap); 
    } 
   
    private static bitmap roundcrop(bitmappool pool, bitmap source) { 
        if (source == null) return null; 
        bitmap result = pool.get(source.getwidth(), source.getheight(), bitmap.config.argb_8888); 
        if (result == null) { 
            result = bitmap.createbitmap(source.getwidth(), source.getheight(), bitmap.config.argb_8888); 
        } 
   
        canvas canvas = new canvas(result); 
        paint paint = new paint(); 
        paint.setshader(new bitmapshader(source, bitmapshader.tilemode.clamp, bitmapshader.tilemode.clamp)); 
        paint.setantialias(true); 
        rectf rectf = new rectf(0f, 0f, source.getwidth(), source.getheight()); 
        canvas.drawroundrect(rectf, radius, radius, paint); 
        return result; 
    } 
   
    public string getid() { 
        return getclass().getname() + math.round(radius); 
    } 
   
    @override 
    public void updatediskcachekey(messagedigest messagedigest) { 
   
    }
}

方法二

自定义imageview 设置圆角效果;

<imageview
        android:id="@+id/iv"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerhorizontal="true"
        />
imageview iv = findviewbyid(r.id.iv); 
bitmap bitmap =bitmapfactory.decoderesource(getresources(), r.drawable.fengjing);
        bitmap outbitmap =getroundbitmapbyshader(bitmap, 500,300,20, 3);
        iv.setimagebitmap(outbitmap);
public class roundrectimageview extends imageview{
 
    private paint paint;
 
    public roundrectimageview(context context) {
        this(context,null);
    }
 
    public roundrectimageview(context context, attributeset attrs) {
        this(context, attrs,0);
    }
 
    public roundrectimageview(context context, attributeset attrs, int defstyle) {
        super(context, attrs, defstyle);
        paint  = new paint();
    }
 
    /**
     * 绘制圆角矩形图片
     */
    @override
    protected void ondraw(canvas canvas) {
        drawable drawable = getdrawable();
        if (null != drawable) {
            bitmap bitmap = getbitmapfromdrawable(drawable);
//            bitmap bitmap = ((bitmapdrawable) drawable).getbitmap();
            bitmap b = getroundbitmapbyshader(bitmap,getwidth(),getheight(), 50,0);
            final rect rectsrc = new rect(0, 0, b.getwidth(), b.getheight());
            final rect rectdest = new rect(0,0,getwidth(),getheight());
            paint.reset();
            canvas.drawbitmap(b, rectsrc, rectdest, paint);
 
        } else {
            super.ondraw(canvas);
        }
    }
 
    /**
     * 把资源图片转换成bitmap
     * @param drawable
     * 资源图片
     * @return 位图
     */
    public static bitmap getbitmapfromdrawable(drawable drawable) {
        int width = drawable.getintrinsicwidth();
        int height = drawable.getintrinsicheight();
        bitmap bitmap = bitmap.createbitmap(width, height, drawable
                .getopacity() != pixelformat.opaque ? bitmap.config.argb_8888
                : bitmap.config.rgb_565);
        canvas canvas = new canvas(bitmap);
        //drawable.setbounds(-4, -4, width + 4, height + 4);
        drawable.draw(canvas);
        return bitmap;
    }
 
    public static bitmap getroundbitmapbyshader(bitmap bitmap, int outwidth, int outheight, int radius, int boarder) {
        if (bitmap == null) {
            return null;
        }
        int width = bitmap.getwidth();
        int height = bitmap.getheight();
        float widthscale = outwidth * 1f / width;
        float heightscale = outheight * 1f / height;
 
        matrix matrix = new matrix();
        matrix.setscale(widthscale, heightscale);
        //创建输出的bitmap
        bitmap desbitmap = bitmap.createbitmap(outwidth, outheight, bitmap.config.argb_8888);
        //创建canvas并传入desbitmap,这样绘制的内容都会在desbitmap上
        canvas canvas = new canvas(desbitmap);
        paint paint = new paint(paint.anti_alias_flag);
        //创建着色器
        bitmapshader bitmapshader = new bitmapshader(bitmap, shader.tilemode.clamp, shader.tilemode.clamp);
        //给着色器配置matrix
        bitmapshader.setlocalmatrix(matrix);
        paint.setshader(bitmapshader);
        //创建矩形区域并且预留出border
        rectf rect = new rectf(boarder, boarder, outwidth - boarder, outheight - boarder);
        //把传入的bitmap绘制到圆角矩形区域内
        canvas.drawroundrect(rect, radius, radius, paint);
 
        if (boarder > 0) {
            //绘制boarder
            paint boarderpaint = new paint(paint.anti_alias_flag);
            boarderpaint.setcolor(color.green);
            boarderpaint.setstyle(paint.style.stroke);
            boarderpaint.setstrokewidth(boarder);
            canvas.drawroundrect(rect, radius, radius, boarderpaint);
        }
        return desbitmap;
    }
 
}

方法三

对图片进行处理,还可以加边框;

/**
 * 通过bitmapshader实现圆形边框
 * @param bitmap 
 * @param outwidth 输出的图片宽度
 * @param outheight 输出的图片高度
 * @param radius 圆角大小
 * @param boarder 边框宽度
 */
public static bitmap getroundbitmapbyshader(bitmap bitmap, int outwidth, int outheight, int radius, int boarder) {
    if (bitmap == null) {
        return null;
    }
    int height = bitmap.getheight();
    int width = bitmap.getwidth();
    
    float widthscale = outwidth * 1f / width;
    float heightscale = outheight * 1f / height;
 
    matrix matrix = new matrix();
    matrix.setscale(widthscale, heightscale);
    //创建输出的bitmap
    bitmap desbitmap = bitmap.createbitmap(outwidth, outheight, bitmap.config.argb_8888);
    //创建canvas并传入desbitmap,这样绘制的内容都会在desbitmap上
    canvas canvas = new canvas(desbitmap);
    paint paint = new paint(paint.anti_alias_flag);
    //创建着色器
    bitmapshader bitmapshader = new bitmapshader(bitmap, shader.tilemode.clamp, shader.tilemode.clamp);
    //给着色器配置matrix
    bitmapshader.setlocalmatrix(matrix);
    paint.setshader(bitmapshader);
    //创建矩形区域并且预留出border
    rectf rect = new rectf(boarder, boarder, outwidth - boarder, outheight - boarder);
    //把传入的bitmap绘制到圆角矩形区域内
    canvas.drawroundrect(rect, radius, radius, paint);
 
    if (boarder > 0) {
        //绘制boarder
        paint boarderpaint = new paint(paint.anti_alias_flag);
        boarderpaint.setcolor(color.green);
        boarderpaint.setstyle(paint.style.stroke);
        boarderpaint.setstrokewidth(boarder);
        canvas.drawroundrect(rect, radius, radius, boarderpaint);
    }
    return desbitmap;
}

实现圆形和边框:

/**
 * 通过bitmapshader实现圆形边框
 * @param bitmap 
 * @param outwidth 输出的图片宽度
 * @param outheight 输出的图片高度
 * @param boarder 边框大小
 */
public static bitmap getcirclebitmapbyshader(bitmap bitmap, int outwidth, int outheight, int boarder) {
int radius;
int width = bitmap.getwidth();
int height = bitmap.getheight();
float widthscale = outwidth * 1f / width;
float heightscale = outheight * 1f / height;
 
bitmap desbitmap = bitmap.createbitmap(outwidth, outheight, bitmap.config.argb_8888);
if (outheight > outwidth) {
    radius = outwidth / 2;
} else {
    radius = outheight / 2;
}
//创建canvas
canvas canvas = new canvas(desbitmap);
paint paint = new paint(paint.anti_alias_flag);
bitmapshader bitmapshader = new bitmapshader(bitmap, shader.tilemode.clamp, shader.tilemode.clamp);
matrix matrix = new matrix();
matrix.setscale(widthscale, heightscale);
bitmapshader.setlocalmatrix(matrix);
paint.setshader(bitmapshader);
canvas.drawcircle(outwidth / 2, outheight / 2, radius - boarder, paint);
if (boarder > 0) {
    //绘制boarder
    paint boarderpaint = new paint(paint.anti_alias_flag);
    boarderpaint.setcolor(color.green);
    boarderpaint.setstyle(paint.style.stroke);
    boarderpaint.setstrokewidth(boarder);
    canvas.drawcircle(outwidth / 2, outheight / 2, radius - boarder, boarderpaint);
}
return desbitmap;
}

到此这篇关于android中图片圆角三种实现方法的文章就介绍到这了,更多相关android图片圆角内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!