android - 在 Android 中加载不自动缩放的图像

标签 android image resize bufferedimage

我正在尝试通过图像为游戏创建 map ,其中每个黑色像素相当于一堵墙,黄色相当于鲜花 (1) 和绿草 (0) 到目前为止我有这张图片(50x50): http://i.imgur.com/Ydj9Cp2.png

这里的问题似乎是,当我在我的代码中读取图像时,它被放大到 100x100,即使我将它放在原始文件夹中也是如此。我不能让它按比例放大或缩小,因为这会给图像带来噪点和模糊,然后 map 将无法读取。

这里有我的代码:

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

        }
    }

如何在不重新调整图像 map 的情况下读取它?

最佳答案

将它们放在 drawable-nodpi 文件夹中,而不是放在 raw 中,然后像读取任何其他 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.

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

(如有必要,还有其他方法,例如,您可以为 BitmapFactory.decodeResource 的选项提供一个 inDensity 参数)。

关于android - 在 Android 中加载不自动缩放的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24006461/

相关文章:

jquery - 部分可扩展站点

android - 使用 FFmpeg 裁剪视频会过度增加 tbn 值

android - 我应该在哪里为 Main Activity 实现 setOnTouchListener

php - 使用 Google 云消息传递的图像推送通知

javascript - 如何在上传到服务器之前保存 Canvas 图像?

javascript - 图像尺寸奇怪

c++ - SDL_VIDEORESIZE 不起作用

android - 如何以编程方式关闭 Android Speech To Text 对话框

android - 如何在 MIUI 设备中打开管理应用程序电池使用设置

python - 在Python中缩小图像的一部分