美文网首页
实现圆角图片

实现圆角图片

作者: 北漂攻城狮的泪 | 来源:发表于2017-02-21 13:54 被阅读0次

//设置圆角图片或者圆形图片

privateBitmapSettingBitmap(Bitmap bitmap) {

introundx =0;

intrect_left,rect_right,rect_top,rect_bottom;

intleft,right,top,bottom;

intwidth = bitmap.getWidth();

intheight = bitmap.getHeight();

if(width <= height) {

roundx = width /2;

left =0;

top =0;

right = width;

bottom = width;

intclip = (height - width) /2;

rect_left =0;

rect_right = width;

rect_top = clip;

rect_bottom = height - clip;

}else{

roundx = height /2;

left =0;

top =0;

right = height;

bottom = height;

intclip = (width - height) /2;

rect_left = clip;

rect_right = width - clip;

rect_top =0;

rect_bottom = height;

}

Bitmap bitmap1 = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);

Canvas canvas =newCanvas(bitmap1);

Paint paint =newPaint();

paint.setColor(Color.RED);

paint.setAntiAlias(true);//去锯齿

canvas.drawARGB(0,0,0,0);

Rect rect =newRect(rect_left,rect_top,rect_right,rect_bottom);

Rect rect_dst =newRect(left,top,right,bottom);

RectF rectF =newRectF(rect_dst);

//        canvas.drawCircle(roundx,roundx,roundx,paint);

/**

* rect:RectF对象。

rx:x方向上的圆角半径。

ry:y方向上的圆角半径。

paint:绘制时所使用的画笔。

*/

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

paint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.SRC_IN));// 设置两张图片相交时的模式

/**

* Rect src: 是对图片进行裁截,若是空null则显示整个图片

RectF dst:是图片在Canvas画布中显示的区域,

大于src则把src的裁截区放大,

小于src则把src的裁截区缩小。

*/

canvas.drawBitmap(bitmap,rect,rect_dst,paint);

if(bitmap != bitmap1) {

// Same bitmap is returned if sizes are the same

bitmap.recycle();

}

returnbitmap1;

}

相关文章

网友评论

      本文标题:实现圆角图片

      本文链接:https://www.haomeiwen.com/subject/recuwttx.html