ListView和位图内的ImageView未完全显示(ImageView inside ListView and bitmap not fully displayed)

编程入门 行业动态 更新时间:2024-10-12 05:51:29
ListView和位图内的ImageView未完全显示(ImageView inside ListView and bitmap not fully displayed)

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

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

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

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

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只在我的drawable文件夹中,其大小为60x60像素

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

I'm having issues with a ListView containing an ImageView, it works fine on tablets but not on my phone...

Here is the expected output (on my tablet):

And here is the actual output (on my phone):

I tried using a drawable, it works well on my phone. Here is how I create the Bitmap and bind it to the view :

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

My row layout :

<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>

I really don't understand why it does this... I'm quite new to android but this is really weird

EDIT : the blason is only in my drawable folder, and its size is 60x60 pixels

EDIT2 : I'm using this Bitmap.createBitmap(srcBmp,0,0,60,60); as I need only this portion of the image (the whole head should be shown). Here is a part of portrait.png :

最满意答案

Bitmap.createBitmap将使用位图的来源,并将为您提供一个切口。 正如您在结果中看到的那样。

请尝试使用: createScaled位图 。

编辑:如果您什么都不做,imageView将为您扫描。 所以最好的解决方案就是删除新的位图行:

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

并在xml中添加缩放模式,如@blipinsk建议。

android:scaleType="fitXY"

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

编辑3: BitmapFactory.decodeResource将在设备密度中进行解码,除非您传递一个选项设置,即使用真实大小。 所以这可能是你想要做的,因为你正在使用像素。

Bitmap.createBitmap will use the source of the bitmap and will give you a cut of that. Just what you see in your results.

Try using: createScaled bitmap instead.

Edit: If you do nothing, the imageView will scalle it for you. So best solution will be just removing the new bitmap line:

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

And add the scale mode in the xml like @blipinsk suggested.

android:scaleType="fitXY"

Edit 2: Try changing the LinearLayout to WrapContent, the parent might be smaller than 60px

Edit 3: BitmapFactory.decodeResource will decode in the device density unless you pass an options setting that says, use the true size. So this is probably what you want to do, since you are working with pixels.

更多推荐

本文发布于:2023-07-30 14:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1338744.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位图   ImageView   ListView   displayed   fully

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!