2014-07-02 109 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); 

這樣,我總是得到源圖像的實部,在像素方面。