我有一个URL列表(它包含图像)。我需要把它转换成雪碧 LIBGDX . 我该怎么做?例如这样的url- https://lh3.googleusercontent.com/ogw/ADGmqu-Krm10pvcLx9MlffZK6f9DaQS3S8Afje3gRBk=s83-c-mo
LIBGDX
https://lh3.googleusercontent.com/ogw/ADGmqu-Krm10pvcLx9MlffZK6f9DaQS3S8Afje3gRBk=s83-c-mo
avwztpqn1#
你可以用 HttpRequest 若要下载资源,请将它们保存到临时文件中,然后可以初始化 Texture , TextureRegion , Sprite 或者像你平时做的那样:
HttpRequest
Texture
TextureRegion
Sprite
Net.HttpRequest request = new Net.HttpRequest(Net.HttpMethods.GET); request.setUrl("https://lh3.googleusercontent.com/ogw/ADGmqu-Krm10pvcLx9MlffZK6f9DaQS3S8Afje3gRBk=s83-c-mo");
然后,在 handleHttpResponse 可以保存到临时文件的回调:
handleHttpResponse
final FileHandle tmpFile = FileHandle.tempFile("texture"); tmpFile.write(httpResponse.getResultAsStream(), false); Gdx.app.postRunnable(new Runnable() { @Override public void run () { texture = new Texture(tmpFile); } });
请注意,您需要 postRunnable 返回到主线程,因为图形资源需要加载到opengl线程上。应用程序加载资源的完整工作示例可能如下所示:
postRunnable
package com.bornander.sandbox; import com.badlogic.gdx.ApplicationAdapter; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Net; import com.badlogic.gdx.files.FileHandle; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.SpriteBatch; public class SandboxGame extends ApplicationAdapter { OrthographicCamera camera; SpriteBatch batch; Texture texture; @Override public void create () { float aspectRatio = (float)Gdx.graphics.getHeight()/(float)Gdx.graphics.getWidth(); camera = new OrthographicCamera(800, 800 * aspectRatio); camera.position.set(camera.viewportWidth / 2.0f, camera.viewportHeight / 2.0f, 0.0f); batch = new SpriteBatch(); Net.HttpRequest request = new Net.HttpRequest(Net.HttpMethods.GET); request.setUrl("https://lh3.googleusercontent.com/ogw/ADGmqu-Krm10pvcLx9MlffZK6f9DaQS3S8Afje3gRBk=s83-c-mo"); Gdx.net.sendHttpRequest(request, new Net.HttpResponseListener() { @Override public void handleHttpResponse (Net.HttpResponse httpResponse) { final FileHandle tmpFile = FileHandle.tempFile("texture"); tmpFile.write(httpResponse.getResultAsStream(), false); Gdx.app.postRunnable(new Runnable() { @Override public void run () { texture = new Texture(tmpFile); } }); } @Override public void failed (Throwable t) { Gdx.app.error("yourtag", "failed to load", t); } @Override public void cancelled () { Gdx.app.log("yourtag", "load cancelled"); } }); } @Override public void render () { Gdx.gl.glClearColor(0.6f, 0.6f, 1.0f, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); camera.update(); batch.setProjectionMatrix(camera.combined); batch.begin(); if (texture != null) batch.draw(texture, 0, 0); batch.end(); } }
1条答案
按热度按时间avwztpqn1#
你可以用
HttpRequest
若要下载资源,请将它们保存到临时文件中,然后可以初始化Texture
,TextureRegion
,Sprite
或者像你平时做的那样:然后,在
handleHttpResponse
可以保存到临时文件的回调:请注意,您需要
postRunnable
返回到主线程,因为图形资源需要加载到opengl线程上。应用程序加载资源的完整工作示例可能如下所示: