当前位置:操作系统 > 安卓/Android >>

android游戏开发框架libgdx的使用(五)--舞台和常用UI类

常用的UI类包括标签,按钮,勾选框,下拉框,图片,输入框,列表,滑动面板,滑条,分割面板。它们都在com.badlogic.gdx.scenes.scene2d.ui包中,都属于Actor,可以方便的纳入舞台的管理中。

其实仔细看看UI类的实现代码不难发现其实它们都是大部分继承自Widget或者Table,如果需要自定义UI可以继承以上两个类(它们继承自Actor),这里要说明一下libgdx的布局部分使用了TWL,有兴趣的朋友可以去看看。

在介绍每个控件之前我们先来看一下NinePatch,这是最近的一个比较重大的更新。

何为NinePatch?其实android原生即有NinePatch类,常在按钮中使用。

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(); 

 

        float rightColumnX = x + width;  

        if (patches[BOTTOM_RIGHT] != null)  

            rightColumnX -= patches[BOTTOM_RIGHT].getRegionWidth();  

        else if (patches[MIDDLE_RIGHT] != null)  

            rightColumnX += patches[MIDDLE_RIGHT].getRegionWidth();  

        else if (patches[TOP_RIGHT] != null) //  

            rightColumnX += patches[TOP_RIGHT].getRegionWidth(); 

 

        float middleRowY = y;  

        if (patches[TOP_LEFT] != null)  

            middleRowY += patches[TOP_LEFT].getRegionHeight();  

        else if (patches[TOP_CENTER] != null)  

            middleRowY += patches[TOP_CENTER].getRegionHeight();  

        else if (patches[TOP_RIGHT] != null) //  

            middleRowY += patches[TOP_RIGHT].getRegionHeight(); 

 

        float topRowY = y + height;  

        if (patches[TOP_LEFT] != null)  

            topRowY -= patches[TOP_LEFT].getRegionHeight();  

        else if (patches[TOP_CENTER] != null)  

            topRowY -= patches[TOP_CENTER].getRegionHeight();  

        else if (patches[TOP_RIGHT] != null) //  

            topRowY -= patches[TOP_RIGHT].getRegionHeight(); 

 

        // Bottom row  

        if (patches[BOTTOM_LEFT] != null) batch.draw(patches[BOTTOM_LEFT], x, y, centerColumnX - x, middleRowY - y);  

        if (patches[BOTTOM_CENTER] != null)  

            batch.draw(patches[BOTTOM_CENTER], centerColumnX, y, rightColumnX - centerColumnX, middleRowY - y);  

        if (patches[BOTTOM_RIGHT] != null)  

            batch.draw(patches[BOTTOM_RIGHT], rightColumnX, y, x + width - rightColumnX, middleRowY - y); 

 

        // Middle row  

        if (patches[MIDDLE_LEFT] != null) batch.draw(patches[MIDDLE_LEFT], x, middleRowY, centerColumnX - x, topRowY - middleRowY);  

        if (patches[MIDDLE_CENTER] != null) &

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,