【问题标题】:ImageView inside ListView and bitmap not fully displayedListView 中的 ImageView 和位图未完全显示
【发布时间】:2014-07-02 15:46:28
【问题描述】:

我在使用包含 ImageView 的 ListView 时遇到问题,它在平板电脑上可以正常工作,但在我的手机上却不行...

这是预期的输出(在我的平板电脑上):

这是实际输出(在我的手机上):

我尝试使用可绘制对象,它在我的手机上运行良好。 以下是我创建位图并将其绑定到视图的方法:

Bitmap srcBmp = BitmapFactory.decodeResource(rcontext.getResources(), R.drawable.portrait);
Bitmap modBmp = Bitmap.createBitmap(srcBmp,0,0,60,60);
((ImageView) view).setImageBitmap(thebmp);

我的行布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="60px"
        android:layout_height="60px"
        android:id="@+id/charIcon"
        android:src="@drawable/blason" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/charName"/>
</LinearLayout>

我真的不明白为什么会这样...... 我对android很陌生,但这真的很奇怪

编辑:blason 仅在我的可绘制文件夹中,其大小为 60x60 像素

EDIT2:我正在使用这个 Bitmap.createBitmap(srcBmp,0,0,60,60);因为我只需要图像的这一部分(应该显示整个头部)。这是 Portrait.png 的一部分:

【问题讨论】:

  • 您正在使用 px。尝试使用 dp。它会更好地扩展。
  • 如果您的可绘制文件夹中有图像并且它的尺寸完美,那么为什么不直接使用imageView.setImageResource(R.drawable.blason);进行设置
  • dp/px 似乎不是这里的问题。在我的平板电脑上显示整个头部,而不是在手机上,无论我为 ImageView 使用什么尺寸
  • @Shivam_Verma : blason 资源只是一个默认图像,以防我以后找不到要显示的好角色

标签: android listview bitmap


【解决方案1】:

Bitmap.createBitmap 将使用位图的来源并为您提供剪辑。正是您在结果中看到的。

尝试使用: createScaled bitmap 代替。

编辑: 如果你什么都不做,imageView 会为你调用它。所以最好的解决方案就是删除新的位图行:

Bitmap srcBmp = BitmapFactory.decodeResource(rcontext.getResources(), R.drawable.portrait);
//Bitmap modBmp = Bitmap.createBitmap(srcBmp,0,0,60,60);
((ImageView) view).setImageBitmap(thebmp);

并像@blipinsk 建议的那样在 xml 中添加缩放模式。

android:scaleType="fitXY"

编辑2:尝试将LinearLayout改为WrapContent,父级可能小于60px

编辑 3: BitmapFactory.decodeResource 将在设备密度中解码,除非您通过选项设置说,使用真实尺寸。所以这可能是您想要做的,因为您正在使用像素。

【讨论】:

  • 这正是我剪切 60x60 第一个像素的目的:/ 我有一种具有不同字符头的精灵,我只需要获得其中一个,这就是我剪切它的原因。当我回答 blipinsk 时,scaleType 没有修复它
  • @Lectem 那么结果有什么问题呢?对我来说它看起来很切。
  • 平板电脑和我的手机之间的区别似乎很明显,在平板电脑上它被正确切割(你看到整个头部),而在手机上你只看到它的一部分
  • 我确实尝试过使用 android:layout_width="wrap_content" android:layout_height="wrap_content" 但它也不起作用
  • @Lectem 比它只能是一件事。您使用的不是同一张图片
【解决方案2】:

添加

android:scaleType="fitXY"

到您的图像视图

【讨论】:

    【解决方案3】:

    我终于找到答案了! BitmapFactory.decodeResource 将根据设备像素密度缩放 png,即使您不提供不同版本的图像。 您可以像这样禁用缩放

        BitmapFactory.Options opt=new BitmapFactory.Options();
        opt.inScaled=false;
        srcBmp = BitmapFactory.decodeResource(rcontext.getResources(), R.drawable.portrait, opt);
    

    这样我总能得到源图像的真实部分,以像素为单位。

    【讨论】:

      猜你喜欢
      • 2021-05-14
      • 1970-01-01
      • 2020-12-29
      • 2012-05-29
      • 1970-01-01
      • 1970-01-01
      • 2016-03-17
      • 2014-03-01
      • 1970-01-01
      相关资源
      最近更新 更多