0

其实我正在使用相对布局图像视图。该图像视图用于从互联网接收图像到应用程序。我需要的是我可以动态地改变图像的大小为一个特定的图像(说jpeg)和另一个大小的附件(docx,pdf)等。如何在android中为特定图像动态更改imageview大小?

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

<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! 
+0

后一些代码的人。 –

回答

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

当我为图像和附件单独设置上述代码时,所有图像和附件采用任何一种声明的参数。有没有变化的附件大小和图像 – kalai

+0

我可以理解你的问题。可能是一些代码。 –

+0

其实我正在使用相对布局图像视图。这意味着你使用RelativeLayout作为容器? –

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); 
+0

谢谢你的代码片段 – kalai

1

试试这个,

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

虽然这段代码可能解决OP的问题,但有几句解释会帮助当前和未来的读者更好地理解你的答案。 – Thom

+0

谢谢你的片段 – kalai

-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! 
+0

谢谢你的例子 – kalai

相关问题