在ImageView中以像素为单位获取图像尺寸 [英] Getting image dimensions in pixels in ImageView

查看:595
本文介绍了在ImageView中以像素为单位获取图像尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

While I try to get image dimensions in pixels in an ImageView, I found that its width is 3 times more than the original jpg file width.

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

I put a jpg file which dimensions are 800 x 600, but the code below displays 2400 as its width.

Bitmap bitmap = ((BitmapDrawable)imgv.getDrawable()).getBitmap();
float fwidth = bitmap.getWidth();
Log.d("width0", Float.toString(fwidth));

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

I checked the jpg file size again but it was not changed (800 x 600), I also searched for a solution but the code above displays the correct dimensions of the bitmap on other user's experience.

我做错了什么?
任何人都可以给我一些建议吗?

What have I done incorrectly? Can anyone give me some advice?

感谢您的帮助。

推荐答案

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

This is the solution found in a web site.

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

I needed to change the Option value when I decode the resource not to scale the original image. I had to use the three parameters for the decodeResource function, not two.

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

Of course, the third parameter was Options specifying the original bitmap not to be scaled. So now I can get 800 when I call bitmap's getWidth() function.

                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();

这篇关于在ImageView中以像素为单位获取图像尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