Search code examples
androidimageviewandroid-viewpagersubsampling

Android ViewPager using SubsamplingScaleImageView results in blank screen for sometime when sliding images


I have made a ViewPager using davemorrissey's SubsamplingScaleImageView

When sliding the ViewPager, the next slide appears to be blank for some seconds before the image loads.

Has anyone faced same type of issue ? Any pointers for possible fixes ?

viewPager = (ViewPager) findViewById(R.id.pager);
magePagerAdapter adapter = new ImagePagerAdapter();       
viewPager.setAdapter(adapter);

ArrayList<String> imageFull = new ArrayList<String>();
for(int i=0;i<10;i++){
String image = "mnt/sdcard/imageDemo"+i+".jpg";
imageFull.add(image);
}


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
      Context context = ImageGallery.this;

      SubsamplingScaleImageView fullImage = new SubsamplingScaleImageView(ImageGallery.this);
      fullImage.setImage(ImageSource.uri(imageFull.get(position)));         

      return fullImage;
   }


   @Override
   public void destroyItem(ViewGroup container, int position, Object object) {
      ((ViewPager) container).removeView((SubsamplingScaleImageView) object);

   }

Solution

  • I was able to workaround it with this patch. I also wrote a similar answer in the visibility problem issue

    initialiseBaseLayer is called the first onDraw of the view, that's why it doesn't load until the page is partially visible.

    The issue is that initializing outside onDraw you lose access to the canvas, which is used to calculate the max bitmap dimensions:

    private Point getMaxBitmapDimensions(Canvas canvas) {
        if (VERSION.SDK_INT >= 14) {
            try {
                int maxWidth = (Integer)Canvas.class.getMethod("getMaximumBitmapWidth").invoke(canvas);
                int maxHeight = (Integer)Canvas.class.getMethod("getMaximumBitmapHeight").invoke(canvas);
                return new Point(maxWidth, maxHeight);
            } catch (Exception e) {
                // Return default
            }
        }
        return new Point(2048, 2048);
    }
    

    So, you will have to provide the max dimensions on your own. I'm using this function. I store the value during the onCreate of the activity and later the ViewPager get that value.

    Before setting the image call fullImage.setMaxDimensions(x, y) and that's it.

    It works fine (in my case) for a ViewPager, but it doesn't on a RecyclerView, so use it with care.