我有一个 if-else 场景,其中 if-path 使用以下代码:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inPurgeable = true;
options.inInputShareable = true;
bitmap = BitmapFactory.decodeResource(getResources(), resourceId, options);
else-path 使用以下代码:
InputStream is = getContentResolver().openInputStream(Uri.fromFile(file));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inPurgeable = true;
options.inInputShareable = true;
bitmap = BitmapFactory.decodeStream(is, null, options);
现在对我来说,阅读 API 的简短描述、测试和谷歌搜索,我无法完全弄清楚其中的区别,但似乎有一个微妙的区别。
第一段代码在应用于 ImageView(在relativelayout 中、scrollview 中)时会缩放以适应屏幕的宽度。第二段代码没有,这是我的问题。它似乎也以某种方式失去了它的纵横比,因为如果我申请:
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
它设法使其适合宽度,但弄乱了高度(使其比应有的短)。任何投入将不胜感激!
(我已经尝试更改第一段代码以查看是否弄乱了其他内容,但是 if-path 的重写给出了相同的错误:)
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inPurgeable = true;
options.inInputShareable = true;
bitmap= BitmapFactory.decodeStream(getResources().openRawResource(resourceId), null, options);