当我将一个Bitmap从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。我希望缩放发生,好像Bitmap是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 :(得分:0)
是的,你应该将Resources
传递给BitmapDrawable
构造函数,这样就不会搞砸了。