Android设置图片圆角的方法
程序员文章站
2022-04-28 17:44:04
android中经常会遇到对图片进行二次处理,例如加圆角,或者显示圆形图片
实现的效果图:
方法一:
通过第三方框架glide实现图片显示有圆角,有三种写法如下:...
android中经常会遇到对图片进行二次处理,例如加圆角,或者显示圆形图片
实现的效果图:
方法一:
通过第三方框架glide实现图片显示有圆角,有三种写法如下:
1.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);
1.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);
1.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(); } /** * 绘制圆角矩形图片 * @author caizhiming */ @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; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读