【问题标题】:Android bitmap color confusionAndroid 位图颜色混淆
【发布时间】:2017-09-19 21:15:01
【问题描述】:

我的 drawable-xhdpi 文件夹中有我的 png-image,其大小为 32x32 像素:

当我创建图像时,我使用了两种颜色。

当我将图像加载为位图时,循环遍历它并在 logcat 中打印每个颜色值,我会看到两个以上不同的颜色值:

renderSystem.addSprite(R.drawable.walltest, 0, 2);

在渲染系统中:

public void addSprite(int id, int x, int y) {
    Bitmap image = BitmapFactory.decodeResource(context.getResources(), id);
    printColors(image);
    //active.add(new Sprite(image, worldToScreenX(x), worldToScreenY(y)));
}


private void printColors(Bitmap src) {
    for (int i = 0; i < src.getWidth(); i++) {
        for (int j = 0; j < src.getHeight(); j ++) {
            Log.d("Color", String.valueOf(src.getPixel(i,j)));
        }
    }
}

输出:

09-19 13:03:38.832 2576-2576/com.example.benjamin.dungeondweller I/chatty: uid=10082(u0_a82) com.example.benjamin.dungeondweller identical 36 lines
09-19 13:03:38.832 2576-2576/com.example.benjamin.dungeondweller D/Color: -7975603
09-19 13:03:38.832 2576-2576/com.example.benjamin.dungeondweller D/Color: -10734540
09-19 13:03:38.832 2576-2576/com.example.benjamin.dungeondweller D/Color: -16777216
09-19 13:03:38.832 2576-2576/com.example.benjamin.dungeondweller D/Color: -16777216
09-19 13:03:38.832 2576-2576/com.example.benjamin.dungeondweller D/Color: -11325392
09-19 13:03:38.832 2576-2576/com.example.benjamin.dungeondweller D/Color: -7975603
09-19 13:03:38.832 2576-2576/com.example.benjamin.dungeondweller I/chatty: uid=10082(u0_a82) com.example.benjamin.dungeondweller identical 8 lines
09-19 13:03:38.832 2576-2576/com.example.benjamin.dungeondweller D/Color: -7975603
09-19 13:03:38.851 2576-2576/com.example.benjamin.dungeondweller D/Color: -10734540
09-19 13:03:38.857 2576-2576/com.example.benjamin.dungeondweller I/chatty: uid=10082(u0_a82) com.example.benjamin.dungeondweller identical 9 lines
09-19 13:03:38.857 2576-2576/com.example.benjamin.dungeondweller D/Color: -10734540
09-19 13:03:38.857 2576-2576/com.example.benjamin.dungeondweller D/Color: -12639196
09-19 13:03:38.857 2576-2576/com.example.benjamin.dungeondweller D/Color: -16777216
09-19 13:03:38.860 2576-2576/com.example.benjamin.dungeondweller I/chatty: uid=10082(u0_a82) com.example.benjamin.dungeondweller identical 41 lines
09-19 13:03:38.860 2576-2576/com.example.benjamin.dungeondweller D/Color: -16777216

这告诉我位图图像与源 png 图像的颜色不同。这是为什么?以及如何加载位图格式的精确副本?

【问题讨论】:

    标签: android bitmap


    【解决方案1】:

    这是因为您的位图在创建时被过滤了。检查像素的各个 RGB 值,看看那里的细微差别。

    【讨论】:

    • 如何禁用过滤?
    • 没关系,我创建了一个 bitmapfactory 选项并将 inscaled 设置为 true。现在可以了。
    猜你喜欢
    • 2019-01-13
    • 2013-06-07
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 2016-03-27
    相关资源
    最近更新 更多