Use ContentProvider to scan local pictures and display them in Android applications

Before, a friend in the group asked me if there was a demo about local image selection, which is similar to the effect of wechat. He said there was no demo on the Internet, and asked me if I could write a demo about this effect, so I studied the demo of local image selection on wechat, wrote it and shared it with you. I hope it will be of some help to friends who need it in the future, The content provider is mainly used to scan the pictures in the mobile phone and display them with GridView. The problem of displaying pictures with GridView and listview has always been a headache, because our mobile phone has limited memory and the memory allocated by the mobile phone to each application is also limited, so it is easy to accompany OOM when there are many pictures, However, there are many open-source image display frameworks that optimize the display of many images. You can learn about them if you are interested. Today, my article uses the lrucache class and cuts the images accordingly, so as to avoid the occurrence of oom as much as possible. Let's take a look at the effect of wechat first

Next, let's realize these effects. First, let's create a new project named imagescan. First, let's look at the first interface. Use to scan the pictures in the mobile phone, then classify them according to the folder where the pictures are located, and display a picture in the folder and the number of pictures in the folder. According to the interface elements (folder name, number of folder pictures, and one picture in the folder) use an entity object imagebean to encapsulate these three properties

The next step is the layout of the main interface. I didn't add the navigation bar above, but only the GridView below, so there is only one GridView in the layout of the main interface

The next step is the layout of GridView's item. If you look at the above figure, you will think that its effect is the effect of adding two pictures. In fact, it is not. The later superposition effect is just a background picture, and the code is pasted first

It's OK to see the layout code above. You've found that the custom myimageview is used above. I won't talk about the role of this custom myimageview first. I'll tell you later. Let's continue to look at the main code of the first interface of the code

To customize the code of myimageview, we only need to set onmeasurelistener to listen. After myimageview is measured, the measured width and height will be recalled to onmeasuresize(), and then we can cut the picture according to the size of myimageview

The above code completes the function of the first interface. Next, click the item of the GridView to jump to another interface to display all the pictures under the folder. The function is similar to that of the first interface. It also uses the GridView to display the pictures. I will not paste the layout code of the second interface, but directly paste the code of the interface

On the item of GridView, we have a custom myimageview to display pictures, and there is another one Check@R_ 760_ 2419 @ to record our selection. The code of the adapter is as follows

The adapter of the second interface is similar to that of the first interface, but there is one more Check@R_ 760_ 2419 @ to record the picture selection, we only need to Check@R_ 760_ 2419 @ set setoncheckedchangelistener to listen. After wechat is selected Check@R_ 760_ 2419 @ has an animation effect, so I use the nineoldandroids animation library to give it Check@R_ 760_ 2419 @ add an animation effect, directly call addanimation() method to add it, and getselectitems() method to get the position of the selected item. When you know the selected position, you will know all other information. Wechat has the function of previewing pictures, so I won't add it here. If you have this requirement, you can add it yourself, Recommend one to everyone https://github.com/chrisbanes/PhotoView

Run the project, the effect is as follows

It looks good. It reads pictures asynchronously, caches and cuts the pictures, so that it is more smooth in displaying local pictures. The GridView sliding is also very smooth, and it can effectively avoid the generation of oom.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>