2013-07-02 45 views
1

我有一个很奇怪的问题,我似乎无法修复。ScrollView里面的ImageView不会调整大小来填充屏幕(保持方面)

我似乎无法调整位于ScrollView内的ImageView的位图大小。 我测试了相同的布局(但没有滚动),图像的大小应该是这样。

我发现其他人有这种问题,对于大多数使用android:fillViewport="true"就足够了,但对我来说是行不通的。

这是我的布局:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"  
     android:fillViewport="true" 
     android:background="@drawable/bg_app" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <RelativeLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="5dp" 
      android:background="@drawable/custom_white_background" 
      android:padding="5dp"> 

      <ImageView 
       android:id="@+id/full_image_view" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:adjustViewBounds="true" 
       android:scaleType="fitCenter" 
       android:src="@drawable/bg_app" 
       android:contentDescription="@string/otl_img_description" 
       android:layout_margin="5dp" 
       android:padding="5dp" />    

      <View 
       android:id="@+id/divider2" 
       android:layout_width="match_parent" 
       android:layout_height="3dp" 
       android:layout_below="@+id/full_image_view" 
       android:layout_centerHorizontal="true" 
       android:layout_marginLeft="5dp" 
       android:paddingRight="5dp" 
       android:layout_marginRight="5dp" 
       android:paddingLeft="5dp" 
       android:alpha="0.3" 
       android:background="@color/black_50" 
       android:fadingEdge="horizontal" /> 

      <TextView 
       android:id="@+id/onthelist_animal_name" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:layout_alignParentLeft="true" 
       android:layout_below="@+id/full_image_view" 
       android:layout_margin="5dp" 
       android:layout_toLeftOf="@+id/onthelist_animal_date" 
       android:gravity="left|center" 
       android:padding="5dp" 
       android:text="@string/otl_animal_name" 
       android:textColor="@color/blue" 
       android:textStyle="bold" 
       android:textSize="18sp" /> 

      <TextView 
       android:id="@+id/onthelist_animal_date" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/full_image_view" 
       android:layout_centerHorizontal="true" 
       android:layout_margin="5dp" 
       android:gravity="center" 
       android:padding="5dp" 
       android:text="@string/otl_animal_date" 
       android:textColor="#a00000" 
       android:textStyle="bold" 
       android:textSize="18sp" /> 

      <ImageButton 
       android:id="@+id/onthelist_share" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignBottom="@+id/onthelist_animal_date" 
       android:layout_alignParentBottom="true" 
       android:layout_alignParentRight="true" 
       android:layout_alignTop="@+id/onthelist_animal_date" 
       android:layout_toRightOf="@+id/onthelist_animal_date" 
       android:adjustViewBounds="true" 
       android:paddingRight="5dp" 
       android:contentDescription="@string/otl_animal_share" 
       android:scaleType="fitEnd" 
       android:src="@android:drawable/ic_menu_share" 
       android:background="@android:color/transparent" />   

     </RelativeLayout> 

     <Button 
      android:id="@+id/onthelist_save" 
      style="@style/ButtonText" 
      android:layout_height="wrap_content" 
      android:background="@layout/custom_red_button" 
      android:text="@string/otl_save" 
      android:layout_margin="5dp" 
      android:padding="5dp" /> 

     <TextView 
      android:id="@+id/onthelist_animal_description" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="5dp" 
      android:background="@drawable/custom_white_background" 
      android:padding="5dp" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:textColor="@color/black" 
      android:text="@string/otl_animal_description" 
      android:linksClickable="true" /> 

    </LinearLayout> 
</ScrollView> 

,我设置图像的ImageView的full_image_view这样的:

 imageView = (ImageView) findViewById(R.id.full_image_view); 
    Log.i("OnTheList", "itemID[FI] = " + position); 
    try { 
     imageView.setImageBitmap(new ImageLoadTask().execute(
       JSONParserOnTheList.ANIMALS.get(position).photo).get());    
    } catch (InterruptedException e) { 
     imageView.setImageResource(R.drawable.default_photo); 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     imageView.setImageResource(R.drawable.default_photo); 
     e.printStackTrace(); 
    } 

