Android Development Case-Gallery

Source: Internet
Author: User

This article does not cover UI content, and if you are a visitor who wants to know about UI, skip this article.

This article will explain in detail how to achieve smooth loading of this map library. Like the use of more than usual applications, such as (see), Weibo and other applications, have implemented the library function, including the main features:

    • Show all pictures by default
    • Display pictures by catalog

In addition, the interface features include:

    • Thumbnail image
    • List of pictures and the number of pictures contained in the table of contents

Discussion: On Android, how do I achieve smooth loading of photos from local photo albums?

Key points of knowledge

    • contentprovider -Data access interface
    • Cursorloader -cursor Asynchronous loader
    • Android Universal Image Loader

P.s. The difficulty of implementing the library is how to quickly query out images and directory information, it seems that Android does not directly provide such interface, we can only use Android.provider.MediaStore.Images.Media and C2>android.provider.mediastore.images.thumbnails. Although we can use Thumbnails to query the thumbnail information and image ID, but it does not provide details of the picture, in addition, if the information used to save the thumbnail or directory is (accidental or artificial) deleted, then use Thumbnails Basically, it doesn't make any sense. Therefore, we use Media here to query images and directories.

Implementation code

> Definition

Static Final Uri Content_uri = Media.external_content_uri; Static Final String Sort_order = media.date_modified + "DESC";

Here, we only query the images on the sdcard and arrange them in reverse order of modification time.

> Search Images

Code slightly, directly using cursorloader to load the picture in sort_order order, if the directory is specified, then set the media.bucket_id + "=?" Query criteria

> Search Catalogue

As with the query image, you can also use Cursorloader to load the data, but set the parameters differently, as follows:

//Method OneStatic Finalstring[] Projection_d ={"DISTINCT" +media.bucket_id, Media.bucket_display_name,"COUNT (*) as" +Media._count};//Method TwoStatic Finalstring[] PROJECTION ={media.bucket_id, media.bucket_display_name, media._id,"COUNT (*) as" +Media._count};Static FinalString SELECTION = "1=1) GROUP by (" +media.bucket_id;Static Finalstring[] Selection_args =NULL;

It is necessary to note that if the corresponding directory does not need to display the thumbnail image of the first picture, then you can use method one, otherwise use method two (PROJECTION to use with seletion , and method one does not need). Other parameters are set to Cursorloader.

Optimization strategy

Finally, we need to mention that we use UIL (that is, Android Universal image Loader) to load the picture, but each load is our query out the original, according to GitHub on the global initial setting of the demo code, loading to 35 screen, the first screen image has been Not in the memory cache, if you re-scroll to the first screen to display the first screen image, you have to read from the original, the user experience is very poor. And we see Android albums, albums and other applications, loading very smooth. By observing these application caches, you can see that as long as you browse through a lot of images that are not being browsed, the cache becomes larger. Therefore, the conceivable way to achieve this is to:

    • Use UIL to cache the original image for smaller size thumbnails, such as 320px or 96px

Here, the performance and user experience of the library implemented in this way is basically the same as the Android album. The only disadvantage of this method is that you need to apply your own cache thumbnail, you can imagine, if the phone has installed 80% of the application has such a feature page, it is not every application to generate a set of thumbnails themselves?

END.

Android Development Case-Gallery

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.