2012-06-20 19 views
0

我有一個高度200和400像素的圖像。我想要顯示所有這些圖像的方式是高度200像素。我的意思是說,無論圖像的大小,而顯示該圖像我想要顯示圖像高達200像素的高度。圖像的其餘部分是隱藏的。那麼如何做到這一點。我已經使用了一個代碼解碼,但在這裏它拉伸更大的圖像,然後顯示它。在我的情況下,我不希望圖像伸展,但只顯示圖像高達200像素。如何解碼圖像文件

代碼中,我使用

private Bitmap decodeFile(File f) 
{ 
    try 
    { 
     // decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 

     // Find the correct scale value. 
     final int REQUIRED_SIZE = 200; 
     int height_tmp = o.outHeight; 
     while(true) 
     { 
      if(height_tmp/2 < REQUIRED_SIZE) 
       break;   
      height_tmp/=2; 
     } 

     o.inSampleSize = height_tmp; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o); 
    } 
    catch (FileNotFoundException e) 
    {} 
    return null; 
} 
+0

http://stackoverflow.com/a/10703256/884674您可以在運行時也調整位圖大小 –

回答

1

只需編輯您的main.xml

android:layout_width="200dp" 
    android:layout_height="200dp" 
+0

我的要求是不同的。其實我的圖像保存在內部存儲文件。所有的圖像都有不同的高度。所以我想從內部存儲器中讀取所有圖像,並以高度爲200像素的列表視圖顯示它。 – AndroidDev

+0

好的,你使用LinearLayout還是Relative? –

+0

相對佈局。如果我固定大小的圖像高度超過200像素拉伸,而顯示 – AndroidDev

1

好,所以努力的LayoutParams爲編程做同樣的任務。

public void taskCompleted() 
{ 

    ImageView iv = new ImageView(this); 


    Bitmap completionBitmap = null; 
    completionBitmap = BitmapFactory.decodeFile(CommonMessageConstants.BASE_IMAGE_FOLDER_ON_DEVICE +"completionimage.png"); 


    iv.setImageBitmap(completionBitmap); 
    RelativeLayout rl = (RelativeLayout) findViewById(R.id.randomImageView); 
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    //lp.setMargins(200, 200, 0, 0); 

    lp.addRule(RelativeLayout.CENTER_IN_PARENT); 


    rl.addView(iv, lp); 

    Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show(); 

}