[英]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
的Implementaion方法中:
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
情況下,我嘗試在image情況下進行了此操作:
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
我可以告訴MyGame
和libGDX庫可以使用的類類型(例如String
)是什么?
如果您具有圖片URI,則可以通過以下方式獲取位圖:
Uri imgUri;
Bitmap bitmap = null;
try {
InputStream inputStream = getContentResolver().openInputStream(imgUri);
bitmap = BitmapFactory.decodeStream(inputStream);
} catch (FileNotFoundException e) {
}
無論是file://
URI還是content://
URI,此方法都有效。
然后使用諸如此答案之類的解決方案將位圖轉換為libGDX可用的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.