的的AsyncTask返回一个位图。

谢谢。

EDIT(解决方案 - > FIX)

许多事物和方法后应用试图解决我找到了解决这一问题。 这个想法是,布局工作正常,问题是,在某些设备上,由于像素密度,图像调整了较小的版本。

当我解码位图时,我应用的修正直接在asynctask中。

所以布局和一切保持不变和asyntask是这样的:

private class ImageLoadTask extends AsyncTask<String, Void, Bitmap> { 
    private Bitmap bitmap; 
    @Override 
    protected void onPreExecute() { 
     loading.setTitle("Loading image ..."); 
     loading.show(); 
     super.onPreExecute(); 
    } 

    @Override 
    protected Bitmap doInBackground(String... params) { 
     try { 
      URL url; 
      url = new URL(params[0]); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 
      BitmapFactory.Options options = new BitmapFactory.Options(); 

      // Bitmap scale depending on density 
      // For example: 
      // 1600*1200 image to be resized to 640*480 
      // (1600*2 equal to 640*5) 
      options.inDensity = 5; 
      options.inTargetDensity = 2; 
      options.inScaled = false; 
      // Decode the bitmap 
      bitmap = BitmapFactory.decodeStream(input, null, options); 
      input.close(); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return bitmap; 
    } 

    @Override 
    protected void onPostExecute(Bitmap result) { 
     loading.dismiss(); 
     loading.cancel(); 
     super.onPostExecute(result); 
    } 
} 

通告BitmapFactory的选项是修复该问题的重要组成部分。

而且,为了不有一个非常大的图像(又名全尺寸图像),你可以给取决于屏幕的大小,这样的尺寸:

ImageView imageView = (ImageView) findViewById(R.id.full_image_view); 
imageView.setMaxHeight(screenSize()[0]-150); 
imageView.setMaxWidth(screenSize()[1]-150); 


private int[] screenSize() { 
    DisplayMetrics metrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(metrics); 

    int height = metrics.heightPixels; 
    int width = metrics.widthPixels; 

    return new int[] {height, width}; 

希望这有助于任何人,在这个斗争未来。 }

+0

经过一些测试后,我发现这可能是因为我在ImageView中放置了一个位图... 当我通过XML布局将相同的图像从drawable的ImageView中放入ImageView时,它的大小可以调整。 有谁知道我该如何解决这个问题? 谢谢! –

+0

我注意到的另一件事是,在一些设备上一切正常。从我在这里测试,结果如下: 它适用于:三星Galaxy Tab 2.0和HTC Desire Bravo。 它不适用于:Samsung Galaxy S4。 这可能是什么原因? –

回答

0

请用一句话说明问题的话,对其他部分的变化全部:

android:layout_below="@+id/full_image_view" 
android:layout_alignBottom="@+id/onthelist_animal_date" 
android:layout_alignTop="@+id/onthelist_animal_date" 
android:layout_toRightOf="@+id/onthelist_animal_date" 

要:

android:layout_below="@id/full_image_view" 
android:layout_alignBottom="@id/onthelist_animal_date" 
android:layout_alignTop="@id/onthelist_animal_date" 
android:layout_toRightOf="@id/onthelist_animal_date" 

E.G. - 删除“+”,这仅用于“android:id”并标记布局的名称。

+0

问题是显示的图像没有调整大小(它更小,更小),这意味着我希望根据设备屏幕大小调整大小(但也要保持它的比例)。 在布局中的项目安排不是一个问题,但ImageView的大小! –

2

请加fadingEdge和fillViewport以滚动型

<ScrollView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"  
     android:fillViewport="true" 
     android:fadingEdge="none" 
     android:background="@drawable/bg_app" > 
0

Here是真正简单的解决方案! 在您的ImageView中使用android:scaleType="fitXY"

相关问题