2016-02-18 77 views
0

我正在使用AsyncTask從互聯網下載圖像並將其顯示在ImageView中。對於我的應用程序,使用API​​調用來檢索圖像URL,並使用它在ImageView中顯示。它的工作原理,但圖像真的很小,很難看到。我不知道這是因爲圖像沒有存儲在drawable中,或者如果我在代碼中丟失了某些東西。爲什麼它調整我的形象?在我的應用程序在ImageView中顯示的圖像比源圖像小

enter image description here

圖像的實際大小圖像的

尺寸

enter image description here

RecipePage.java

Results result = new Results(); 

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_recipeviewer, container, false); 

     new DownloadImageTask((ImageView) rootView.findViewById(R.id.recipe_image)) 
       .execute(result.getURL()); //getURL() just contains the URL of the image 
     return rootView; 


    } 

    class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
     ImageView bmImage; 

     public DownloadImageTask(ImageView bmImage) { 
      this.bmImage = bmImage; 
     } 

     protected Bitmap doInBackground(String... urls) { 
      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(); 
      } 
      return mIcon11; 
     } 

     protected void onPostExecute(Bitmap result) { 
      bmImage.setImageBitmap(result); 
     } 
    } 

fragment_recipeviewer.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/recipe_image" /> 
</LinearLayout> 

我可能失去了一些東西明顯或做一些愚蠢的。任何解決方案?

+0

你剛纔是不是在建一個使用 –

+0

@PhanVănLinh瞭解Android Studio – Theman

+0

該模擬器我的意思是,哪個模擬器?,samsung s6,nexus 5,或... –

回答

0

更新您的佈局文件。

縮放類型有多種選擇。選擇最適合你的。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:scaleType="fitXY" 
     android:id="@+id/recipe_image" /> 
</LinearLayout> 
0

的原因是:
Nexus的5(你使用仿真器)已分辨率爲:1080×1920像素
和(your image)有分辨率爲:350×233像素
所以你的形象會顯得很小,但如果你想增加圖像的大小,您可以在顯示正確的

<ImageView 
    android:id="@id/img" 
    android:layout_width="150dp" // modify your width 
    android:layout_height="wrap_content" 
    android:adjustViewBounds="true" 
    android:scaleType="fitCenter" /> 

但要記住,如果你在一個更大的視圖中顯示的小圖像,你將會有一個壞的圖像質量 希望這有助於