开发者

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

开发者 https://www.devze.com 2023-01-20 19:11 出处:网络
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

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.

The problem is: loading this mask with BitmapFactory results in a Bitmap object that is scaled. This way the colors get distorted a bit. If I have e.g. a solid rectangle with color (155, 155, 0), then it'll be like (148, 158, 0), (150, 154, 0), and so on. But I need to get the exact color.

So how do I 开发者_如何学Goload the raw bitmap, without any scaling / compressing / stuff like that?


I did something similar, with a png file, which was stored in R.raw. The user clicked on an image, triggering the onTouch event of the colored image which was behind it.

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 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消