【问题标题】:Android list view image does not fit screen widthAndroid 列表视图图像不适合屏幕宽度
【发布时间】:2012-06-05 10:08:10
【问题描述】:

我有这个列表视图,其中的图像视图不适合屏幕宽度,即使位图的大小大于屏幕大小。这是我得到的结果:

(由于垃圾邮件政策,我不能在这里发布我的截图。请点击下面的链接查看截图。) http://www.sundancepost.com/ivue/screen/screen1.jpg

如上面屏幕中的红色框所示,列表不适合屏幕的宽度。

以下是我想要的结果:

http://www.sundancepost.com/ivue/screen/screen2.jpg

这是我的布局代码。

featured_listrow.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:orientation="vertical">    

<ImageView
   android:id="@+id/banner"        
   android:layout_width="fill_parent"       
   android:layout_height="wrap_content"
   android:adjustViewBounds="true"
   android:src="@drawable/ampersand_banner"/>

</RelativeLayout>

精选列表:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/android:list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:smoothScrollbar="true" 
    >

</ListView>

</LinearLayout>

我相信我已经为这两个布局文件正确设置了 layout_width 和 layout_height。我认为这与运行时的 android:adjustViewBounds 设置有关。有人可以帮帮我吗?

【问题讨论】:

  • 很遗憾,结果还是一样。即使我调整图像大小以适应屏幕宽度也会发生这种情况。
  • 尝试使用 scaleType 的值。另外,请显示 listVIew 的适配器代码。我不认为 xml 有什么问题。

标签: android listview android-layout imageview


【解决方案1】:

我终于发现了问题所在。图片的缩放发生在 http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

中的 ImageLoader.class

所以,当我注释掉代码的缩放部分时,一切都恢复正常了。

这是代码:

private Bitmap decodeFile(File f){
    try {
        //decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f),null,o);

        //Find the correct scale value. It should be the power of 2.
       // final int REQUIRED_SIZE=100;
       // int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;
      //while(true){
        //if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
        //break;
        //width_tmp/=2;
        //height_tmp/=2;
        //scale*=2;
       //}

        //decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}

【讨论】:

    【解决方案2】:

    您可以使用

    在图像视图中设置图像

    1) android:scaleType="fitXY"

    2) android:scaleType="centerCrop"

    在您的图像视图中使用任何一种。它给了我想要的完美结果。

    【讨论】:

      【解决方案3】:

      我认为在这里唯一可以帮助的是使用其他程序调整图像大小。问题是图像不能以编程方式仅裁剪水平或垂直边,在 xml 或代码中设置图像大小时总是这样做。

      【讨论】:

      • 我确实通过 Photoshop 调整了图像大小。我图像的当前宽度是 480 像素,应该很适合我的 android 设备。但是,碰巧的是,即使图像尺寸正确,图像仍然不适合屏幕。
      • 有趣的是,当我将 features_listrow.xml 切换到图形布局时,图像看起来很适合屏幕的宽度。但是,它只是不会在运行时发生。
      猜你喜欢
      • 2012-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多