Load image without autoscaling in Android

Deadly 提交于 2020-01-04 13:12:03

问题


I'm trying to create a map for a game through an image, where each black pixel is equivalent to a wall, and yellow to flowers(1) and green grass(0) so far i had this image (50x50): http://i.imgur.com/Ydj9Cp2.png

the problem here seems to be that, when i read the image on my code, it get's scaled up to 100x100, even tough i have it on the raw folder. I can't let it scale up or down because that will put noise and blur on the image and then the map won't be readable.

here i have my code:

(...)
Bitmap tab=BitmapFactory.decodeResource(resources, com.example.lolitos2.R.raw.mappixel);
    //tab=Bitmap.createScaledBitmap(tab, 50, 50, false);
    Log.e("w", tab.getWidth()+"."+tab.getHeight());
    for (int i = 0; i < tab.getWidth(); i++) {
        for (int j = 0; j < tab.getHeight(); j++) {
            int x = j;
            int y = i;
            switch (tab.getPixel(x, y)) {
            // se o é uma parede
            case Color.BLACK:
                getParedes()[x][y] = new Parede(x, y);
                break;
            case Color.GREEN:
                fundo.add(new Passivo(x,y,0));
                break;
            default:
                fundo.add(new Passivo(x,y,1));
            }

        }
    }

How can i read my image Map without rescaling it?


回答1:


Put them in the drawable-nodpi folder, instead of in raw, then just read them as any other BitmapDrawable.

If you have some drawable resources that the system should never scale (perhaps because you perform some adjustments to the image yourself at runtime), you should place them in a directory with the nodpi configuration qualifier. Resources with this qualifier are considered density-agnostic and the system will not scale them.

(From http://developer.android.com/guide/practices/screens_support.html)

(If necessary, there are other ways, e.g. you can provide an inDensity parameter to the options of BitmapFactory.decodeResource).




回答2:


Problem is at following line

tab=Bitmap.createScaledBitmap(tab, 50, 50, false);

Change those 50s to larger number.

Hit like if I helped you Thanks



来源:https://stackoverflow.com/questions/24006461/load-image-without-autoscaling-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!