100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Android裁剪图片为圆形图片

Android裁剪图片为圆形图片

时间:2020-06-30 15:46:36

相关推荐

Android裁剪图片为圆形图片

Android裁剪图片为圆形图片

/kkmike999/article/details/16359713

-11-16 19:15 3234人阅读 评论(8) 收藏 举报[java]view plain copy/** *转换图片成圆形 * *@parambitmap *传入Bitmap对象 *@return */ publicBitmaptoRoundBitmap(Bitmapbitmap){ intwidth=bitmap.getWidth(); intheight=bitmap.getHeight(); floatroundPx; floatleft,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom; if(width<=height){ roundPx=width/2; left=0; top=0; right=width; bottom=width; height=width; dst_left=0; dst_top=0; dst_right=width; dst_bottom=width; }else{ roundPx=height/2; floatclip=(width-height)/2; left=clip; right=width-clip; top=0; bottom=height; width=height; dst_left=0; dst_top=0; dst_right=height; dst_bottom=height; } Bitmapoutput=Bitmap.createBitmap(width,height,Config.ARGB_8888); Canvascanvas=newCanvas(output); finalintcolor=0xff424242; finalPaintpaint=newPaint(); finalRectsrc=newRect((int)left,(int)top,(int)right,(int)bottom); finalRectdst=newRect((int)dst_left,(int)dst_top,(int)dst_right,(int)dst_bottom); finalRectFrectF=newRectF(dst); paint.setAntiAlias(true);//设置画笔无锯齿 canvas.drawARGB(0,0,0,0);//填充整个Canvas paint.setColor(color); //以下有两种方法画圆,drawRounRect和drawCircle //canvas.drawRoundRect(rectF,roundPx,roundPx,paint);//画圆角矩形,第一个参数为图形显示区域,第二个参数和第三个参数分别是水平圆角半径和垂直圆角半径。 canvas.drawCircle(roundPx,roundPx,roundPx,paint); paint.setXfermode(newPorterDuffXfermode(Mode.SRC_IN));//设置两张图片相交时的模式,参考/blog/1189452 canvas.drawBitmap(bitmap,src,dst,paint);//以Mode.SRC_IN模式合并bitmap和已经draw了的Circle returnoutput; }

参考流程图 & 原理图:

Demo下载:

/detail/kkmike999/6562925

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。