How to round an image with Glide library?

前端 未结 22 1678
悲哀的现实
悲哀的现实 2020-11-28 00:46

So, anybody know how to display an image with rounded corners with Glide? I am loading an image with Glide, but I don\'t know how to pass rounded params to this library.

相关标签:
22条回答
  • 2020-11-28 00:53

    You can simply call the RoundedCornersTransformation constructor, which has cornerType enum input. Like this:

    Glide.with(context)
                .load(bizList.get(position).getCover())
                .bitmapTransform(new RoundedCornersTransformation(context,20,0, RoundedCornersTransformation.CornerType.TOP))
                .into(holder.bizCellCoverImg);
    

    but first you have to add Glide Transformations to your project.

    0 讨论(0)
  • 2020-11-28 00:54

    its very simple i have seen Glide library its very good library and essay base on volley Google's library

    usethis library for rounded image view

    https://github.com/hdodenhof/CircleImageView

    now

    //For a simple view:

     @Override
     public void onCreate(Bundle savedInstanceState) {
      ...
    
      CircleImageView civProfilePic = (CircleImageView)findViewById(R.id.ivProfile);
      Glide.load("http://goo.gl/h8qOq7").into(civProfilePic);
    }
    

    //For a list:

    @Override
    public View getView(int position, View recycled, ViewGroup container) {
    final ImageView myImageView;
     if (recycled == null) {
        myImageView = (CircleImageView) inflater.inflate(R.layout.my_image_view,
                container, false);
    } else {
        myImageView = (CircleImageView) recycled;
    }
    
    String url = myUrls.get(position);
    
    Glide.load(url)
        .centerCrop()
        .placeholder(R.drawable.loading_spinner)
        .animate(R.anim.fade_in)
        .into(myImageView);
    
      return myImageView;
    }
    

    and in XML

    <de.hdodenhof.circleimageview.CircleImageView
       android:id="@+id/ivProfile
       android:layout_width="160dp"
       android:layout_height="160dp"
       android:layout_centerInParent="true"
       android:src="@drawable/hugh"
       app:border_width="2dp"
       app:border_color="@color/dark" />
    
    0 讨论(0)
  • 2020-11-28 00:54

    Roman Samoylenko's answer was correct except the function has changed. The correct answer is

    Glide.with(context)
                    .load(yourImage)
                    .apply(RequestOptions.circleCropTransform())
                    .into(imageView);
    
    0 讨论(0)
  • 2020-11-28 00:56

    Now in Glide V4 you can directly use CircleCrop()

    Glide.with(fragment)
      .load(url)
      .circleCrop()
      .into(imageView);
    

    Built in types

    • CenterCrop
    • FitCenter
    • CircleCrop
    0 讨论(0)
  • 2020-11-28 00:57

    Circle crop + placeholder + crossfade

     Glide.with(context!!)
                        .load(randomImage)
                        .apply(RequestOptions.bitmapTransform(CircleCrop()).error(R.drawable.nyancat_animated))
                        .transition(DrawableTransitionOptions()
                                .crossFade())
                        .into(picture)
    

    0 讨论(0)
  • 2020-11-28 00:58

    The easiest way (requires Glide 4.x.x)

    Glide.with(context).load(uri).apply(RequestOptions().circleCrop()).into(imageView)
    
    0 讨论(0)
提交回复
热议问题