Let's talk about my problems:
1. I'm writing a fragment in the activity.
2. There is a imageView in this fragment for displaying pictures.
I used Asynctask to get the picture, and I'm going to show the thumbnail of the picture in this ImageView, and I'm going to use the Thumbnailutils.extractthumbnail method to generate the thumbnail.
Let's first look at Thumbnailutils.extractthumbnail (source, width, height); The parameters of this method
Source file (bitmap type)
Width compressed into widths
Height compressed into
Here you need a parameter for width and height: If you want to fill the picture again ImageView, here should pass in ImageView width and height.
But here's the problem: here, use the Imageview.getwidth method to get the width to always be 0
After a search, finally found a solution, as follows:
Private voidShowImage (FinalFile Resultfilearg) { if(Resultfilearg! =NULL&&resultfilearg.exists ()) { //Add download image to ImageViewViewtreeobserver Vto2 = imageview1.getviewtreeobserver (); Vto2.addongloballayoutlistener ( New Ongloballayoutlistener () { @Override Public voidongloballayout () {if(Build.VERSION.SDK_INT < 16) {imageview1.getviewtreeobserver (). Removeglobalonlayoutlistener ( This); } Else{imageview1.getviewtreeobserver (). Removeongloballayoutlistener ( This); } Bitmap BM=Bitmapfactory.decodefile (Resultfilearg.getpath ()); Bitmap thumbnailimg=Thumbnailutils.extractthumbnail (BM, imageview1.getmeasuredwidth (), Imageview1.getmeasuredheight ()); Bm.recycle (); Imageview1.setimagebitmap (THUMBNAILIMG); //Imageview1.setimagebitmap (BM); } }); } }
Steps to use:
1. Get a Viewtreeobserver object for ImageView first. Method: Imageview1.getviewtreeobserver ()
2. Add a listener for this Viewtreeobserver object (for example, called Ongloballayoutlistener), method: Vto2.addongloballayoutlistener
3. Complete the implementation of this ongloballayoutlistener, you can call the Imageview.getwidth method in this implementation method. Complete the functions we need here.