简体   繁体   中英

Get width of ImageView returning 0

I realize there is a question similar to this on stack overflow ( getWidth() and getHeight() of View returns 0 ) that has gotten much attention. It seems like I may be calling getWidth too early, but how can I be sure that it does have an actual width though? I'm worried because I believe I may be getting a size of zero because of how I'm implementing an imageBitmap and not an imageResource.

My onCreate:

// Byte array to bitmap
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inMutable = true;
        mainImageBitmap = BitmapFactory.decodeByteArray(byteArray, 0,
                byteArray.length, options);

        // Set bitmap
        mainImageView.setImageBitmap(mainImageBitmap);
        Log.e("", mainImageView.getWidth() + " ");
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(400, 400); //Want to use getWidth and getHeight here, just hardcoding to 400 for now
        layout.setLayoutParams(params);

use GlobalLayout

example :

Global Layout Example

attach the Global layout to the view u want when the view will be ready it will callback to this function with width and height

in your case try using the next Code

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM