首页 文章

Android - 将位图从SDCard转换为Drawable

提问于
浏览
1

当我将位图从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

这是一种可以接受的方式吗?

1 回答

  • 0

    是的,你应该将 Resources 传递给 BitmapDrawable 构造函数,这样就不会搞砸了 .

相关问题