Resize ImageView at runtime using button - Android

I display an image using the ImageView container. the image should be resized when the user enters a specific value and clicks on the update

resizing just for display, the original resource remains as it is. NO editing, saving, etc.

xml layout

<ImageView android:background="@drawable/picture01" android:id="@+id/picture01holder" android:layout_below="@+id/TextView01" android:layout_height="wrap_content" android:layout_width="wrap_content" </ImageView> 

main.java

 final ImageView pic01 = (ImageView)findViewById(R.id.picture01); 

Now, what am I doing to dynamically assign this pic01 of my choice. just a function, I can implement it inside the button myself. I believe that I need to use something like pic01.setLayoutParams, but I do not know how to use it.

Basically I want to overwrite layout_height = "wrap_content" and layout_width = "wrap_content" in .java

+4
source share
3 answers

resize Imageview by taking LayoutParams

  final ImageView pic01 = (ImageView)findViewById(R.id.picture01); LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(50, 50); pic01.setLayoutParams(layoutParams); 
+5
source

First of all, you should set the scaled image to CENTER_INSIDE or CENTER_CROP depending on your preference. You must do this in your onCreate () method.

 myImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 

Afterword you just need to resize the image in the layout. This will depend on the type of layout used. Example for LinearLayout:

 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100,200); myImageView.setLayoutParams(params); 

Or you can set minimum component sizes:

 myImageView.setMinimumWidth(200); myImageView.setMinimumHeight(200); 

In both cases, make sure the size can be achieved inside the layout. If you have several components with a lower weight, the image may not be able to take up the necessary space.

+4
source
 RelativeLayout.LayoutParams myParams = new RelativeLayout.LayoutParams(yourWidthHere, yourHeightHere); pic01.setLayoutParams(myParams) 
0
source

All Articles