标签:
1 package com.mygdx.viewport; 2 3 import com.badlogic.gdx.graphics.Texture; 4 import com.badlogic.gdx.graphics.g2d.Batch; 5 import com.badlogic.gdx.scenes.scene2d.Actor; 6 /** 7 * 我的演员类 8 * @author Jack(乐智) 9 * @blog dtblog.cn 10 * @qq 984137183 11 */ 12 public class MyActor extends Actor { 13 private Texture texture; 14 public MyActor(){ 15 this.init(); 16 } 17 private void init() { 18 texture=new Texture("badlogic.jpg"); 19 } 20 @Override 21 public void draw(Batch batch, float parentAlpha) { 22 batch.draw(texture, 0, 0); 23 } 24 25 } 26 27 28 package com.mygdx.viewport; 29 30 import com.badlogic.gdx.ApplicationAdapter; 31 import com.badlogic.gdx.Gdx; 32 import com.badlogic.gdx.graphics.GL20; 33 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 34 import com.badlogic.gdx.scenes.scene2d.Stage; 35 import com.badlogic.gdx.utils.viewport.ExtendViewport; 36 import com.badlogic.gdx.utils.viewport.ScreenViewport; 37 import com.badlogic.gdx.utils.viewport.StretchViewport; 38 39 /** 40 * 伸展视口 41 * @author Jack(乐智) 42 * @blog dtblog.cn 43 * @qq 984137183 44 */ 45 public class UseStretchViewport extends ApplicationAdapter{ 46 //精灵画笔 47 public SpriteBatch batch; 48 //声明演员 49 public MyActor actor; 50 //声明舞台 51 public Stage stage; 52 //声明伸展视口,等比缩放 53 private StretchViewport viewport; 54 //声明延展视口 55 private ExtendViewport extViewport; 56 private ScreenViewport screenViewport;//屏幕视口 57 @Override 58 public void create() { 59 //初始化精灵画笔 60 batch=new SpriteBatch(); 61 //初始化演员 62 actor=new MyActor(); 63 viewport=new StretchViewport(800, 480); 64 extViewport =new ExtendViewport(200, 200); 65 screenViewport=new ScreenViewport(); 66 //初始化舞台 67 // stage=new Stage(viewport); 68 // stage=new Stage(extViewport ); 69 stage=new Stage(screenViewport); 70 //添加演员 71 stage.addActor(actor); 72 73 } 74 @Override 75 public void render() { 76 //设置背景颜色 77 Gdx.gl.glClearColor(1, 1, 1, 1); 78 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 79 //更新舞台逻辑 80 stage.act(); 81 //绘制舞台内容 82 stage.draw(); 83 } 84 85 }
【开源java游戏框架libgdx专题】-10-核心库-Viewport
标签:
原文地址:http://www.cnblogs.com/AIThink/p/5929070.html