实现圆形图片方式

第一种方式

//圆角图  
RoundedBitmapDrawable roundedDrawable = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.zhangphil));  
roundedDrawable.getPaint().setAntiAlias(true);  
roundedDrawable.setCornerRadius(30);  
ImageView image2 = (ImageView) findViewById(R.id.imageView2);  image2.setImageDrawable(roundedDrawable); 
//圆形图  
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.zhangphil);  
RoundedBitmapDrawable circleDrawable = RoundedBitmapDrawableFactory.create(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.zhangphil));  
circleDrawable.getPaint().setAntiAlias(true);  
circleDrawable.setCornerRadius(Math.max(bitmap.getWidth(), bitmap.getHeight()));  
ImageView image3 = (ImageView) findViewById(R.id.imageView3);  
        image3.setImageDrawable(circleDrawable); 

第二种方式(5.0以上)

/**
* 裁剪圆形头像
* @param view 要裁剪头像的控件
*/
private void cilpAvatar(View view) {
            ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                //圆形阴影
                outline.setOval(0, 0, view.getWidth(), view.getHeight());
            }
        };
        //设置阴影
        view.setOutlineProvider(viewOutlineProvider);
        //裁剪控件
   view.setClipToOutline(true);
}

第三种方式

/**
* 加载圆形图片
* @param context
* @param url
* @param iv
*/
public static void setCircular(final Context context, String url, final ImageView iv) {
        Glide.with(context).load(url)
                .asBitmap()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .thumbnail(0.1f)
                .centerCrop()
                .into(new BitmapImageViewTarget(iv) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable roundedBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                        roundedBitmapDrawable.setCircular(true);
                        iv.setImageDrawable(roundedBitmapDrawable);
                    }
      });
}

第四种方式

也是就各种GitHub上的库实现

你可能感兴趣的:(实现圆形图片方式)