Image size is wrong after collapse/expanding

101 Views Asked by At

I have a box that contains:

  • a Button: touch to collapse or expand the below image.

  • an CustomImageView: is set GONE when collapse, set VISIBLE when expand. The image is resized to fill the width of the screen as following code:

    public class CustomImageView extends ImageView {

    public CustomImageView(Context context) {
        super(context);
    }
    
    public CustomImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    
        Drawable drawable = getDrawable();
        if (drawable != null)
        {
            int width =  MeasureSpec.getSize(widthMeasureSpec);
            int diw = drawable.getIntrinsicWidth();
            if (diw > 0) {
                int height = width * drawable.getIntrinsicHeight() / diw;
                setMeasuredDimension(width, height);
            }
            else {
                super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            }
        }
        else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    
    }
    

    }

At first running default, the image is expanded (VISIBLE).

+-------------+
|-----(+)-----|
===============
| ----------- |
| |         | |
| |  (IMG)  | |
| |         | |
| ----------- |
===============

After press button +, the image is GONE,

+-------------+
|-----(-)-----|
===============

Next, pressing button -, the image is set VISIBLE,

+-------------+
|-----(+)-----|
===============
| ----------- |
| |  (IMG)  | |
===============

The issue is here, the image is display only part of it, not a full height. If I use the default support ImageView class, it certainly displays correctly, however, it doesn't resize to full-width of the screen (height is resized with width-ratio).

Can anyone point me out about this issue? Thanks!

0

There are 0 best solutions below