Renaud Favier
Renaud Favier

Reputation: 1655

set 9 patch as imageView src with Picasso

I have an ImageView where I load an image from network (center cropped)

    <ImageView
        android:id="@+id/content_image_view"
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:scaleType="centerCrop"
        android:src="@drawable/course" />

An object comes to my adapter with an int (the id of a local drawable) and sometimes an url.

if the url is null I load the local drawable, which can be a 9 patch. My 9patches are then displayed zoomed.

I tried different things but for now :

ApiController.getInstance().tokenedPicasso.with(holder.contentImageView.getContext())
                        .load(formatted.contentImageUrl)
                        .placeholder(formatted.contentPlaceHolderId)
                        .centerCrop()
                        .fit()
                        .config(Bitmap.Config.RGB_565)
                        .into(holder.contentImageView);

Have someone tried to do the same thing before ? Thanks,

Upvotes: 0

Views: 941

Answers (2)

deadfish
deadfish

Reputation: 12304

Okay, I solved it in another way and it works in this way.

  • If data was not loaded -> show placeholder .9.png
  • IF data was loaded -> show image

Before all thing we should create extended ImageView with Target.

Calling:

Picasso.with(getContext()).load(url).into((Target)imageViewTarget);

Custom ImageView:

public class ImageViewTarget extends ImageView implements Target {
    public ImageViewTarget(Context context) {
        super(context);
    }

    public ImageViewTarget(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ImageViewTarget(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void setPlaceHolder() {
        Drawable d = null;

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
            d = getResources().getDrawable(R.drawable.blank_photo);
        } else {
            d = getResources().getDrawable(R.drawable.blank_photo, getContext().getTheme());
        }

        if(d!=null) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                setBackgroundDrawable(d);
            } else {
                setBackground(d);
            }
        }
        setPadding(0, 0, 0, 0);
    }

    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        setImageDrawable(new BitmapDrawable(getResources(), bitmap));
        setScaleType(ScaleType.CENTER_CROP);
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        //...
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
        setPlaceHolder();
    }
}

blank_photo.9.png

Upvotes: 1

Renaud Favier
Renaud Favier

Reputation: 1655

I Have partialy answered my question,

I test first if the 9 patch will be displayed, and if it is, I change the scale type do FIT_XY.

But I'll keep searching for a more elegant solution. because telling if a 9 patch will be displayed can be complex sometime and I'd rather have something that at least work with all my drawables.

Upvotes: 2

Related Questions