如何在android中为特定图像动态更改imageview大小?

时间:2015-09-25 06:01:32

标签: android android-fragments android-view android-relativelayout

实际上我正在使用相对布局进行图像查看。此图像视图用于接收从Internet到应用程序的图像。我需要的是可以动态更改特定图像(例如jpeg)的图像大小和附件的另一个大小(docx,pdf)等。

我使用if条件用于图像而另一个用于附件。现在如何设置接收图像的图像大小和接收附件的另一个图像大小。我需要动态地做。注意我只为图像和附件使用一个图像视图。

<RelativeLayout>
<ImageView>
id=iv
width=wrap content
height=wrap content
</ImageView>
</RelativeLayout>

for class file

if("image/png"){
iv.setImageResource(R.drawable.abc);
or 
code to get images from web

}
else if(mimetype("application/pdf")
{
iv.setImageResource(R.drawable.abc)
}

this is the model

Help me out! 

5 个答案:

答案 0 :(得分:4)

/ *                     requestLayout()                         发生变化时调用此函数                         使该视图的布局无效。                 * /

                imageView.requestLayout();
                imageView.getLayoutParams().height = height;
                imageView.getLayoutParams().width = width;

引用:https://android--code.blogspot.com/2015/08/android-imageview-set-width-and-height.html

答案 1 :(得分:1)

RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams)imageView.getLayoutParams;
lp.width = x;
lp.height = y;
imageView.setLayoutParams(lp)

答案 2 :(得分:1)

试试这个,

 RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(width,height);
ImageView ivImage=(ImageView)findViewById(R.id.image);
ivImage.setLayoutParams(params);

答案 3 :(得分:0)

尝试使用此代码动态更改图像宽高比

private void setPicDimensions() {
        Point screenDimensions = Utils.getScreenDimensions();
        width = screenDimensions.x;
        height = Utils.getHeightFromAspectRatio(width, 16, 9);// dynamically set aspect ratio value here its 16:9
    }

     // in your image view set layout params dynamically whereever you need to change
     //Declare height and width as int and iv_pic as imageview
     LayoutParams layoutParams = iv_offer_pic.getLayoutParams();
        layoutParams.height = height;
        layoutParams.width = width;
        iv_pic.setLayoutParams(layoutParams);

答案 4 :(得分:-1)

       most important is:
  <imageview  android:scaleType="fitXY" />
   <RelativeLayout>
  <ImageView>
  id=iv
  android:scaleType="fitXY" 
  width=wrap content
  height=wrap content
  </ImageView>
  </RelativeLayout>

  for class file

   if("image/png"){
   iv.setImageResource(R.drawable.abc);
   or 
   code to get images from web
   RelativeLayout.LayoutParams lp =     (RelativeLayout.LayoutParams)imageView.getLayoutParams;
   lp.width = 100;
    lp.height = 100;
    imageView.setLayoutParams(lp)
    }
    else if(mimetype("application/pdf")
    {RelativeLayout.LayoutParams lp =     (RelativeLayout.LayoutParams)imageView.getLayoutParams;
    lp.width = 50;
    lp.height = 50;
     imageView.setLayoutParams(lp)
     iv.setImageResource(R.drawable.abc)
    }

   this is the model

   Help me out!