在ImageView中以像素为单位获取图像尺寸

时间:2016-07-15 07:57:06

标签: android bitmap imageview dimensions

当我尝试在ImageView中获取像素的图像尺寸时,我发现它的宽度是原始jpg文件宽度的3倍。

我放了一个尺寸为800 x 600的jpg文件,但下面的代码显示了2400的宽度。

    var content = document.getElementById("div id");
    var WinPrint = window.open('', '');
    WinPrint.document.write( "<link rel=\"stylesheet\" href=\"style.css\" type=\"text/css\" media=\"print\"/>" );
    WinPrint.document.write(content.innerHTML);
    WinPrint.document.close();
    WinPrint.focus();
    WinPrint.print();
    WinPrint.close();

我再次检查了jpg文件大小,但没有更改(800 x 600), 我还搜索了一个解决方案,但上面的代码显示了其他用户体验中位图的正确尺寸。

我做错了什么? 谁能给我一些建议?

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

这是在网站上找到的解决方案。

我需要在解码资源时更改Option值,而不是缩放原始图像。我不得不使用decodeResource函数的三个参数,而不是两个。

当然,第三个参数是Options,指定不缩放的原始位图。所以现在当我调用bitmap的getWidth()函数时,我可以得到800。

                Resources res = getResources();
                int id = R.drawable.map10;
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inScaled = false;
//                options.inSampleSize = 3;
                Bitmap bb = BitmapFactory.decodeResource(res, id, options);

                float fwidth = bb.getWidth();

答案 1 :(得分:0)

确保您的ImageView设置为: height wrap_content width wrap_content scaleType

相关问题