当前位置:首页 > 联系方式 >

(libgdx学习)Net的使用

作者:永年县聚联紧固件制造有限公司    发布时间:2017-09-03 12:51:22

(libgdx学习)Net的使用

在做这个例子的时候,记得把android中的联网的权限加上去

一、初始化及常用的API

HttpRequest httpRequest = new HttpRequest(Net.HttpMethods.GET); httpRequest.setUrl("http://www.cnlie.cn/uploads/allimg/c130624/13H0D049B340-1c53.jpg"); Gdx.net.sendHttpRequest(httpRequest, MyGame.this); final InputStream inputStream = httpResponse.getResultAsStream();
netTexture = new Texture(new FileHandle("image.png"){
@Override
public InputStream read() {

return inputStream;
}
});
二、应用举例 package com.example.groupactiontest; import java.io.InputStream; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Net; import com.badlogic.gdx.Net.HttpRequest; import com.badlogic.gdx.Net.HttpResponse; import com.badlogic.gdx.Net.HttpResponseListener; import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.graphics.GL10; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.InputListener; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.ImageButton; import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable; public class MyGame implements ApplicationListener,HttpResponseListener { Stage stage; ImageButton button; Texture texture; Texture netTexture; SpriteBatch batch; @Override public void handleHttpResponse(HttpResponse httpResponse) { final InputStream inputStream = httpResponse.getResultAsStream(); netTexture.setEnforcePotImages(false); netTexture = new Texture(new FileHandle("image.png"){ @Override public InputStream read() { return inputStream; } }); netTexture.setEnforcePotImages(true); } @Override public void failed(Throwable arg0) { System.out.println("联网失败...."); } @Override public void create() { stage = new Stage(); texture = new Texture(Gdx.files.internal("button.png")); batch = new SpriteBatch(); TextureRegion buttonUp = new TextureRegion(texture,0, 0, 210, 60); TextureRegion buttonDown = new TextureRegion(texture,210,0,210,60); TextureRegionDrawable up = new TextureRegionDrawable(buttonUp); TextureRegionDrawable down = new TextureRegionDrawable(buttonDown); button = new ImageButton(up, down); button.addListener(new InputListener(){ @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { HttpRequest httpRequest = new HttpRequest(Net.HttpMethods.GET); httpRequest.setUrl("http://www.cnlie.cn/uploads/allimg/c130624/13H0D049B340-1c53.jpg"); Gdx.net.sendHttpRequest(httpRequest, MyGame.this); return true; } }); stage.addActor(button); Gdx.input.setInputProcessor(stage); } @Override public void dispose() { // TODO Auto-generated method stub } @Override public void pause() { // TODO Auto-generated method stub } @Override public void render() { Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); if(netTexture != null){ batch.begin(); batch.draw(netTexture, 200, 150); batch.end(); } stage.act(); stage.draw(); } @Override public void resize(int arg0, int arg1) { // TODO Auto-generated method stub } @Override public void resume() { // TODO Auto-generated method stub } }

三、效果图

,专题

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:咸宁网站制作 http://xianning.666rj.com

  • 上一篇:ASP.NET MVC 视图(一)
  • 下一篇:最后一页
  •