我是 Android 编程的初学者,但我花了很多时间来确定这一点,所以希望有人知道发生了什么。我的应用程序有一堆 Thing 对象,每个对象都有一个位图,每个对象都会以某种方式更改位图(更改颜色、大小等)。这一切都适用于我在 MS Paint 中制作的一些“测试”位图(保存为 .png 文件)。然而,当我用 GIMP 制作的更好的图片替换它们时,应用程序开始崩溃。
最终我发现只有在 .png 图像被保存并在 GIMP 中选中“保存背景颜色”时,该应用程序才会崩溃。我需要这个,因为其中一些图片需要部分透明,以便可以叠加。然而:疯狂的是,当我尝试覆盖或创建图像时它不会崩溃,当我将 ColorMatrixColorFilter 应用于 Paint 对象时它会崩溃。
我不明白这怎么可能。将 Paint 对象实际应用到任何东西都远远不够,那么为什么一个图像文件可以工作,而另一个不能呢?崩溃是 ActivityThread.performLauncActivity 中的 RuntimeException,我无法找到更多关于它的信息。
这是我正在做什么以及它在哪里崩溃的一些伪代码。
问题:1)为什么会崩溃?2)如果我想加载透明图像并应用颜色过滤器,将它们相互叠加以创建新的位图等,我应该做些什么不同的事情?只要我的所有图像都没有背景,所有这些都可以正常工作。
public class ThingHolder {
Thing[] things;
public ThingHolder() {
things = new Thing[3];
things[0] = new Thing(); // The first one works fine
things[1] = new Thing(); // The second fails, see below
...
// Note: It never gets far enough to draw the bitmap
// to a Canvas, that occurs much later
...
canvas.drawBitmap(things[0].bmp, null, rect, things[0].paint);
canvas.drawBitmap(things[1].bmp, null, rect, things[1].paint);
}
}
public class Thing {
Bitmap bmp;
Paint paint;
public Thing() {
bmp = getBmp(1); // this always returns resource file circle.png
paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(redMatrix()));
// RuntimeException occurs here if circle.png has a background color
// But it works fine if circle.png was saved in MS Paint, or in
// Gimp without the background color option checked
}
private float[] redMatrix() {
float[] matrix = {
1, 0, 0, 0, 0, //red
0, 0, 0, 0, 0, //green
0, 0, 0, 0, 0, //blue
0, 0, 0, 1, 0 //alpha
};
return matrix;
}
}
编辑1:这是跟踪,以防有帮助:DalvikVM[localhost:8611]
Thread [<1> main] (Suspended (exception RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) 行:2663
ActivityThread.handleLaunchActivity(ActivityThread $ActivityRecord, Intent) line: 2679
ActivityThread.access$2300(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 125 ActivityThread$H.handleMessage(Message) line: 2033
ActivityThread$H(Handler).dispatchMessage(Message) line: 99 Looper .loop() line: 123 ActivityThread.main(String[]) line: 4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: 不可用 [native method]
Method.invoke(Object, Object...) 行:521
ZygoteInit$MethodAndArgsCaller.run() 行:868
ZygoteInit.main(String[]) 行:626 NativeStart.main(String[]) 行:不可用 [本机方法]
编辑 2:这是使用 2.2 目标(特别是在 VirtualBox 中运行的 android-x86-2.2-eeepc)发生的。当针对 3.2 目标运行时,其他地方的位图处理似乎仍然存在一些问题,但它不再抛出 RuntimeException。所以看起来这可能只是一个错误。