我在使用包含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.
更多推荐
发布评论