[Android] Widget preview: add a container to simulate the home screen widget bounds

Nicolas Pomepuy git at videolan.org
Mon May 15 14:03:22 UTC 2023


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Tue Mar 14 09:20:39 2023 +0100| [4ab42ca1b1976ebe9f20643d81ec74db3c23e18c] | committer: Duncan McNamara

Widget preview: add a container to simulate the home screen widget bounds

> https://code.videolan.org/videolan/vlc-android/commit/4ab42ca1b1976ebe9f20643d81ec74db3c23e18c
---

 .../org/videolan/vlc/widget/MiniPlayerConfigureActivity.kt  | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/widget/MiniPlayerConfigureActivity.kt b/application/vlc-android/src/org/videolan/vlc/widget/MiniPlayerConfigureActivity.kt
index 4cb1194958..45dd05a330 100644
--- a/application/vlc-android/src/org/videolan/vlc/widget/MiniPlayerConfigureActivity.kt
+++ b/application/vlc-android/src/org/videolan/vlc/widget/MiniPlayerConfigureActivity.kt
@@ -31,8 +31,12 @@ import android.graphics.Bitmap
 import android.graphics.BitmapFactory
 import android.os.Bundle
 import android.util.Log
+import android.view.Gravity
 import android.view.Menu
 import android.view.MenuItem
+import android.widget.FrameLayout
+import android.widget.FrameLayout.LayoutParams
+import androidx.core.content.ContextCompat
 import androidx.core.os.bundleOf
 import androidx.lifecycle.ViewModelProvider
 import androidx.lifecycle.lifecycleScope
@@ -137,9 +141,14 @@ class MiniPlayerConfigureActivity : BaseActivity() {
                         val width = if (widget.width <= 0 || widget.height <= 0) 276 else widget.width
                         val height = if (widget.width <= 0 || widget.height <= 0) 94 else widget.height
                         val views = provider.layoutWidget(this at MiniPlayerConfigureActivity, id, Intent(MiniPlayerAppWidgetProvider.ACTION_WIDGET_INIT), binding.previewPlaying.isChecked, coverBitmap, palette)
-                        val preview = views?.apply(applicationContext, null)
+                        val container = FrameLayout(this at MiniPlayerConfigureActivity).apply {
+                            layoutParams = LayoutParams(width, height)
+                        }
+                        val preview = views?.apply(applicationContext, container)
                         preview?.let {
-                            val bm: Bitmap = bitmapFromView(it, width.dp, height.dp)
+                            (preview.layoutParams as LayoutParams).gravity = Gravity.CENTER
+                            container.addView(preview)
+                            val bm: Bitmap = bitmapFromView(container, width.dp, height.dp)
                             withContext(Dispatchers.Main) { binding.widgetPreview.setImageBitmap(bm) }
                         }
                     }



More information about the Android mailing list