2014-01-08 228 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