In the onCreate method, I try to determine the size of the ImageView component, the main layout. The program reports that they are zero (w = o, h = 0). I tried to cram in onStart (), the result is the same. When I do the sizing (ImageView) at the press of a button, everything is determined correctly. Dimensions are needed to create a picture in memory and then output to ImageView. It is necessary that when loading the program, she herself created the picture and placed it in ImageView without pressing the button (now the drawing works, but by the button).

  • In onCreate, the markup has not yet fully calculated its size. There are several ways to get around this and they are of varying degrees of hardcore. Try to get dimensions in onResume first. - Yuriy SPb

3 answers 3

You can set the parameter android:scaleType="centerCrop" on the ImageView android:scaleType="centerCrop"

and then the system will scale the image itself, place it in the center, and cut off the excess

 <ImageView android:id="@+id/ivPhoto" android:layout_width="250dp" android:layout_height="250dp" tools:background="@drawable/dummy_photo" android:scaleType="centerCrop" /> 

There are 8 types of scaling

    Use something like this:

     float scale = getBaseContext().getResources().getDisplayMetrics().density; px = dp_that_you_want * (scale / 160); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(px, px); imageView.setLayoutParams(layoutParams); 

    Hope this helped you.

    • Try this: ViewTreeObserver observer = imageView.getViewTreeObserver (); vto.addOnGlobalLayoutListener (new OnGlobalLayoutListener () {@Override public void onGlobalLayout () {imageView.getHeight ();} - artemiygreg

    Try this

    ViewTreeObserver observer = imageView.getViewTreeObserver (); vto.addOnGlobalLayoutListener (new OnGlobalLayoutListener () {@Override public void onGlobalLayout () {imageView.getHeight ();}