【发布时间】:2022-12-28 04:29:38
【问题描述】:
I am creating a game for Android using LibGDX, the task is to insert a profile photo from the gallery?
【问题讨论】:
标签: libgdx
I am creating a game for Android using LibGDX, the task is to insert a profile photo from the gallery?
【问题讨论】:
标签: libgdx
class MainActivity : AppCompatActivity(), AndroidFragmentApplication.Callbacks {
private var blockImageFromGalleryResult: (Uri?) -> Unit = {}
private val selectImageFromGalleryResult = registerForActivityResult(ActivityResultContracts.GetContent()) {
uri: Uri? -> blockImageFromGalleryResult(uri)
}
fun selectImageFromGallery(block: (Uri?) -> Unit) {
blockImageFromGalleryResult = block
selectImageFromGalleryResult.launch("image/*")
}
class MenuScreen: AdvancedScreen(1280f, 727f) {
private val photoImage = Image(SpriteManager.MenuRegion.PHOTO.region)
private fun AdvancedStage.addPhoto() {
addActor(photoImage)
photoImage.apply {
setBounds(LM.photo)
toClickable().setOnClickListener {
MainActivity().selectImageFromGallery {
it?.let { uri ->
val bitmap: Bitmap = game.activity.contentResolver.openInputStream(uri).use { data -> BitmapFactory.decodeStream(data) }
runGDX {
val tex: Texture = Texture(bitmap.width, bitmap.height, Pixmap.Format.RGBA8888)
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, tex.textureObjectHandle)
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0)
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0)
bitmap.recycle()
photoImage.drawable = TextureRegionDrawable(tex)
}
}
}
}
}
}
https://medium.com/me/stats/post/4858e26734cf
【讨论】: