嘿,当我将位图从 SDCard 转换为 Drawable 以调整大小时,出现了一些奇怪的现象:
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
这是一种可接受的方式吗?