Java 无法正确缩放表LibGdx 表格; 阶段性; 皮肤; 地图集; 私有位图字体黑色; 正交摄影机; 私有静态最终int虚拟_宽度=1280; 专用静态最终int虚拟_高度=720; 专用静态最终浮点数纵横比=(浮点数)虚拟浮点数宽度/(浮点数)虚拟浮点数高度; 私有矩形视口; @凌驾 公共无效渲染(浮动增量){ Gdx.gl.glClearColor(1,1,1,1); camera.update(); 试一试{ 相机。应用(Gdx.gl10); }捕获(例外e){ } //设置视口 Gdx.gl.glViewport((int)viewport.x,(int)viewport.y, (int)viewport.width,(int)viewport.height); //清除上一帧 Gdx.gl.glClear(GL10.gl\u颜色\u缓冲\u位); 表2.drawDebug(阶段); stage.draw(); } @凌驾 公共空心调整大小(整型宽度、整型高度){ //计算新视口 浮动纵横比=(浮动)宽度/(浮动)高度; 浮标=1f; 矢量2裁剪=新矢量2(0f,0f); if(纵横比>纵横比) { 比例=(浮动)高度/(浮动)虚拟高度; crop.x=(宽度-虚拟宽度*比例)/2f; } 否则如果(纵横比纵横比) { 比例=(浮动)高度/(浮动)虚拟高度; crop.x=(宽度-虚拟宽度*比例)/2f; } 否则如果(纵横比
我希望我用这段代码创建的表能够适合屏幕,而且它在我的Galaxy S3上做得非常好,我添加了Java 无法正确缩放表LibGdx 表格; 阶段性; 皮肤; 地图集; 私有位图字体黑色; 正交摄影机; 私有静态最终int虚拟_宽度=1280; 专用静态最终int虚拟_高度=720; 专用静态最终浮点数纵横比=(浮点数)虚拟浮点数宽度/(浮点数)虚拟浮点数高度; 私有矩形视口; @凌驾 公共无效渲染(浮动增量){ Gdx.gl.glClearColor(1,1,1,1); camera.update(); 试一试{ 相机。应用(Gdx.gl10); }捕获(例外e){ } //设置视口 Gdx.gl.glViewport((int)viewport.x,(int)viewport.y, (int)viewport.width,(int)viewport.height); //清除上一帧 Gdx.gl.glClear(GL10.gl\u颜色\u缓冲\u位); 表2.drawDebug(阶段); stage.draw(); } @凌驾 公共空心调整大小(整型宽度、整型高度){ //计算新视口 浮动纵横比=(浮动)宽度/(浮动)高度; 浮标=1f; 矢量2裁剪=新矢量2(0f,0f); if(纵横比>纵横比) { 比例=(浮动)高度/(浮动)虚拟高度; crop.x=(宽度-虚拟宽度*比例)/2f; } 否则如果(纵横比纵横比) { 比例=(浮动)高度/(浮动)虚拟高度; crop.x=(宽度-虚拟宽度*比例)/2f; } 否则如果(纵横比,java,android,libgdx,Java,Android,Libgdx,我希望我用这段代码创建的表能够适合屏幕,而且它在我的Galaxy S3上做得非常好,我添加了 Table table; Stage stage; Skin skin; TextureAtlas atlas; private BitmapFont black; OrthographicCamera camera; private static final int VIRTUAL_WIDTH = 1280; private static final int VIRTUAL_HEIGHT = 720
Table table;
Stage stage;
Skin skin;
TextureAtlas atlas;
private BitmapFont black;
OrthographicCamera camera;
private static final int VIRTUAL_WIDTH = 1280;
private static final int VIRTUAL_HEIGHT = 720;
private static final float ASPECT_RATIO = (float)VIRTUAL_WIDTH/(float)VIRTUAL_HEIGHT;
private Rectangle viewport;
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1,1,1,1);
camera.update();
try {
camera.apply(Gdx.gl10);
} catch(Exception e) {
}
// set viewport
Gdx.gl.glViewport((int) viewport.x, (int) viewport.y,
(int) viewport.width, (int) viewport.height);
// clear previous frame
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
Table.drawDebug(stage);
stage.draw();
}
@Override
public void resize(int width, int height) {
// calculate new viewport
float aspectRatio = (float)width/(float)height;
float scale = 1f;
Vector2 crop = new Vector2(0f, 0f);
if(aspectRatio > ASPECT_RATIO)
{
scale = (float)height/(float)VIRTUAL_HEIGHT;
crop.x = (width - VIRTUAL_WIDTH*scale)/2f;
}
else if(aspectRatio < ASPECT_RATIO)
{
scale = (float)width/(float)VIRTUAL_WIDTH;
crop.y = (height - VIRTUAL_HEIGHT*scale)/2f;
}
else
{
scale = (float)width/(float)VIRTUAL_WIDTH;
}
float w = (float)VIRTUAL_WIDTH*scale;
float h = (float)VIRTUAL_HEIGHT*scale;
viewport = new Rectangle(crop.x, crop.y, w, h);
}
@Override
public void show() {
stage = new Stage();
camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
black = new BitmapFont(Gdx.files.internal("data/font.fnt"), false);
atlas = new TextureAtlas("data/button.pack");
skin = new Skin(atlas);
table = new Table(skin);
table.setPosition(50, Gdx.graphics.getHeight()-(Gdx.graphics.getHeight()-50));
table.setSize(Gdx.graphics.getWidth()-100, Gdx.graphics.getHeight()-100);
LabelStyle style = new LabelStyle();
style.font = black;
table.align(Align.top);
Label label = new Label("ORTHO", style);
label.setWrap(true);
label.setAlignment(Align.left);
Label label1 = new Label("A", style);
label1.setWrap(true);
label1.setAlignment(Align.center);
Label label2 = new Label("B", style);
label2.setWrap(true);
label2.setAlignment(Align.center);
Label label3 = new Label("C", style);
label3.setWrap(true);
label3.setAlignment(Align.center);
table.add(label).width(Gdx.graphics.getWidth()-100).colspan(3).align(Align.left).padBottom(50);
table.row();
table.add(label1).width((Gdx.graphics.getWidth()-100)/3).height((Gdx.graphics.getWidth()-100)/3);
table.add(label2).width((Gdx.graphics.getWidth()-100)/3).height((Gdx.graphics.getWidth()-100)/3);
table.add(label3).width((Gdx.graphics.getWidth()-100)/3).height((Gdx.graphics.getWidth()-100)/3);
table.row();
table.add(label1).width((Gdx.graphics.getWidth()-100)/3).height((Gdx.graphics.getWidth()-100)/3);
table.add(label2).width((Gdx.graphics.getWidth()-100)/3).height((Gdx.graphics.getWidth()-100)/3);
table.add(label3).width((Gdx.graphics.getWidth()-100)/3).height((Gdx.graphics.getWidth()-100)/3);
table.row();
table.add(label1).width((Gdx.graphics.getWidth()-100)/3).height((Gdx.graphics.getWidth()-100)/3);
table.add(label2).width((Gdx.graphics.getWidth()-100)/3).height((Gdx.graphics.getWidth()-100)/3);
table.add(label3).width((Gdx.graphics.getWidth()-100)/3).height((Gdx.graphics.getWidth()-100)/3);
table.debug();
stage.addActor(table);
TextButtonStyle textButtonStyle = new TextButtonStyle();
textButtonStyle.up = skin.getDrawable("button.up");
textButtonStyle.down = skin.getDrawable("button.down");
textButtonStyle.pressedOffsetX = 1;
textButtonStyle.pressedOffsetY = -1;
textButtonStyle.font = black;
textButtonStyle.font.setScale(3);
TextButton button = new TextButton("game", textButtonStyle);
button.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("hello");
return false;
}
});
}
私有矩形视口;
....
....
Gdx.gl.glClearColor(1,1,1,1);
camera.update();
试一试{
相机。应用(Gdx.gl10);
}捕获(例外e){
}
....
....
//设置视口
Gdx.gl.glViewport((int)viewport.x,(int)viewport.y,
(int)viewport.width,(int)viewport.height);
....
....
浮动纵横比=(浮动)宽度/(浮动)高度;
浮标=1f;
矢量2裁剪=新矢量2(0f,0f);
if(纵横比>纵横比)
{
比例=(浮动)高度/(浮动)虚拟高度;
crop.x=(宽度-虚拟宽度*比例)/2f;
}
否则如果(纵横比<纵横比)
{
比例=(浮动)宽度/(浮动)虚拟宽度;
y=(高度-虚拟高度*比例)/2f;
}
其他的
{
比例=(浮动)宽度/(浮动)虚拟宽度;
}
float w=(float)虚拟_宽度*比例;
浮动h=(浮动)虚拟高度*刻度;
视口=新矩形(裁剪x、裁剪y、w、h);
....
....
摄影机=新正交摄影机(虚拟_宽度、虚拟_高度);
为了将它适当地扩展到其他设备,比如我正在运行的480-320模拟器,但它一点也没有,它可以很好地扩展它,但会将表的位置降低到应该的位置
private Rectangle viewport;
....
....
Gdx.gl.glClearColor(1,1,1,1);
camera.update();
try {
camera.apply(Gdx.gl10);
} catch(Exception e) {
}
....
....
// set viewport
Gdx.gl.glViewport((int) viewport.x, (int) viewport.y,
(int) viewport.width, (int) viewport.height);
....
....
float aspectRatio = (float)width/(float)height;
float scale = 1f;
Vector2 crop = new Vector2(0f, 0f);
if(aspectRatio > ASPECT_RATIO)
{
scale = (float)height/(float)VIRTUAL_HEIGHT;
crop.x = (width - VIRTUAL_WIDTH*scale)/2f;
}
else if(aspectRatio < ASPECT_RATIO)
{
scale = (float)width/(float)VIRTUAL_WIDTH;
crop.y = (height - VIRTUAL_HEIGHT*scale)/2f;
}
else
{
scale = (float)width/(float)VIRTUAL_WIDTH;
}
float w = (float)VIRTUAL_WIDTH*scale;
float h = (float)VIRTUAL_HEIGHT*scale;
viewport = new Rectangle(crop.x, crop.y, w, h);
....
....
camera = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);