Loading HD Extra Long Map - Using subsampling-scale-image-view

Keywords: Android

Reference learning website:
About Andorid loading HD large image (imitating the long image effect in sina weibo) - CSDN blog http://blog.csdn.net/t1623183652/article/details/50037351

Trying to use Glide is too laggy, PhotoView, ImageLoader and so on, but the general pictures can be loaded out, but the HD super long graph can not be loaded, the interface is black, LargeImageView is used to load the pictures, but very cards, = = *) alas, the operation is not ideal. Just when I was at a loss, I suddenly found subsampling scale image view (I heard it for the first time). I tried it immediately. It was great. There was no pressure to load long HD images. I don't want to say much nonsense. I will go directly to the specific steps.

Specific steps:

This control open double finger zoom, double-click zoom function!!!
1. Add dependency:

compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.5.0'

2. Layout:

<com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
        android:id="@+id/subsampling_scale_image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

3. Use:

subsamplingScaleImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);              subsamplingScaleImageView.setMinScale(0.1F);//Minimum display scale
subsamplingScaleImageView.setMaxScale( 5.0f);//Maximum display scale
File file = new File(saveFilePath); 
// Send the image file to SubsamplingScaleImageView. Here, set the ImageViewState and set the initial display scale                     
// The three parameters of ImageViewState are scale, center and orientation 
 subsamplingScaleImageView.setImage(ImageSource.uri(Uri.fromFile(file)),new ImageViewState(1.0f, new PointF(0, 0), 0));

Add:
According to the size of the image itself, calculate the scale to make it consistent with the width of the screen, so that you can set it to be the same width as the screen when you first display the image.

 /**
     * Calculate the magnification required for the first display of the picture
     * @param imagePath Absolute path of pictures
     */
    public float getInitImageScale(String imagePath){
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
        WindowManager wm = this.getWindowManager();
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        // Get the width and height of the picture
        int dw = bitmap.getWidth();
        int dh = bitmap.getHeight();
        float scale = 1.0f;
        //If the width of the picture is larger than the screen, but the height is smaller than the screen, the picture will be reduced to fill the screen width
        if (dw > width && dh <= height) {
            scale = width * 1.0f / dw;
        }
        //If the width of the picture is smaller than the screen, but the height is larger than the screen, enlarge the picture to fill the screen width
        if (dw <= width && dh > height) {
            scale = width * 1.0f / dw;
        }
        //If the height and width of the picture are smaller than the screen, enlarge the image to fill the screen width
        if (dw < width && dh < height) {
            scale = width * 1.0f / dw;
        }
        //If the height and width of the picture are larger than the screen, the picture will be reduced to fill the screen width
        if (dw > width && dh > height) {
            scale = width * 1.0f / dw;
        }
        return scale;
    }

The maximum magnification of the picture is set to fill the screen width and then add 2.0f. When the picture is displayed for the first time, it will start from the upper left corner of the screen and fill the screen width exactly.

float initImageScale = getInitImageScale(saveFilePath);                      subsamplingScaleImageView.setMaxScale(initImageScale + 2.0f);//Maximum display scale subsa mplingScaleImageView.setImage ( ImageSource.uri ( Uri.fromFile (file)),new ImageViewState(initImageScale, new PointF(0, 0), 0));

Posted by capitala on Sun, 12 Jul 2020 09:12:10 -0700