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
-
project/build.gradle
allprojects { repositories { ... maven { url 'https://jitpack.io'}}} copy the codeCopy the code
-
app/build.gradle
dependencies { implementation 'com. Making. Yangkun19921001: long_picture_view: 1.0.2'} Duplicate codeCopy the code
-
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
-
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
<figcaption></figcaption>
Preview the long picture effect after micro-blog
<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
<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
<figcaption></figcaption>
Rough steps:
- Scale diagonally
- Load the visible area of the screen
- Reuse memory from the previous bitmap region
- Deal with sliding
The detailed steps
- Define the area where the Rect image needs to be loaded
- Define image reuse bitmapFactory.options ()
- Define a help class for gesture recognition of the GestureDetector swiping up, down, left, and right
- Define a sliding help class called Scroller
- Loading image information does not load the actual image
- The memory overcommitment function is enabled
- Create a region decoder that decodes only part of it.
- The refresh requestLayout
- Determine the area to load the image and calculate the zoom ratio column
- OnDraw draws a picture of the region
- Scale the image area to get the View size
- DrawBitmap to draw an image
- Handle sliding events to hand over to gesture handling
- Change the Rect of the real area by sliding the finger to determine the bottom limit of the head
- Invalidate constantly redraws
- OnFling, computeScroll handles inertia problems
- Invalidate constantly redraws
Zero one thumb up
diary