您的位置:首页 > 移动开发 > Android开发

android 圆角图片的实现

2015-12-25 20:59 573 查看
图片展示的时候总觉的直角的图片不好看?好办法来了!~~

public class ToRoundCorner extends Activity{

public Bitmap toRoundCorner(Bitmap bitmap, int pixels) {

Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.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(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}

}


把上面的代码放到工具包中需要的时候只要调用下就好了!

ImageView pic = (ImageView) navigationView.getHeaderView(0).findViewById(R.id.tou_pic);
TextView username = (TextView) navigationView.getHeaderView(0).findViewById(R.id.nav_username);
TextView phone = (TextView) navigationView.getHeaderView(0).findViewById(R.id.nav_phone);

//将图片转换成bitmap
Drawable drawable = getResources().getDrawable(R.mipmap.aboutus);
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();
//将图片转成圆角
ToRoundCorner toround = new ToRoundCorner();
pic.setImageBitmap(toround.toRoundCorner(bitmap , 50));


用法很简单是不是? 其中最后一行toround.toRoundCorner(bitmap , 50)中bitmap 是要传入的图片,后一个数字越大图片圆角越明显。



方法摘自:脚本之家(http://www.jb51.net/article/32320.htm)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: