如何动态更改图像大小

raj*_*raj 2 android android-image android-imageview

我已经在 xml 文件中定义了 imageview 大小。因此,当图像出现时,它将作为默认值。我根据某些条件显示不同的图像。所以,我需要改变图像的宽度和高度。

xml:

<ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"        
        android:visibility="invisible"  />
Run Code Online (Sandbox Code Playgroud)

爪哇:

if (text.equalsIgnoreCase("y")) {                      
      image.setImageResource(R.drawable.y); 

      }                    
    else if (text.equalsIgnoreCase("z")) {
    image.setImageResource(R.drawable.z); // Here i need to change the width & height of the image
               }
Run Code Online (Sandbox Code Playgroud)

Ill*_*ent 5

你可以这样做:

imgView.getLayoutParams().height = 300;
imgView.getLayoutParams().width= 300;
imgView.requestLayout();//changing width and height programmatically will require the view to be redrawn
Run Code Online (Sandbox Code Playgroud)