How to delay Picasso image loading?

Ken Ratanachai S.

I have implemented a shared element activity transition animation between MainActivity (GridView) and DetailActivity. When item is clicked, the image in Grid item will zoom-in to become the background image in DetailActivity. I have made the transition very smooth by saving the image on the MainActivity into a file, then use it as placeholder image in DetailActivity before the higher quality image is downloaded via picasso. When picasso finished its task the higher quality image will replace the placeholder one very neatly.

Code from onCreateView() in DetailActivityFragment.java

ImageView iv = (ImageView) mRootview.findViewById(R.id.movie_poster);
try {
    // Prepare "InterimPoster" jpeg file to be placeholder image
    Bitmap bitmap = BitmapFactory.decodeStream(c.openFileInput("InterimPoster"));
    Picasso.with(c).load("http://image.tmdb.org/t/p/w780" + mMovieInfo[2])
            .placeholder(new BitmapDrawable(getResources(), bitmap))
            .fit()
            .centerCrop()
            .noFade()
            .into(iv);

} catch (FileNotFoundException e) {
    e.printStackTrace();
}

As for animation, I implement it like in the link here step 1-3 https://guides.codepath.com/android/Shared-Element-Activity-Transition

However, sometimes there's flicker when the higher quality image finished download before the transition animation is completed. The zooming-in image will be replaced with new image while moving which is an unpleasant animation.

So I wonder how can I fix this flicker? One thing I can think of is to delay the image download because I already have lower quality image as a placeholder. How can I do that?

Here the video. Usually, on my test device it's smooth 80% of the time, but luckily in this video it flicker most of the time.

Ken Ratanachai S.

Now I have found a way to delay Picasso image loading (or any other task). It's very simple really with the use of Handler and Runnable.

Now my code looks like this. The image replacement is pretty smooth in any case now.

    // Set Background Poster in Try-catch in case file cannot be open
    try {
        // Get/Prepare "InterimPoster" jpeg file to be placeholder image
        final ImageView iv = (ImageView) mRootview.findViewById(R.id.movie_poster);
        final Bitmap bitmap = BitmapFactory.decodeStream(c.openFileInput("InterimPoster"));
        final Drawable lowResPoster = new BitmapDrawable(getResources(), bitmap);
        iv.setImageDrawable(lowResPoster);

        // Download higher resolution image with 0.8 sec delay to avoid load complete before
        // animation finishes (causing some flicker/overflow image problem).
        Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                Picasso.with(c).load("http://image.tmdb.org/t/p/w780" + mMovieInfo[2])
                        // still need placeholder here otherwise will flash of white image
                        .placeholder(lowResPoster)
                        .error(lowResPoster)
                        .fit()
                        .centerCrop()
                        .noFade() // without this image replacement will not be smooth
                        .into(iv);
            }
        };
        handler.postDelayed(runnable, 800);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Picasso not loading the image

From Dev

Picasso not loading URL into image

From Dev

picasso android image not loading

From Dev

Implementing Picasso not loading image

From Dev

Image loading using picasso on disk

From Dev

Image Slider image not loading (ViewPager + Picasso)

From Dev

Fade in animation while loading image Using Picasso

From Dev

Loading image using Picasso showing colors in corners

From Dev

Picasso image loading previously cached images

From Dev

Picasso not loading updated image from Web in android

From Java

Picasso image loading issue with Android 9.0 Pie

From Dev

Picasso loading of image spawned inside AsyncTask

From Dev

Picasso's image loading using fit() method

From Dev

Android,Picasso. Is not loading an Image from a url

From Dev

Picasso loading image error-Android

From Dev

Picasso image loading issue in GoogleMap.InfoWindowAdapter

From Dev

Picasso only loading one image in a BaseAdapter

From Dev

Picasso image loading issue with Android 9.0 Pie

From Dev

Picasso Image Loading Library, some issues

From Dev

Android: Lazy Loading Image in horizontalscrollview using Picasso

From Dev

Picasso loading image error-Android

From Dev

Android - Picasso removes margin after loading Image

From Dev

Picasso only loading single image but the size is 20

From Dev

Different width when loading same image with Picasso

From Dev

Picasso not loading image from a URL, sometimes

From Dev

Show an "Loading..." image while background loading of image with Picasso

From Dev

Show an "Loading..." image while background loading of image with Picasso

From Dev

Delay loading image before appending content

From Dev

Picasso image loader: loading updated images from url

Related Related

HotTag

Archive