libGDX: 我如何告诉核心游戏 getIconImageUri() 并将其用作我的游戏中的图像演员

libGDX: How can I tell core game getIconImageUri() and use it as Image actor into my game

我在核心项目上创建了IGoogleServices接口:

public interface IGoogleServices {    
    .....
    public String getPlayerName();
}

然后我将以下代码放在 android 项目的 MainActivity 的实现方法中:

private String name;
@Override
public String getPlayerName() {
    runOnUiThread(new Runnable() {
        public void run() {
            name = Games.Players.getCurrentPlayer(_gameHelper.getApiClient()).getDisplayName();
        }
    });
    return name;
}

这是告诉 MyGame 玩家名称的简单方法 String:

lblPlayerName = new Label("" + googleServices.getPlayerName(), style);
stage.addActor(lblPlayerName);

但是在图片的情况下我做不到。

在之前的String案例中,我尝试在图像案例中这样做:

private Uri imgUri;
@Override
public void requestImgProfile() {
    runOnUiThread(new Runnable() {
        public void run() {
            imgUri = Games.Players.getCurrentPlayer(_gameHelper.getApiClient()).getIconImageUri();
            ImageManager manager = ImageManager.create(MainActivity.this);
            manager.loadImage(new ImageManager.OnImageLoadedListener() {
                public void onImageLoaded(Uri arg0, Drawable drawable, boolean arg2) {
                        try {
                            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                            FileOutputStream out = new FileOutputStream(imagePath);;
                            out = openFileOutput(imgUri + ".png", Context.MODE_MULTI_PROCESS);
                            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
                            out.flush();
                            out.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }, imgUri);
            };
        });
    }

我的imgUri是:

content://com.google.android.gms.games.background/images/26s5523b/2633

什么是 class 类型(如 String),我可以告诉 MyGame 它和 libGDX 图书馆可以处理它?

如果您有图像 URI,您可以通过以下方式获取位图:

Uri imgUri;
Bitmap bitmap = null;
try {
    InputStream inputStream = getContentResolver().openInputStream(imgUri);
    bitmap = BitmapFactory.decodeStream(inputStream);
} catch (FileNotFoundException e) {
}

无论是 file:// URI 还是 content:// URI,这都有效。

然后使用 this answer 等解决方案将位图转换为 libGDX 可用的内容。