http://blog.csdn.net/guolin_blog/article/details/10470797
Remember that I have written an article on how to implement the photo wall function on Android, but that time is used to layout the GridView, this layout only applies to the "wall" on each picture is the same size, if the size of the picture is uneven, The display in the GridView will be very ugly. and using the waterfall flow layout is a good solution to this problem, so today we also to catch up on the trend, see how to implement the waterfall streaming photo wall on Android.
First of all, the implementation of the principle, waterfall flow layout, although it seems to be arranged at random, in fact, it is a very scientific arrangement rules. The entire interface is divided into columns of equal width according to the width of the screen, because the phone's screen is not very large, here we are divided into three columns. Whenever you need to add a picture, the width of the picture is compressed into the same width as the column, and then the height of the picture is compressed at the same compression scale, and then in these three columns, a column with the lowest current height is found, and the picture is added to the column. Then, whenever you need to add a new picture, repeat the above action to form a photo wall of the waterfall flow pattern, as shown below.
Implementation of the main ideas:
Encapsulates a Imageloader class for picture management, caching, and compression
Public classImageloader {/*** The core class of image caching technology, which caches all downloaded images and removes the least recently used images when the program memory reaches the set value. */ Private StaticLrucache<string, bitmap>Mmemorycache; /*** Examples of imageloader. */ Private StaticImageloader Mimageloader; PrivateImageloader () {//get the maximum available memory for an application intMaxMemory = (int) Runtime.getruntime (). MaxMemory (); intCacheSize = MAXMEMORY/8; //set the picture cache size to the 1/8 of the program's maximum available memoryMmemorycache =NewLrucache<string, bitmap>(cacheSize) {@Overrideprotected intsizeOf (String key, Bitmap Bitmap) {returnBitmap.getbytecount (); } }; } /*** Get an instance of Imageloader. * * @returnan instance of Imageloader. */ Public StaticImageloader getinstance () {if(Mimageloader = =NULL) {Mimageloader=NewImageloader (); } returnMimageloader; } /*** Store a picture in the LRUCache. * * @paramKey * LRUCache, where the URL of the image is passed in. * @parambitmap * LRUCache key, where bitmap objects downloaded from the network are passed in. */ Public voidAddbitmaptomemorycache (String key, Bitmap Bitmap) {if(Getbitmapfrommemorycache (key) = =NULL) {mmemorycache.put (key, bitmap); } } /*** Get a picture from LRUCache and return NULL if it does not exist. * * @paramKey * LRUCache, where the URL of the image is passed in. * @returnthe bitmap object that corresponds to the incoming key, or null. */ PublicBitmap Getbitmapfrommemorycache (String key) {returnMmemorycache.get (key); } Public Static intcalculateinsamplesize (bitmapfactory.options Options,intreqwidth) { //the width of the source picture Final intwidth =Options.outwidth; intInsamplesize = 1; if(Width >reqwidth) { //calculates the ratio between the actual width and the target width Final intWidthRatio = Math.Round ((float) Width/(float) reqwidth); Insamplesize=WidthRatio; } returninsamplesize; } Public StaticBitmap Decodesampledbitmapfromresource (String pathName,intreqwidth) { //The first resolution sets Injustdecodebounds to true to get the picture size FinalBitmapfactory.options Options =Newbitmapfactory.options (); Options.injustdecodebounds=true; Bitmapfactory.decodefile (pathName, Options); //call the method defined above to calculate the Insamplesize valueOptions.insamplesize =calculateinsamplesize (options, reqwidth); //parse the picture again using the obtained insamplesize valueOptions.injustdecodebounds =false; returnbitmapfactory.decodefile (pathName, Options); } }
Android Waterfall Stream Photos