Как центрировать окно режима «картинка в картинке» (режим PiP) ⇐ Android
Как центрировать окно режима «картинка в картинке» (режим PiP)
I am working on a project in android studio with two languages java and kotlin about displaying a view in my application that displays content such as video.
I would like to center an instance of the picture-in-picture mode or commonly known as picture and picture mode. Currently, when I start the instance it is created in the lower right corner, but I would like to have more control over where it appears, for example, I want it to appear at the bottom when the instance appears. center of the device screen.
What I have tried through the layout is to try to center it with android:layout_gravity='center_horizontal' but it didn't work, it still appears in the lower right corner.
Another option that I have tried is to add android:layout_centerInParent='true' but nothing and finally I tried to directly modify the code with the gravity constants of the android api but nothing...
and finally I went to the android manifest adding the label
android:layout_gravity='center_vertical'
This is the code I'm currently working on:
VideoPL.kt
package com.sim.lyxx; import android.app.Activity import android.app.ActivityOptions import android.app.PictureInPictureParams import android.content.Intent import android.graphics.PixelFormat import android.graphics.Rect import android.os.Build import android.os.Bundle import android.util.Rational import android.view.Gravity import android.view.Menu import android.view.MotionEvent import android.view.View import android.view.WindowManager import android.widget.LinearLayout import android.widget.Toast import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.MediaItem import com.google.android.exoplayer2.Player import com.google.android.exoplayer2.ui.AspectRatioFrameLayout import com.google.android.exoplayer2.ui.PlayerControlView import com.sim.lyxx.databinding.CustomToastBinding @RequiresApi(Build.VERSION_CODES.O) class VideoPL : AppCompatActivity() { private lateinit var binding: CustomToastBinding private var mExoPlayer: ExoPlayer? = null private var isPipMode: Boolean = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = CustomToastBinding.inflate(layoutInflater) setContentView(binding.root) hideSystemUI() // Inicializa ExoPlayer y comienza la reproducción initializeExoPlayer() // Entra en modo PIP al iniciar la actividad createPIPMode() // Configura el listener para la visibilidad del controlador binding.exoPlayer.setControllerVisibilityListener { visibility -> val layout = findViewById(R.id.ll_customPlayBackControlView) if (layout.tag != "IN_ANIMATION") { when (visibility) { View.GONE -> { layout.tag = "IN_ANIMATION" binding.exoPlayer.showController() layout.animate().alpha(0F).setDuration(450L) .withEndAction({ binding.exoPlayer.hideController() layout.tag = "" }).start() } View.VISIBLE -> { layout.animate().alpha(1F).setDuration(450L).start() } } } } } override fun dispatchTouchEvent(ev: MotionEvent): Boolean { // Bloquea los eventos táctiles solo cuando estás en modo Picture-in-Picture if (isInPictureInPictureMode) { // Retorna true para consumir los eventos táctiles y evitar que se propaguen a otras vistas return true } // Continúa con el comportamiento predeterminado cuando no estás en modo Picture-in-Picture return super.dispatchTouchEvent(ev) } private fun hideSystemUI() { window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE) } private fun initializeExoPlayer() { mExoPlayer = ExoPlayer.Builder(applicationContext).build() binding.exoPlayer.player = mExoPlayer binding.exoPlayer.useController = false binding.exoPlayer.hideController() binding.exoPlayer.resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FILL val mediaItem: MediaItem = MediaItem.fromUri("http://clips.vorwaerts-gmbh.de/VfE_html5.mp4") mExoPlayer!!.setMediaItem(mediaItem) mExoPlayer!!.prepare() mExoPlayer!!.play() } override fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean) { super.onPictureInPictureModeChanged(isInPictureInPictureMode) if (!isInPictureInPictureMode) { // Restaura configuración después de salir del modo PiP binding.exoPlayer.useController = false binding.exoPlayer.hideController() } } private fun createPIPMode() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val pictureInPictureParams = PictureInPictureParams.Builder() .build() enterPictureInPictureMode(pictureInPictureParams) } } override fun onBackPressed() { if (isPipMode) { createPIPMode() } else { super.onBackPressed() } } override fun onCreateOptionsMenu(menu: Menu): Boolean { return false } companion object { @JvmStatic fun cancelExecution(activity: Activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && activity.isInPictureInPictureMode) { val params = PictureInPictureParams.Builder() .setAspectRatio(Rational(16, 9)) .build() activity.setPictureInPictureParams(params) Toast.makeText(activity, "Actividad VideoPL cerrada", Toast.LENGTH_SHORT).show() } else { if (activity is MainActivity) { // Cierra la actividad actual y reinicia la aplicación val intent = Intent(activity, MainActivity::class.java) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK) activity.startActivity(intent) activity.finish() } } } } } layout:
android_manifest.xml
Источник: https://stackoverflow.com/questions/780 ... w-pip-mode
I am working on a project in android studio with two languages java and kotlin about displaying a view in my application that displays content such as video.
I would like to center an instance of the picture-in-picture mode or commonly known as picture and picture mode. Currently, when I start the instance it is created in the lower right corner, but I would like to have more control over where it appears, for example, I want it to appear at the bottom when the instance appears. center of the device screen.
What I have tried through the layout is to try to center it with android:layout_gravity='center_horizontal' but it didn't work, it still appears in the lower right corner.
Another option that I have tried is to add android:layout_centerInParent='true' but nothing and finally I tried to directly modify the code with the gravity constants of the android api but nothing...
and finally I went to the android manifest adding the label
android:layout_gravity='center_vertical'
This is the code I'm currently working on:
VideoPL.kt
package com.sim.lyxx; import android.app.Activity import android.app.ActivityOptions import android.app.PictureInPictureParams import android.content.Intent import android.graphics.PixelFormat import android.graphics.Rect import android.os.Build import android.os.Bundle import android.util.Rational import android.view.Gravity import android.view.Menu import android.view.MotionEvent import android.view.View import android.view.WindowManager import android.widget.LinearLayout import android.widget.Toast import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.MediaItem import com.google.android.exoplayer2.Player import com.google.android.exoplayer2.ui.AspectRatioFrameLayout import com.google.android.exoplayer2.ui.PlayerControlView import com.sim.lyxx.databinding.CustomToastBinding @RequiresApi(Build.VERSION_CODES.O) class VideoPL : AppCompatActivity() { private lateinit var binding: CustomToastBinding private var mExoPlayer: ExoPlayer? = null private var isPipMode: Boolean = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = CustomToastBinding.inflate(layoutInflater) setContentView(binding.root) hideSystemUI() // Inicializa ExoPlayer y comienza la reproducción initializeExoPlayer() // Entra en modo PIP al iniciar la actividad createPIPMode() // Configura el listener para la visibilidad del controlador binding.exoPlayer.setControllerVisibilityListener { visibility -> val layout = findViewById(R.id.ll_customPlayBackControlView) if (layout.tag != "IN_ANIMATION") { when (visibility) { View.GONE -> { layout.tag = "IN_ANIMATION" binding.exoPlayer.showController() layout.animate().alpha(0F).setDuration(450L) .withEndAction({ binding.exoPlayer.hideController() layout.tag = "" }).start() } View.VISIBLE -> { layout.animate().alpha(1F).setDuration(450L).start() } } } } } override fun dispatchTouchEvent(ev: MotionEvent): Boolean { // Bloquea los eventos táctiles solo cuando estás en modo Picture-in-Picture if (isInPictureInPictureMode) { // Retorna true para consumir los eventos táctiles y evitar que se propaguen a otras vistas return true } // Continúa con el comportamiento predeterminado cuando no estás en modo Picture-in-Picture return super.dispatchTouchEvent(ev) } private fun hideSystemUI() { window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE) } private fun initializeExoPlayer() { mExoPlayer = ExoPlayer.Builder(applicationContext).build() binding.exoPlayer.player = mExoPlayer binding.exoPlayer.useController = false binding.exoPlayer.hideController() binding.exoPlayer.resizeMode = AspectRatioFrameLayout.RESIZE_MODE_FILL val mediaItem: MediaItem = MediaItem.fromUri("http://clips.vorwaerts-gmbh.de/VfE_html5.mp4") mExoPlayer!!.setMediaItem(mediaItem) mExoPlayer!!.prepare() mExoPlayer!!.play() } override fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean) { super.onPictureInPictureModeChanged(isInPictureInPictureMode) if (!isInPictureInPictureMode) { // Restaura configuración después de salir del modo PiP binding.exoPlayer.useController = false binding.exoPlayer.hideController() } } private fun createPIPMode() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val pictureInPictureParams = PictureInPictureParams.Builder() .build() enterPictureInPictureMode(pictureInPictureParams) } } override fun onBackPressed() { if (isPipMode) { createPIPMode() } else { super.onBackPressed() } } override fun onCreateOptionsMenu(menu: Menu): Boolean { return false } companion object { @JvmStatic fun cancelExecution(activity: Activity) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && activity.isInPictureInPictureMode) { val params = PictureInPictureParams.Builder() .setAspectRatio(Rational(16, 9)) .build() activity.setPictureInPictureParams(params) Toast.makeText(activity, "Actividad VideoPL cerrada", Toast.LENGTH_SHORT).show() } else { if (activity is MainActivity) { // Cierra la actividad actual y reinicia la aplicación val intent = Intent(activity, MainActivity::class.java) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK) activity.startActivity(intent) activity.finish() } } } } } layout:
android_manifest.xml
Источник: https://stackoverflow.com/questions/780 ... w-pip-mode
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
PiP (картинка в картинке), как только я присоединюсь к использованию jitsi_meet_flutter_sdk
Anonymous » » в форуме IOS - 0 Ответы
- 58 Просмотры
-
Последнее сообщение Anonymous
-
-
-
PIP (картинка в картинке) не работает в интерактивном веб-просмотре Android
Anonymous » » в форуме Android - 0 Ответы
- 13 Просмотры
-
Последнее сообщение Anonymous
-