I have a picture with a Match_Parent width and a height of 160dp and below all sorts of texts. The 4-inch looks fine! I need to make it so that with large screens, for example, if the screen is larger than 5 inches, the picture also increases without losing proportions!

  • Why do so? you can use Drawable Importer -a to generate all screen sizes and that's all - redL1ne
  • I do not understand to be honest. - DevOma pm
  • That is, all these folders throw different sizes of the picture? - DevOma pm
  • Well, yes, programmatically changing the size of it looks a bit like a bicycle) why is it so difficult? - redL1ne
  • I do not argue, this option is possible if the picture is somewhere on the server and you need to programmatically download it and substitute it in the layout some - redL1ne

1 answer 1

Described before how to make ResizableImageView :

 public class ResizableImageView extends ImageView { public ResizableImageView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ Drawable d = getDrawable(); if(d!=null){ int width = MeasureSpec.getSize(widthMeasureSpec); int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth()); setMeasuredDimension(width, height); }else{ super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } } 

The image will occupy the entire width of the container, and then adjust the height to the desired proportions.

Instead of the usual ImageView , do this in the markup:

  <ru.your.package.ResizableImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="160dp" android:src="@drawable/ivHeader" /> 
  • Thanks, I'll try ... - DevOma
  • And where to register my picture ID pictures for example ivHeader? - DevOma
  • @Omuradil <ru.your.package.ResizableImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="160dp" android:src="@drawable/ivHeader" /> - Suvitruf
  • I have a gray background instead of a picture! What is the problem? Of course, I registered my package! - DevOma
  • one
    You can also use the android:adjustViewBounds . It is just for the image to retain the original aspect. Only it does not work at all scaleType and everything seems to be correct only with API 18. - xkor