kotlin Jetpack合成中的画中画

juud5qan  于 2022-12-23  发布在  Kotlin
关注(0)|答案(3)|浏览(165)

我是新的组成。有一种方法来处理画中画模式在喷气背包组成?我找不到任何官方文件有关这一点。

vsmadaxz

vsmadaxz1#

class MainActivity : ComponentActivity() {

class MyReciever:BroadcastReceiver(){
    override fun onReceive(context: Context?, intent: Intent?) {
       println("clicked on PIP action")
    }

}

private val isPipSupported by lazy {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        packageManager.hasSystemFeature(
            PackageManager.FEATURE_PICTURE_IN_PICTURE
        )
    } else {
        false
    }
}
private var videoViewBounds = Rect()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        PictureInPictureTheme {
            /***
             * video view does't exit for compose.
             * So we use android view
             * and in factory we create video view and use apply to assign video.
             */
            AndroidView(
                factory ={
                    VideoView(it,null).apply {
                        setVideoURI(Uri.parse("android.resource://$packageName/${R.raw.lakshay}"))
                            start()
                    }
                } ,
                modifier = Modifier
                    .fillMaxWidth()
                    .onGloballyPositioned {
                        videoViewBounds = it
                            .boundsInWindow()
                            .toAndroidRect()
                    }
            )

        }
    }
}
private fun updatedPipParams(): PictureInPictureParams?{
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        PictureInPictureParams.Builder()
            .setSourceRectHint(videoViewBounds)
            .setAspectRatio(Rational(16,9))
            .setActions(
                listOf(
                    RemoteAction(
                    android.graphics.drawable.Icon.createWithResource(applicationContext,
                    R.drawable.ic_baseline_baby_changing_station_24),
                        "Baby Changing Station",
                        "Baby Changing Station",
                        PendingIntent.getBroadcast(
                            applicationContext,
                            0,
                            Intent(applicationContext,MyReciever::class.java),
                            PendingIntent.FLAG_IMMUTABLE
                        )
                    )
                )
            )
            .build()
    } else {
        TODO("VERSION.SDK_INT < O")
    }
}

override fun onUserLeaveHint() {
    super.onUserLeaveHint()
    if(!isPipSupported){
        return
    }
    updatedPipParams()?.let {params->
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            enterPictureInPictureMode(params)
        }
    }

}

}

idfiyjo8

idfiyjo82#

您可以使用(LocalContext.current as Activity),然后调用enablePictureInPictureMode方法。

dsekswqp

dsekswqp3#

Android有一个guide的画中画与示例代码在java和Kotlin。
它不在喷气背包状态。

相关问题