Android encounters ImageView getwidth always 0 o'clock, set Setimagebitmap method

Source: Internet
Author: User

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.

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.