android: create circular image with picasso

后端 未结 10 821
天命终不由人
天命终不由人 2020-11-29 18:31

The question had been asked and there had been a promise made for the very version of Picasso that I am using: How do I send a circular bitmap to an ImageView using Picasso?

10条回答
  •  青春惊慌失措
    2020-11-29 18:43

    here is something that's provided by the support-v4 library! Look into RoundedBitmapDrawable. No need to roll your own:

    Picasso.with(context).load(url)
                            .resize(w, h)
                            .into(myImageView, new Callback() {
                                @Override
                                public void onSuccess() {
                                    Bitmap imageBitmap = ((BitmapDrawable) myImageView.getDrawable()).getBitmap();
                                    RoundedBitmapDrawable imageDrawable = RoundedBitmapDrawableFactory.create(getResources(), imageBitmap);
                                    imageDrawable.setCircular(true);
                                    imageDrawable.setCornerRadius(Math.max(imageBitmap.getWidth(), imageBitmap.getHeight()) / 2.0f);
                                    myImageView.setImageDrawable(imageDrawable);
                                }
                                @Override
                                public void onError() {
                                    myImageView.setImageResource(R.drawable.default_image);
                                }
                            });
    

    Note: Picasso also has a .transform(customTransformation) call that you could theoretically use, however, I had issues with that. This above works. Good luck!

提交回复
热议问题