Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 遊戲 >> Android 遊戲開發

Android遊戲引擎libgdx使用教學5:常用UI類別與舞台

編輯:Android 遊戲開發

像是按鈕、下拉選單、列表、標籤、圖片、核取方塊、編輯框、分割面板、滑動面板、滑動條等,都是比較常用的 UI 類別。它們都屬於 Actor,可以很方便地納入舞台管理,而且都包含在 com.badlogic.gdx.scenes.scene2d.ui 套件中。

仔細觀察 UI 類別的實作會發現,它們大多繼承自 Widget 或 Table。若需要自訂 UI,可以繼承這兩個類別(它們都繼承自 Actor)。這裡要說明一下,libgdx 的版面配置部分使用了 TWL,有興趣的朋友可以去研究看看。

在介紹各個控制項之前,先來看 NinePatch,這是近期一項比較重大的更新。何謂 NinePatch?其實 Android 原生就有 NinePatch 類別,常用於按鈕。

如圖,將圖片分成九份,中間部分可依需要拉伸,讓按鈕的大小隨內容變動而不受圖片尺寸限制。而在 libgdx 中,NinePatch 其實就是九個 TextureRegion 物件。

常用的建構方法有兩個:

public NinePatch (Texture texture, int left, int right, int top, int bottom)
public NinePatch (TextureRegion region, int left, int right, int top, int bottom)

關於其中四個 int 參數如何取值,我們可以參考原始碼:

public NinePatch (TextureRegion region, int left, int right, int top, int bottom) {
    int middleWidth = region.getRegionWidth() - left - right;
    int middleHeight = region.getRegionHeight() - top - bottom;
    this.patches = new TextureRegion[] {
        new TextureRegion(region, 0, 0, left, top),
        new TextureRegion(region, left, 0, middleWidth, top),
        new TextureRegion(region, left + middleWidth, 0, right, top),
        new TextureRegion(region, 0, top, left, middleHeight),
        new TextureRegion(region, left, top, middleWidth, middleHeight),
        new TextureRegion(region, left + middleWidth, top, right, middleHeight),
        new TextureRegion(region, 0, top + middleHeight, left, bottom),
        new TextureRegion(region, left, top + middleHeight, middleWidth, bottom),
        new TextureRegion(region, left + middleWidth, top + middleHeight, right, bottom)
    };
}

先計算中間部分的寬度與高度,然後開始切圖:先取最上方最左邊那塊(圖中編號 1),再取它右邊的,然後再右邊的;取完最上面那列,再取中間那列,最後取最下面那列。由上而下、由左而右。

而在繪製時又是如何處理的呢?看原始碼:

public void draw (SpriteBatch batch, float x, float y, float width, float height) {
    float centerColumnX = x;
    if (patches[BOTTOM_LEFT] != null)
        centerColumnX += patches[BOTTOM_LEFT].getRegionWidth();
    else if (patches[MIDDLE_LEFT] != null)
        centerColumnX += patches[MIDDLE_LEFT].getRegionWidth();
    else if (patches[TOP_LEFT] != null)
        centerColumnX += patches[TOP_LEFT].getRegionWidth();
    // ... 依此計算 rightColumnX、middleRowY、topRowY 後,
    // 再自下而上、由左而右畫出九宮格
}

先計算左右欄的寬度,再計算中間與頂部的高度,然後從下而上繪製。說實話,我覺得這段程式碼讀起來還挺有趣的。

常用控制項

先建立一個舞台。先來試試 Label,Label 是有快取的,所以替換顯示內容不是用 setText,而是使用 setWrappedText 方法。

package com.cnblogs.htynkn.listener;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actors.Label;

public class FirstGame implements ApplicationListener {
    private Stage stage;
    Label label;

    @Override
    public void create() {
        stage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
        label = new Label("fpsLabel",
            new BitmapFont(Gdx.files.internal("cf.fnt"),
                Gdx.files.internal("cf.png"), false), "label1");
        label.x = 5;
        label.y = Gdx.graphics.getHeight() - label.height - 5;
        stage.addActor(label);
        Gdx.input.setInputProcessor(stage);
    }

    @Override
    public void render() {
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        label.setWrappedText("FPS: " + Gdx.graphics.getFramesPerSecond(),
            HAlignment.CENTER);
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
    }
}

效果:

接著看看 Button。建立時需要一個 ButtonStyle,它定義了按鈕三種狀態對應的圖片樣式、按下與放開時的 X、Y 偏移,以及按鈕中文字繪製所需的 BitmapFont 與 Color。這裡我為了省事,三種狀態只用同一張圖片。

texture = new Texture(Gdx.files.internal("06.png"));
NinePatch n1 = new NinePatch(texture, 7, 7, 9, 9);
BitmapFont bitmapFont = new BitmapFont(Gdx.files.internal("cf.fnt"),
    Gdx.files.internal("cf.png"), false);
button = new Button("button", new ButtonStyle(n1, n1, n1,
    0f, 0f, 0f, 0f, bitmapFont, new Color(1, 1, 0, 0.5f)), "button");
button.x = 10;
button.y = 10;
button.width = 100f;
button.height = 32f;
stage.addActor(button);

效果:

按鈕理應要有點擊事件,透過 setClickListener 來設定:

button.setClickListener(new ClickListener() {
    @Override
    public void click(Actor actor) {
        Gdx.app.log("Info", "點擊事件觸發了");
    }
});

再看看 CheckBox。CheckBox 的樣式定義在 CheckBoxStyle 中,需要四個參數:兩種狀態各一張圖片、一個 BitmapFont 和 Color。

CheckBoxStyle style = new CheckBoxStyle(
    new TextureRegion(texture1), new TextureRegion(texture2),
    bitmapFont, new Color(1, 1, 1, 0.5f));
checkBox = new CheckBox("checkbox", style, "checkbox");
checkBox.x = 100;
checkBox.y = 100;
checkBox.width = 158f;
checkBox.height = 32f;
checkBox.setText("Yes");
checkBox.setClickListener(new ClickListener() {
    @Override
    public void click(Actor actor) {
        checkBox.setText(checkBox.isChecked ? "Yes" : "NO");
    }
});
stage.addActor(checkBox);

效果:

其他 UI 的用法大致相同,顯示樣式在對應的 Style 或 Skin 中定義。但要注意有些 UI 類別需要手動設定 width 和 height,否則顯示會很奇怪。

最後說一下 Slider 的用法。SliderStyle 需要一個 NinePatch 和一個 Texture。我最初想不通為什麼不是兩個 NinePatch,仔細看原始碼才了解:NinePatch 是作為背景,而 Texture 那個則是中間滑動的方塊。

關於用設定檔配置 Style 的問題,Google Code 的 wiki 上似乎沒寫,但 libgdx 論壇裡有,例如:

somePatch1: [
    { height: 13, width: 9, x: 761, y: 78 },
    { height: 13, width: 1, x: 770, y: 78 },
    { height: 13, width: 9, x: 771, y: 78 },
    { height: 1, width: 9, x: 761, y: 91 },
    { height: 1, width: 1, x: 770, y: 91 },
    { height: 1, width: 9, x: 771, y: 91 },
    { height: 13, width: 9, x: 761, y: 92 },
    { height: 13, width: 1, x: 770, y: 92 },
    { height: 13, width: 9, x: 771, y: 92 }
]

或者:

somePatch2: [
    { height: 13, width: 9, x: 761, y: 78 },
]
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved