简体   繁体   English

如何在不缩放Android的情况下加载原始位图?

[英]How to load a raw bitmap, without scaling on Android?

I have a bitmap file, that serves as a mask for a view. 我有一个位图文件,用作视图的蒙版。 It has multiple rectangular areas with different colors on a white background. 它在白色背景上有多个不同颜色的矩形区域。 When the user touches the View, I use the X and Y coords of this event to look up the color of the mask (which is not displayed) and do things based on the color code returned. 当用户触摸视图时,我使用该事件的X和Y坐标查找蒙版的颜色(未显示),并根据返回的颜色代码进行操作。

The problem is: loading this mask with BitmapFactory results in a Bitmap object that is scaled. 问题是:用BitmapFactory加载此蒙版将导致可缩放的Bitmap对象。 This way the colors get distorted a bit. 这样颜色会有点失真。 If I have eg a solid rectangle with color (155, 155, 0), then it'll be like (148, 158, 0), (150, 154, 0), and so on. 例如,如果我有一个颜色为(155,155,0)的实心矩形,那么它将类似于(148,158,0),(150,154,0),依此类推。 But I need to get the exact color. 但是我需要获得确切的颜色。

So how do I load the raw bitmap, without any scaling / compressing / stuff like that? 那么,如何在不进行任何缩放/压缩/类似操作的情况下加载原始位图?

I did something similar, with a png file, which was stored in R.raw. 我对png文件进行了类似的操作,该文件存储在R.raw中。 The user clicked on an image, triggering the onTouch event of the colored image which was behind it. 用户单击图像,触发其后面的彩色图像的onTouch事件。

public static Bitmap loadBitmapFromView(View v)
{
    Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
    v.draw(c);
    return b;
}

@Override
public boolean onTouch(View v, MotionEvent event)
{
    Bitmap b = loadBitmapFromView(v);
    long color = b.getPixel((int)event.getX(), (int)event.getY());
    //check what the color is, act accordingly 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM