2015-04-03 36 views
1

我試圖在基於LibGdx的Android遊戲中顯示彈出屏幕。在這樣做的時候,我不得不在顯示彈出窗口時將灰色塗抹到背景屏幕上。在顯示彈出窗口時將灰色應用於非活動屏幕

截至目前爲了實現這一目標,我創建了一個具有黑色背景和alpha 70%的紋理。該紋理繪製在主背景的頂部,因此可以實現。

但是上面的方法需要一個與主背景相同的大尺寸紋理。它也增加了App的重量和渲染方法。

請幫助我一個合適的方法。

我的要求示例圖片如下所示。這是我想在LibGdx沒有具體到Android

enter image description here

+0

你使用的是scene2d嗎? – Khopa 2015-04-03 14:51:14

回答

2

你的紋理可以是你伸展到一個1x1的白色圖像填滿屏幕。 (白色,以便更靈活。)如果你不想計算如何拉伸它相對於相機在哪裏愚弄,你可以這樣做:

private static final Matrix4 IDT = new Matrix4(); 

batch.setProjectionMatrix(camera.combined); 
batch.begin(); 
//draw stuff in background 
batch.setProjectionMatrix(IDT); 
batch.setColor(0, 0, 0, 0.7f); 
batch.draw(singlePixelTexture, -1, -1, 2, 2); //coordinates and size of the screen when identity projection matrix is used 
batch.setProjectionMatrix(camera.combined); 
//draw your front window 
batch.end(); 

但是,您也可以離開孤獨,而是投影矩陣計算在哪裏以及如何大背景應該是,以填補現有的矩陣屏幕:

float camX = camera.position.x; 
float camY = camera.position.y; 
float camWidth = camera.viewportWidth; 
float camHeight = camera.viewportHeight; 

batch.setProjectionMatrix(camera.combined); 
batch.begin(); 
//draw stuff in background 
batch.setColor(0, 0, 0, 0.7f); 
batch.draw(singlePixelTexture, camX - camWidth/2, camY - camHeight/2, camWidth, camHeight); 
//draw your front window 
batch.end(); 

,僅供參考,雖然有大量的紋理確實使用更多的內存和加載時間,它不會放慢除非它被縮小而沒有mip映射。


編輯:如何在運行時生成單個像素質地:

singlePixelPixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888); 
singlePixelPixmap.setColor(1, 1, 1, 1); 
singlePixelPixmap.fill(); 
PixmapTextureData textureData = new PixmapTextureData(singlePixelPixmap, Pixmap.Format.RGBA8888, false, false, true); 
singlePixelTexture = new Texture(textureData); 
singlePixelTexture.setFilter(TextureFilter.Nearest, TextureFilter.Nearest); 

注意,像素圖必須設置在dispose方法,但是你不想早於做的,或者如果存在GL上下文丟失,紋理不能自動重新加載。

+0

我們可以在運行時生成灰色像素,並在上面的示例中使用它。如果是這樣怎麼辦? – iappmaker 2015-04-03 13:15:08

+0

是的,請參閱編輯。 – Tenfour04 2015-04-03 13:34:07

+0

我已經更新了像這個singlePixelPixmap.setColor(0,0,0,100);你能否讓我知道#333333的顏色設置?任何步驟/指針 – iappmaker 2015-04-03 17:15:39

0

你可以設置背景半透明的黑色一樣的效果,#5000

+0

只需設置背景顏色不會產生所需的效果。 – CodeWarrior 2015-04-03 11:26:48