Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize ImageView in run time using a button - Android

I am displaying an image using ImageView container. the image needs to be resized when a user enters a specific value and clicks update

resize is just for display, the original resource remains as it is. NO editing, save, 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 do i do to dynamically assign this pic01 a size of my choice. just the function, i can implement it inside a button myself. i believe i have to use something like pic01.setLayoutParams but i dont know how to use it.

basically i want to overwrite layout_height="wrap_content" and layout_width="wrap_content" in .java

like image 649
kasai Avatar asked Dec 27 '22 12:12

kasai


2 Answers

change the Imageview size by taking LayoutParams

    final ImageView pic01 = (ImageView)findViewById(R.id.picture01);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(50, 50);
    pic01.setLayoutParams(layoutParams);
like image 91
jazz Avatar answered Jan 07 '23 10:01

jazz


First of all, you have to set the scale type of the image to CENTER_INSIDE or CENTER_CROP depending on your preferences. You should do this in your onCreate() method.

myImageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

Afterwords you just have to resize the image view in your layout. This will depend on the type of layout you're using. An example for LinearLayout would be:

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

Or you can set the components minimal size:

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

In both cases make sure that the size can be achieved inside the layout. If you have multiple components with smaller weights, the image view may be unable to take up the space you need.

like image 45
Andras Balázs Lajtha Avatar answered Jan 07 '23 10:01

Andras Balázs Lajtha