Performance optimization series

APP startup optimization

UI rendering optimization

Memory optimization

Image compression

Long figure optimization

Power optimization

Dex encryption

Dynamic replacement Application

Exploring the principle of hot fix for APP stability

APP continuous running process alive implementation

ProGuard compresses code and resources

APK limit compression

Long figure optimization

Now more than 95 percent of the market, the APP has the function of preview pictures, so how to do le, today we will imitate the micro blog preview long picture, to optimize the long picture.

How to use

Code transfer matrix

  1. project/build.gradle

    	allprojects {
    		repositories {
    			...
    			maven { url 'https://jitpack.io'}}}Copy the code
  2. app/build.gradle

    dependencies {
    	        implementation 'com. Making. Yangkun19921001: long_picture_view: 1.0.2'
    	}
    Copy the code
  3. XML

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

     BigView bigView=findViewById(R.id.bv_img);
     // Load path
     bigView.setImage(BIG_IMAGE_PAHT);
     / / load InputStream
     bigView.setImage(InputStream is)
     // Load the network image
     bigView.setNetUrl(String url,LoadNetImageCallBack callBack);
    Copy the code

Effect of contrast

Micro blog long picture effect

Imitate micro blog preview long picture effect

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

Memory Usage

No memory leak, jitter, or overflow. If you are not familiar with memory optimization, you can see performance optimization (3)…

Java heap memory: between 5-6 M

Native memory: about 25 MB

solution

Rough steps:

  1. Zoom along the diagonal
  2. Load the area that the screen can see
  3. Overuse memory from the previous bitmap region
  4. Deal with sliding

The detailed steps

  1. Defines the area where the Rect image needs to be loaded
  2. Define image reuse bitmapfactory.options ()
  3. Defines a helper class for gesture recognition GestureDetector to slide up and down left and right
  4. Define a sliding band class Scroller
  5. Loading image information is not loading the actual image
  6. The memory overcommitment function is enabled
  7. Create a region decoder that decodes only part of the region.
  8. The refresh requestLayout
  9. Determine the area of the image to load and calculate the scale ratio column
  10. OnDraw Draws an area picture
  11. Scale the image area to get the size of the View
  12. DrawBitmap Draws a picture
  13. Hand over the handling of the swipe event to the gesture
  14. With the finger slide Rect change the reality area, judge the bottom of the head boundary
  15. Invalidate keeps redrawing
  16. OnFling, computeScroll deals with inertia
  17. Invalidate keeps redrawing