Long figure optimization

Now more than 95% of apps on the market have the function of preview pictures, so how to do, today we will imitate the micro-blog preview long picture, to optimize the long picture.

How to use

Code transfer array

  1. project/build.gradle

        allprojects {
            repositories {
                ...
                maven { url 'https://jitpack.io'}}} copy the codeCopy the code

  2. app/build.gradle

    dependencies {
                implementation 'com. Making. Yangkun19921001: long_picture_view: 1.0.2'} Duplicate codeCopy the code

  3. Used directly in XML

     <com.yk.big_picture_library.BigView
            android:id="@+id/bv_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/> Copy the codeCopy the code

  4. In the code

    BigView bigView=findViewById(R.id.bv_img); // Load path bigview.setimage (BIG_IMAGE_PAHT); SetImage (InputStream is); Bigview. setNetUrl(String URL,LoadNetImageCallBack callBack); Copy the codeCopy the code

Effect of contrast

Weibo long picture effect

image

<figcaption></figcaption>

Preview the long picture effect after micro-blog

image

<figcaption></figcaption>

The effect is not very smooth, the original image is 12 M compressed after less than 2 M, before loading can be compressed, I use my own another image compression open source project using Huffman algorithm for image compression.

Memory usage status

image

<figcaption></figcaption>

There is no memory leakage, jitter, or overflow. If you don’t know about memory optimization, you can look at performance optimization.

Java heap memory: between 5 and 6 M

Native memory: about 25 MB

solution

image

<figcaption></figcaption>

Rough steps:

  1. Scale diagonally
  2. Load the visible area of the screen
  3. Reuse memory from the previous bitmap region
  4. Deal with sliding

The detailed steps

  1. Define the area where the Rect image needs to be loaded
  2. Define image reuse bitmapFactory.options ()
  3. Define a help class for gesture recognition of the GestureDetector swiping up, down, left, and right
  4. Define a sliding help class called Scroller
  5. Loading image information does not load the actual image
  6. The memory overcommitment function is enabled
  7. Create a region decoder that decodes only part of it.
  8. The refresh requestLayout
  9. Determine the area to load the image and calculate the zoom ratio column
  10. OnDraw draws a picture of the region
  11. Scale the image area to get the View size
  12. DrawBitmap to draw an image
  13. Handle sliding events to hand over to gesture handling
  14. Change the Rect of the real area by sliding the finger to determine the bottom limit of the head
  15. Invalidate constantly redraws
  16. OnFling, computeScroll handles inertia problems
  17. Invalidate constantly redraws

Zero one thumb up

diary