2017-01-16 26 views
1

我知道這個主題有很多問題。但我仍然需要幫助。我想在我的TableLayout中顯示我的ImageView等於widthheight。但在不同的設備上,我無法做到這一點。Android - 來自網絡服務的圖片在不同設備上具有不同的尺寸

這裏是我在AsyncTask中使用URL的圖像,並在onPostExecute上設置了ImageView方法。

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) { 
    // set the imageview 
    bmImage.setImageBitmap(result); 
} 

這裏是一個TableRow像,一邊裏面在我的ImageView的位於

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 

android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:paddingRight="10dp" 
android:paddingEnd="10dp" 
android:layout_marginRight="10dp" 
android:layout_marginEnd="10dp" 
android:paddingBottom="10dp" 
android:id="@+id/mylayout" 
> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/image" 
    android:clickable="true" 
    android:background="@drawable/border"/> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="75dp" 
    android:maxLines="4" 
    android:id="@+id/text" 
    android:layout_below="@+id/image" 
    android:layout_alignEnd="@+id/image" 
    android:layout_alignRight="@+id/image" 

    android:clickable="true" 
    android:textSize="18sp" 
    android:background="#e3e3e3" 
    android:paddingTop="10dp" 
    android:gravity="center_vertical" 
    /> 


</RelativeLayout> 

我以爲我可以創建一個TableLayout,並找到View(上)我的XML文件(news.xml) (view is above)

要做到這一點,我得到屏幕寬度,並減去paddings併除以2,並將該寬度設置爲ImageView,以便能夠爲所有圖像獲得相同的大小e觀點。

這裏是我的代碼,

DisplayMetrics metrics = new DisplayMetrics(); 
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics); 

    screenWidth = metrics.widthPixels; 

    tableLayout = (TableLayout) view.findViewById(R.id.tableLayout); 


    Log.e("screenwidth",":"+screenWidth); 
    // here is my paddings converted to pixel and will be subtracted from screen width 
    int pixValue = (int) (20 * Resources.getSystem().getDisplayMetrics().density); 
    int imageWidthPix = (screenWidth - pixValue)/2; 
    Log.e("imageWidthPix ",":"+imageWidthPix); 

    for (int i = 0; i < categoryNews.get(myString).size(); i++) { 

     if (i % 2 == 0) { 
      tr = new TableRow(getActivity()); 
      tableLayout.addView(tr); 
     } 

     //here is where my ImageView layout (news.xml) 
     View oneNews = inflater.inflate(R.layout.news, null); 

     ImageView imageView = (ImageView) oneNews.findViewById(R.id.image); 
     TextView textView = (TextView) oneNews.findViewById(R.id.text); 

     //here I set the width as I want 
     imageView.setLayoutParams(new RelativeLayout.LayoutParams(imageWidthPix, RelativeLayout.LayoutParams.WRAP_CONTENT)); 

     String imagePath = "http://my.url.com/" + categoryNews.get(myString).get(i).getImagePath(); 

     new DownloadImageTask(imageView,getActivity().getApplicationContext()) 
       .execute(imagePath); 

我得到了我的手機上了完美的結果。但是在具有不同屏幕尺寸的不同設備上,ImageView頂部和底部都有空格。我試圖轉換九塊補丁塊並將其轉換爲位圖,但沒有區別。如果我無法從網絡服務獲取圖片,我會將其他分辨率圖片放入mipmap-hpdi,mipmap-mdpi等文件夾中,這樣就不會出現錯誤。但是我正在動態獲取圖像,因此如何才能實現ImageView屏幕寬度的一半? 在此先感謝。

+0

你的佈局中只有兩件事情。 –

+0

是在我的news.xml中有一個圖像視圖和一個相對佈局內的文本視圖,你可以在問題 – Hilal

回答

0

在這裏,我編輯了自己的XML

根據您的需要儘量線性佈局權,更改權重,改變​​layout_weight,讓您的圖像視圖和TextView的更小或更大。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingBottom="10dp" 
    android:orientation="vertical" 
    android:id="@+id/mylayout" 
    android:weightSum="10"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="3" 
     android:id="@+id/image" 
     android:src="@drawable/sd" 
     android:clickable="true"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:maxLines="4" 
     android:id="@+id/text" 
     android:clickable="true" 
     android:textSize="18sp" 
     android:background="#e3e3e3" 
     android:paddingTop="10dp" 
     android:gravity="center_vertical" 
     /> 


</LinearLayout> 
+0

中看到靜態imageview在它的頂部和底部有很多空間 – Hilal

+1

嘗試使用你的bitmap.createScaledBitmap();和android:fitsSystemWindows =「true」在您的圖像視圖 –

+0

好吧謝謝,'bitmap.createScaledBitmap()'似乎工作 – Hilal

相關問題