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

相關問題