2014-07-02 112 views
1
用含有ImageView的一个ListView

我有问题,它适用于平板电脑,但不能在我的手机很好...ImageView的不完全显示

这里是预期的输出(在我的平板电脑) : enter image description here

这里是实际输出(我的电话): enter image description here

我用绘制试过,效果很好我的手机上。 这里是我创建位图,并将其绑定到视图:

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只在我的绘制文件夹,它的尺寸为60×60像素

EDIT2:我USI这个Bitmap.createBitmap(srcBmp,0,0,60,60);因为我只需要这部分图像(应显示整个头部)。这里是portrait.png的一部分: enter image description here

+0

您正在使用** px **。尝试使用** dp **。它会更好地扩展。 –

+0

如果你在你的可绘制文件夹及其完美尺寸的图像,那么为什么不设置使用'imageView.setImageResource(R.drawable.blason);' –

+0

dp/px在这里看起来不是问题。在我的平板电脑上,显示的是整个头部,而不是电话上,无论我用于ImageView – Lectem

回答

0

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

并添加缩放模式中的XML一样@blipinsk建议。

android:scaleType="fitXY" 

编辑2:尝试改变的LinearLayout到WrapContent,家长可能会小于60像素

编辑3:BitmapFactory.decodeResource将在器件密度进行解码,除非你传递一个选项设置,说,请使用真实尺寸。所以这可能是你想要做的,因为你正在处理像素。

+0

这正是我的目的是削减60x60的第一个像素:/我有一个不同的人物头像spriteshits,我只需要得到其中的一个,这就是为什么我削减它。 scaleType不能解决它,因为我回答blipinsk – Lectem

+0

@Lectem所以结果有什么问题?它看起来切断了我。 –

+0

平板电脑和我的手机之间的差异似乎非常明显,它在平板电脑上正确切割(您可以看到整个头部),而在手机上只能看到其中的一部分 – Lectem

0

添加

android:scaleType="fitXY" 

您的ImageView

+0

我已尝试所有的scaleTypes,没有什么工作... – Lectem

0

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

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

这样,我总是得到源图像的实部,在像素方面。