android 圆角图片的实现

时间:2022-11-17 22:01:50

图片展示的时候总觉的直角的图片不好看?好办法来了!~~

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 是要传入的图片,后一个数字越大图片圆角越明显。

android 圆角图片的实现

方法摘自:脚本之家(http://www.jb51.net/article/32320.htm)