2013-12-13 52 views
0

我試圖加載存儲在文件系統中的圖像到imageButton中。但它不能正確縮放。該文件在可繪製資源和文件系統中是相同的(t700.png),但結果是不同的。setImageDrawable不能正確縮放,沒有資源圖片

這項工作很好:

button.setImageDrawable(getResources().getDrawable(R.drawable.t700)); 

這種變化的規模/尺寸並獲得更大的圖像:

String filePath = VideoActivity.this.getFilesDir()+File.separator+EPGApiService.LOGOS_DIR+File.separator+channel.getLogoFile(); 
Bitmap bitmap = BitmapFactory.decodeFile(filePath); 
BitmapDrawable imgDrawable = new BitmapDrawable(this.getResources(),bitmap); 
button.setImageDrawable(imgDrawable); 

我需要相同的結果..

注: 如果我這樣做

BitmapDrawable logoBitmapDrawableFromDisk = new BitmapDrawable(VideoActivity.this.getResources(),logoBitmap); 
BitmapDrawable logoBitmapDrawableFromDisk2 = new BitmapDrawable(logoBitmap); 
BitmapDrawable logoBitmapDrawableFromRes = (BitmapDrawable) getResources().getDrawable(R.drawable.t700); 

Drawables位圖大小不一樣!

logoBitmapDrawableFromDisk尺寸爲410x123

logoBitmapDrawableFromDisk2尺寸爲205x62

logoBitmapDrawableFromRes尺寸爲820x246

真正的文件是一樣的。我需要這兩種方式的相同大小。

+0

你使用的是什麼版本的android? 'R.drawable.t700'是一個文件或者是另一個可繪製資源的別名? – ramaral

+0

它是指res /可繪製 –

+0

中的t700.png圖像文件,位於文檔:BitmapDrawable(Resources res) 此構造函數在API級別18中已棄用。使用BitmapDrawable(android.content.res.Resources,android.graphics.Bitmap)指定一個位圖來繪製。 –

回答

0

BitmapDrawable(getResources(), bitmap)

從位圖創建可繪製,基於所述資源的顯示的指標設定初始目標密度。

它看起來更大,因爲它使用資源對象將圖像調整爲屏幕密度。

爲了避免使用BitmapDrawable(bitmap)

或調整您的PNG看的權利在MDPI設備。
然後BitmapDrawable(getResources(), bitmap)會給你所有設備的正確外觀。

在所有設備中具有相同外觀的正確方法是爲每個設備密度創建不同的圖標。
從mdpi密度的維度開始。對於每個其他密度尺寸應當基於計算在下列值:

HDPI乘以1.5
由2
xhdpi乘以3

每個圖標xxhdpi相乘應根據與單獨的文件夾放適當的名稱取決於密度:

MDPI - > RES/drawable_mdpi
HDPI - > RES/drawable_hdpi
xhdpi - > RES/drawable_xhdpi
xxhdpi - > res/drawable_xxhdpi

將ImageButton源設置爲xml或使用getResources().getDrawable(R.drawable.t700);設備將自動選擇正確的一個。

欲瞭解更多信息,請參閱this

+0

不要使用BitmapDrawable(位圖)。標誌看起來更小和不同的大小。我需要將圖像調整爲固定的圖像。但只能使用button.setImageDrawable(getResources()。getDrawable(R.drawable.t700)); –

+0

只能手動設置密度:logoBitmap.setDensity(200); –

+0

這是t700.png的真實寬度和高度嗎? – ramaral