2013-07-23 71 views
1
到繪製

嘿有一些奇怪的顯示出來,當我將位圖轉換從SD卡的可繪製至於調整:Android的 - 位圖轉換從SD卡

String sdDir = Environment.getExternalStorageDirectory().getPath(); 
String filename = "test.png"; //420px X 420px 
Drawable tmpDraw; 
Bitmap tmpBit = BitmapFactory.decodeFile(sdDir+filename); 

Log.e("BAH","Bitmap height:"+tmpBit.getHeight()); //420 

tmpDraw = (Drawable) new BitmapDrawable(getResources(),tmpBit); 
int height = tmpDraw.getMinimumHeight(); 

Log.e("BAH","Drawable height:"+height); //420 

我假設我需要一個參照點從縮放?如果從SDCard讀取一個文件,它被歸類爲MDPI,這就是它被重新調整的原因?我目前正在使用Nexus 7來測試哪種TVDPI。我希望縮放發生,就好像位圖是在HDPI中一樣。

任何建議將不勝感激!

想我已經破解了:

String sdDir = Environment.getExternalStorageDirectory().getPath(); 
String filename = "test.png"; //420px X 420px 
Drawable tmpDraw; 

BitmapFactory.Options options = new BitmapFactory.Options(); 
DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics(); 
options.inDensity = 240; //Being DPI for HDPI 
options.inTargetDensity=metrics.densityDpi; //Being current DPI 
//inDensity/inTargetDensity are a ratio 

Bitmap tmpBit = BitmapFactory.decodeFile(sdDir+filename,options); 

Log.e("BAH","Bitmap height:"+tmpBit.getHeight()); //373 Correct for TVDPI 

tmpDraw = (Drawable) new BitmapDrawable(getResources(),tmpBit); 
int height = tmpDraw.getMinimumHeight(); 

Log.e("BAH","Drawable height:"+height); //373 Correct for TVDPI 

這是做一個可以接受的方式?

+0

你想要什麼?你想防止縮放,並有高度420而不是315? –

+0

我希望縮放發生,就好像SD卡上的位圖是在HDPI中一樣。在位圖或可繪製級別。 – delaji

回答

0

是的,你應該通過一個ResourcesBitmapDrawable構造函數,所以它不會搞砸了。

+0

半答覆。 tmpDraw =(Drawable)new BitmapDrawable(getResources(),tmpBit);不會產生從位圖到可繪製的重新縮放。假設SDCard採用HDPI格式,我怎樣才能獲得初始位圖? – delaji

+0

由於您確實指出了在位圖和Drawable之間進行縮放的問題,因此會爲您提供答案。謝謝! – delaji