rmad
rmad

Reputation: 31

TouchImageView zoom scaled to frame with Picasso

I was using Universal Image Loader library to load a set of images and TouchImageView to allow zooming. I decided to replace Universal Image Loader with picasso. Everything worked fine except now the image zooms around a frame which is slightly bigger than the image.

@Override
    public Object instantiateItem(ViewGroup view, int position) {
        View imageLayout = inflater.inflate(R.layout.item_pager_image, view, false);
        assert imageLayout != null;
        TouchImageView imageView = (TouchImageView) imageLayout.findViewById(R.id.image);
        final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading);
        spinner.setVisibility(View.INVISIBLE);
        Picasso.with(getApplicationContext()).setIndicatorsEnabled(false);
        Picasso.with(getApplicationContext()).load(images[position]).into(imageView,new Callback() {

            @Override
            public void onSuccess() {
                spinner.setVisibility(View.GONE);        
            }

            @Override
            public void onError() {

            }
        });
        view.addView(imageLayout, 0);
        return imageLayout;

I have been breaking my head over a few hours for this. Is this some issue TouchImageView has with Picasso? Any help would be appreciable. Thanks.

Upvotes: 3

Views: 4644

Answers (4)

Gavin
Gavin

Reputation: 97

For those who still run into this problem.

As inspired by a comment in this issue:

It's because needs View size and it's not available in TouchImageView implementation before bitmap is set

Load the image after the TouchImageView is created using .post().

Kotlin code:
touchImageView.post { // Load the image when the view is ready
    Picasso.get()
        .load(file)
        .placeholder(R.drawable.image_placeholder)
        .into(touchImageView)
}
Java code:
// Load the image when the view is ready
touchImageView.post(new Runnable() {
    @Override
    public void run() {
    Picasso.get()
        .load(file)
        .placeholder(R.drawable.image_placeholder)
        .into(touchImageView)
    }
});

Upvotes: 0

Faruk
Faruk

Reputation: 5821

Here is if you are using Glide. Glide is faster in loading than picasso and cheaper in memory consuming

Glide.with(context).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {
    @Override
    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
        someTouchImageView.setImageBitmap(resource);
    }
});

Upvotes: 0

Gueorgui Obregon
Gueorgui Obregon

Reputation: 5087

Mahram Foadi posted here a great solution that work for me too:

Picasso.with(context).load (someUri).into(new Target () {
   @Override
    public void onBitmapLoaded (final Bitmap bitmap,
        final Picasso.LoadedFrom loadedFrom) {
        someTouchImageView.setImageBitmap (bitmap);
    }

   @Override
    public void onBitmapFailed (final Drawable drawable) {
       Log.d(TAG, "Failed");
    }

   @Override
   public void onPrepareLoad (final Drawable drawable) {
        someTouchImageView.setImageDrawable (drawable);
   }
});

Hope this helps other people like us to use TouchImageView with Picasso ;)

Upvotes: 5

rmad
rmad

Reputation: 31

I figured out the whole issue somehow got fixed when I set the image width and height from wrap_content to fill_parent.

Upvotes: 0

Related Questions