2012-10-05 24 views
2

從URL加載時,我有我在我的佈局創造了一個ImageView的在我的Android應用程序形象的Android

從URL裝入的圖像此的要求裝入小尺寸。以下是ImageView的

<ImageView 
    android:id="@+id/MyImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:layout_gravity="center_horizontal">   
</ImageView> 

然後在我的代碼我用下面這個ImageView的

ImageView bmImage = (ImageView)this.findViewById(R.id.MyImage); 
String urldisplay = urls[0]; 
Bitmap mIcon11 = null; 
try { 
    InputStream in = new java.net.URL(urldisplay).openStream(); 
    mIcon11 = BitmapFactory.decodeStream(in); 
} catch (Exception e) { 
    Log.e("Error", e.getMessage()); 
    e.printStackTrace(); 
} 

bmImage.setImageBitmap(mIcon11); 

是躺在我的URL路徑上的圖像來加載圖像尺寸爲320×50。但非常奇怪的是,當圖像顯示在ImageView中時,尺寸變得非常小,幾乎是寬度和高度的一半

我嘗試了很多,但沒有解決方案。任何人都可以幫忙嗎?

+1

這是什麼的ImageView在佈局的父這個配置?你有沒有嘗試設置ImageView的'scaleType'屬性? – acj

回答

1

我認爲問題是,你設定位圖的像素,並期望裝置像素

試圖讓屏幕密度,然後設置寬度/高度,因此相應的延伸,就像這樣:

ImageView bmImage = (ImageView)this.findViewById(R.id.MyImage); 
String urldisplay = urls[0]; 
     Bitmap mIcon11 = null; 
     try { 
      InputStream in = new java.net.URL(urldisplay).openStream(); 
      mIcon11 = BitmapFactory.decodeStream(in); 
     } catch (Exception e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 

bmImage.setImageBitmap(mIcon11); 
if(mIcon11 != null){ 
    int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mIcon11.getWidth(), this.getResources().getDisplayMetrics()); 
    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mIcon11.getHeight(), this.getResources().getDisplayMetrics()); 

    bmImage.setMinimumWidth(width); 
    bmImage.setMinimumHeight(height); 
} 
+0

謝謝albattran,它的工作。你救了我的日子:-) – user669231

+0

請再給我一個幫助,我希望橫幅集中在屏幕上,但沒有解決。雖然我已將佈局設置爲center_horizo​​ntal。橫幅即將與屏幕左對齊 – user669231

+0

歡迎您!很高興我能幫上忙。 – albattran