简体   繁体   中英

Understanding and implementing OnSizeChanged to get ImageView width and height

So I have an ImageView that I have declared in my xml.

class.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/relativeLayout"
    tools:context=".ImageConfirmation" >

    <ImageView
        android:id="@+id/confirmationView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        />

</RelativeLayout>

And my Java file looks like.

MyClass.java

public class MyClass extends Activity{

    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_confirmation);
        imageView = (ImageView) findViewById(R.id.confirmationView);

        }
}

Now when I try to do imageView.getWidth() and imageView.getHeight() it always returns me ZERO. So I was thinking of using the protected void onSizeChanged (int w, int h, int oldw, int oldh) from here . But I am not sure if this is the right way to do it.

If yes then how should I go about implementing it in my code structure. My class already extends an Activity so not sure how to implement the onSizeChanged.

I also tried doing

ViewTreeObserver vto = confirmationView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                imageViewSurfaceWidth = imageView.getWidth();
                imageViewSurfaceHeight = imageView.getHeight();
            }

        });

But this didn't work for me either. How to get the imageView width and Height.

You can use onSizeChange method, for this way, you should write your custom ImageView and override onSizeChange . In this method the height and width will be provided.

But attentation, this method won't be called in the before onCreate() in Activity.

I believe you can use this way to get the height and width:

imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            imageViewSurfaceWidth = imageView.getWidth();
            imageViewSurfaceHeight = imageView.getHeight();
            Log.i("activity", "width = "+imageViewSurfaceWidth+", height = "+imageViewSurfaceHeight);
        }
    });

I use this way to for this purpose for a long time, and it works well.

You said this doesn't work, and I think it should not happen, maybe you should log the height and width you get in onGlobalLayout() to see what is wrong.

I use this inside main activity:

@Override
public void onWindowFocusChanged(boolean hasFocus) {

 super.onWindowFocusChanged(hasFocus);
main_width=main_view.getWidth();
main_height=main_view.getHeight();


}

where main_view is your imageView.

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