Android 位图 getPixel 取决于密度?
正确的代码。鉴于
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId);
Drawable drawable = getResources().getDrawable(resId);
我得到的
bitmap.getWidth() == drawable.getIntrinsicWidth() == 9
实际图像是 6x6 像素。关键是密度为 240
,因此比例为 1.5
到 DENSITY_DEFAULT = 160
。这本身并不是一个问题,但 bitmap.getPixel(x, y);
也适用于放大的尺寸。
设置位图的密度似乎没有什么区别。我应该如何查询图像中真实位置的像素?我宁愿不将所有东西除以密度(通常甚至不可能)。
谢谢
Right to the code. Given
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId);
Drawable drawable = getResources().getDrawable(resId);
I get
bitmap.getWidth() == drawable.getIntrinsicWidth() == 9
though the actual image is 6x6
pixels. The point is that the density is 240
, thus the scale is 1.5
to the DENSITY_DEFAULT = 160
. That would not be such an issue itself, but bitmap.getPixel(x, y);
does also work with the enlarged dimensions.
Settings bitmap's density seems to make no difference. How should I query the image for pixel at real position? I would rather not divide everything by the density (ofttimes not even possible).
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先对这里发生的事情发表一些评论。你得到的Bitmap实际上是重新采样的;它的像素数据适用于 9x9 图像。
bitmap.getWidth()
方法不受调用bitmap.setDensity()
的影响。要加载位图,同时防止 Android 框架为您重新采样,请创建一个 BitmapFactory.Options 实例并设置 inScaled 为
false
,然后将其传递给decodeResource():请注意,它是 UI 中使用的重新采样版本(除非您明确解决该问题) ,因此如果您想对 UI 中绘制的位图执行
bitmap.getPixel(x,y);
操作,则不应使用inScaled = false
。First a few comments on what's happening here. The Bitmap you get is actually resampled; its pixel data is for a 9x9 image. The method
bitmap.getWidth()
is not affected by calls tobitmap.setDensity()
.To load the Bitmap while preventing the Android framework from resampling it for you, create a BitmapFactory.Options instance and set inScaled to
false
, then pass this to decodeResource():Note that it is the resampled version that is used in the UI (unless you explicitly work around that), so if you want to do
bitmap.getPixel(x,y);
on the Bitmap that is drawn in the UI, you should not make use ofinScaled = false
.