Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Picasso with RoundedBitmapDrawable

I've seen Udacity lectures about Material Design and there was mentioned usage of RoundedBitmapDrawable to achieve circular view. However I have some troubles to make it work with Picasso.

I'm not sure how exactly Picasso works, but I have large, nonsquare image in file storage. Therefor I use Picasso as follows:

Picasso.with(context).load(f).resize(densityDpi, densityDpi).centerInside().transform(new Transformation() {
    @Override
    public Bitmap transform(Bitmap source) {
        Log.d("jano", "transformation running");
        RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(), source);
        drawable.setCircular(true);
        drawable.setCornerRadius(source.getWidth() / 2.0f);
        return drawable.getBitmap();
    }

    @Override
    public String key() {
        return "circle";
    }
}).into(imageView);

Images are however squared without rounded corners (should be circular). And That is what I want to achieve.

Is there any simple way to achieve this with RoundedBitmapDrawable or do I have to fully implement transformation? (which I have seen on StackOverflow)

Please do not submit answer without description why it cannot be used. I only want to know about combination of these 2 items (Picasso, RoundedBitmapDrawable)

like image 966
VizGhar Avatar asked Apr 15 '26 13:04

VizGhar


1 Answers

I tried a lot to do the same but also not working... I guess is a problem during the getBitmap, anyway, I solved doing this: (Note that the main difference is that I'm using a setImage as drawable and not converting to Bitmap as I said)

    Picasso.with(getContext())
    .load(mUser.user.profileImageUrl)
    .into(mProfileImage, new Callback() {
        @Override
        public void onSuccess() {
            Bitmap source = ((BitmapDrawable) mProfileImage.getDrawable()).getBitmap();
            RoundedBitmapDrawable drawable =
                    RoundedBitmapDrawableFactory.create(getContext().getResources(), source);
            drawable.setCircular(true);
            drawable.setCornerRadius(Math.max(source.getWidth() / 2.0f, source.getHeight() / 2.0f));
            mProfileImage.setImageDrawable(drawable);
        }

        @Override
        public void onError() {

        }
    });
like image 65
Fernando Bonet Avatar answered Apr 18 '26 04:04

Fernando Bonet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!