2014-01-08 227 views
2

我遇到了問題,我的佈局是ImageView。隨着AsyncTask我從網上加載圖像。這是一張全景照片。如果我啓動Fragment,我將加載src="@drawable/pano"的圖像完美顯示。它有全高,我可以滾動瀏覽全景。更改圖像時更改ImageView大小

一旦新的圖像加載到ImageView的ImageView的重定比例,這樣的高度:

之前的AsyncTask(圖像從繪圖資源加載中...):

before

這是後裝的AsyncTask形象: after

這裏是我的xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<HorizontalScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerInParent="true" 
    android:overScrollMode="never" 
    android:scrollbars="none" > 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" > 

     <ImageView 
      android:id="@+id/img1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentTop="true" 
      android:adjustViewBounds="true" 
      android:scaleType="fitCenter" 
      android:src="@drawable/pano" /> 
    </RelativeLayout> 
</HorizontalScrollView> 

的AsyncTask代碼:

public class DownloadPanorama extends 
      AsyncTask<ImageView, Void, Bitmap> { 
     ImageView imageView; 
     Bitmap bm; 

     public DownloadPanorama(ImageView mChart) { 
      imageView = mChart; 
     } 

     @Override 
     protected Bitmap doInBackground(ImageView... imageViews) { 
      return download_Image((String) imageView.getTag()); 
     } 

     @Override 
     protected void onPostExecute(Bitmap result) { 
      imageView.setImageBitmap(result); 
     } 

     private Bitmap download_Image(String url) { 
      Bitmap bm = null; 
      try { 
       URL aURL = new URL(url); 
       URLConnection conn = aURL.openConnection(); 
       conn.connect(); 
       InputStream is = conn.getInputStream(); 
       BufferedInputStream bis = new BufferedInputStream(is); 
       bm = BitmapFactory.decodeStream(bis); 
       bis.close(); 
       is.close(); 
      return bm; 
     } 
    } 

不使用的AsyncTask,只使用原始setImageBitmap時也發生了......所以我不知道如果我要改變scaleType在我的XML文件,或者如果我需要在加載新圖像後再次設置它?

+0

嘗試改變的ImageView'安卓layout_height'到'match_parent' – iTurki

+0

感謝您的幫助。這不起作用:( – user754730

+0

你可以發佈你的'AsyncTask'代碼嗎? – Phil

回答

0

編輯

這聽起來像您的意見和更新的問題,你知道你想要的圖像是大小(1200x400)。你有兩件事可以做到這一點。第一,和推薦,是

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.outWidth=1200; 
options.outHeight=400; 
bm = BitmapFactory.decodeStream(bis, new Rect(0,0,0,0), options); 

另一種選擇是改變你的瀏覽範圍,以取代線

bm = BitmapFactory.decodeStream(bis); 

。在上面您的意見,這聽起來像界限可能會改變,所以你有這樣您加載不同大小的新形象,每次重複:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) imageView.getLayoutParams(); 
params.width = 1200; 
params.height = 400; 
imageView.setLayoutParams(params); 
您需要更改 機器人:adjustViewBounds =」真正的」 到 機器人:adjustViewBounds = 「假」
+0

感謝您的幫助!但是這也不能解決它... – user754730

+0

請張貼您的asynctask代碼 - 如果行爲有或沒有不同使用它,然後有什麼是錯誤 – Martin

+0

沒有行爲是相同的...它只是不同,如果我從XML加載它,並且如果我使用setImageBitm AP ... – user754730

0

我會嘗試改變:

android:layout_height="wrap_content" 

android:layout_height="fill_parent" 
在您的ImageView

如果不工作,你可能能夠擴展您的位圖:

myBitmap = Bitmap.createScaledBitmap(myBitmap, width, height,true); 
+0

這也表現了相同:( – user754730