Libgdx在运行时从Facebook获取图像

4

我有一个从Facebook获取的图片链接,我想在libgdx游戏中运行时显示该图片。我正在使用Facebook Graph API,并通过Json解析来解析数据。我的方法如下:

在主活动中

protected void gettingfacebookData() {
    try {
        JSONArray friendArray = new JSONArray(
                prefLevel.getFriendFacebookData());
        for (int i = 0; i < friendArray.length(); i++) {
            JSONObject jsonObject = friendArray.getJSONObject(i);

            String name = jsonObject.getString("name");
            String score = jsonObject.getString("score");
            String fid = jsonObject.getString("fid");
            String image = "http://graph.facebook.com/" + fid
                    + "/picture?type=large";
//saving score into array list
            PlayingScreen.scoreAl.add(score);
//saving image url into arraylist
            PlayingScreen.imageUrlAl.add(image);

            }           }

        } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

现在我该如何在运行时显示这个特定的图片URL?
2个回答

3
我们使用一个包装类,对Texture进行包装,用于显示来自网络的图片(例如Facebook个人资料图片)。这个包装类接受图片的url和临时纹理。当创建了这个包装类之后,它就开始在后台线程中下载图像字节。这个包装类的消费者只需调用getTexture()方法获取纹理,直到下载完成,该方法返回临时纹理。当有字节可用于创建纹理时,getTexture()处理这些字节并开始返回从url创建的新纹理。

以下是这个包装类的简单版本。请注意,processTextureBytes方法在getTexture方法内部被调用,而不是在后台线程中的某个位置被调用。这是因为我们必须在获得GLContext的线程中构造纹理。您可以向此类添加缓存和重试机制。

顺便说一下,不要使用http://graph.facebook.com/[uid]/picture url,而要尝试使用FQL中的一个pic_ url。您可能需要检查这个

public class WebTexture {
    private final String url;
    private Texture texture;
    private volatile byte[] textureBytes;

    public WebTexture(String url, Texture tempTexture) {
        this.url = url;
        texture = tempTexture;
        downloadTextureAsync();
    }

    private void downloadTextureAsync() {
        Utils.runInBackground(new Runnable() {
            @Override
            public void run() {
                textureBytes = downloadTextureBytes();
            }
        });
    }

    private byte[] downloadTextureBytes() {
        try {
            return Utils.downloadData(url);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public Texture getTexture() {
        if (textureBytes != null)
            processTextureBytes();
        return texture;
    }

    private void processTextureBytes() {
        try {
            Pixmap pixmap = new Pixmap(textureBytes, 0, textureBytes.length);
            Texture gdxTexture = new Texture(pixmap);
            gdxTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
            texture = gdxTexture;
        } catch (Throwable t) {
            t.printStackTrace();
        } finally {
            textureBytes = null;
        }
    }
}

1
Utils 的导入是什么? - Laxman Marothiya
是的,那个import是什么? - RocketScienceGuy

1
  1. 你应该下载这个图片。
  2. 一旦你把图片转换成字节数组,你可以创建一个Pixmap:

    Pixmap pixmap = new Pixmap(imageBytes, 0, imageBytes.length);
  3. 然后你可以使用这个Pixmap来渲染图片。例如,如果你正在使用scene2d.ui,你可以将Image的drawable设置如下:

image.setDrawable(new TextureRegionDrawable(new TextureRegion(new Texture(profilePicture))));

希望这能帮到你。


这实际上是我正在做的事情,但我遇到了渐进式JPEG图像错误,并且此时我的游戏开始出现滞后,因为我只保存URL而没有下载图像并在运行时开始图像下载,这导致了滞后 :( - Nishant Anindya
你在桌面端还是安卓设备上遇到了渐进式JPEG错误? - ilkinulas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接