AndEngine 合并精灵
我已经用 AndEngine 驱动的动态壁纸解决严重的性能问题有一段时间了(我不久前发布了另一个关于它的问题,在我知道是什么原因导致它之前),似乎唯一的方法让它正常工作的方法是将几个精灵合并在一起。
我有六个左右相当大的精灵,大小都相同,直接层叠在彼此之上。我这样做,所以它看起来就像一张图像,但只要我愿意,它的每个部分都可以通过代码设置为不同的颜色。由于某种原因,在使用完全利用 Alpha 的混合模式时分层精灵(即使图像中没有任何 Alpha)会对帧速率造成一些重大损害,而且我找不到任何简单的解决方法这。
我找到的解决方案是,我需要单独对精灵进行所需的操作,然后将它们合并为一个。我花了相当多的时间深入研究 AndEngine 的各种类,试图不破坏这个过程中的任何东西,但我就是不知道该怎么做。
我确信将一个精灵放到另一个精灵上并不是那么困难,或者以某种方式将重新制作的精灵放回临时纹理图集中,我只是不太明白如何获得它去。
编辑::
public void onLoadResources(){
this.mTestAtlas = new BitmapTextureAtlas(512, 1024, TextureOptions.NEAREST_PREMULTIPLYALPHA);
Bitmap b = BitmapFactory.decodeStream(layer1);
Bitmap a = overlay(b);
test = new BitmapTextureAtlasSource(a);
this.mTesting = BitmapTextureAtlasTextureRegionFactory.createFromSource(this.mTestAtlas, test, 0, 0);
this.getEngine().getTextureManager().loadTexture(this.mTestAtlas);
}
public Scene onLoadScene(){
layer1Sprite = new Sprite(x, y, width, height, this.mTesting);
scene.attachChild(layer1Sprite);
}
public static Bitmap overlay(Bitmap bmp1) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
Paint layer1Paint = new Paint();
ColorFilter layer1Filter;
layer1Filter = new LightingColorFilter(Color.rgb(175, 0, 175), 1);
layer1Paint.setColorFilter(layer1Filter);
canvas.drawBitmap(bmp4, 0, 0, layer1Paint);
return bmOverlay;
}
I have been tackling a severe performance issue with an AndEngine powered live wallpaper for a while now (I've posted another question about it a while back, before I knew what was causing it at all), and it seems that the only way to get this to work properly is to merge a few sprites together.
I have a half dozen or so fairly large sprites, all the same size, that get layered directly on top of each other. I do it this way so it all looks like one image, but each part of it can be set to a different color via code whenever I'd like. For some reason or other, layering sprites when using a blending mode that utilizes alpha at all (Even if there wasn't any alpha in the images) does some major damage to your frame rate, and I can't find any simple way around this.
The solution I've arrived at is that I will need to do what I need to with the sprites individually, and then merge them together into one. I've spent quite a few hours diving into various classes of AndEngine, trying not to destroy anything in the process, but I just cannot figure out how to do it.
I'm certain that it cannot be that difficult to slap one sprite onto another, or putting the re-worked sprites back into a temporary texture atlas somehow, I just can't quite grasp how to get it going.
EDIT ::
public void onLoadResources(){
this.mTestAtlas = new BitmapTextureAtlas(512, 1024, TextureOptions.NEAREST_PREMULTIPLYALPHA);
Bitmap b = BitmapFactory.decodeStream(layer1);
Bitmap a = overlay(b);
test = new BitmapTextureAtlasSource(a);
this.mTesting = BitmapTextureAtlasTextureRegionFactory.createFromSource(this.mTestAtlas, test, 0, 0);
this.getEngine().getTextureManager().loadTexture(this.mTestAtlas);
}
public Scene onLoadScene(){
layer1Sprite = new Sprite(x, y, width, height, this.mTesting);
scene.attachChild(layer1Sprite);
}
public static Bitmap overlay(Bitmap bmp1) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
Paint layer1Paint = new Paint();
ColorFilter layer1Filter;
layer1Filter = new LightingColorFilter(Color.rgb(175, 0, 175), 1);
layer1Paint.setColorFilter(layer1Filter);
canvas.drawBitmap(bmp4, 0, 0, layer1Paint);
return bmOverlay;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Andengine 没有提供任何工具或方法来做到这一点,但幸运的是,原生 Android 提供了。这是一篇文章,介绍如何创建一个输入两个位图但输出一个合并位图的函数。
http://www.jondev.net/articles/Combining_2_Images_in_Android_using_Canvas
这里有一个相关的问题:
Android 合并两个图像
Andengine GLES2 更新:
截至 2011 年 12 月,有一个新版本的Andrengine称为Andengine GLES2。它有一个内置资源,用于将引擎精灵绘制到纹理中。这是发行说明:
以及原始论坛帖子的链接,概述了 GLES2 的新增功能。
http://www.andengine.org/forums/announces/andengine-gles2-pre-release-on-december-23rd-t6097.html" andengine.org/forums/announces/andengine-gles2-pre-release-on-december-23rd-t6097.html
Andengine does not provide any tools or methods to do this, but fortunately, native Android does. Here is an article that shows how to make a function that inputs two bitmaps but outputs one, merged bitmap.
http://www.jondev.net/articles/Combining_2_Images_in_Android_using_Canvas
There is a related question here:
Android merge two images
Update for Andengine GLES2:
AS of December 2011, there is a new version of Andrengine called Andengine GLES2. It has a built in resource for drawing your andengine sprites into a textures. Here is teh release note:
And the link to the original forum post outlining additions to GLES2.
http://www.andengine.org/forums/announces/andengine-gles2-pre-release-on-december-23rd-t6097.html