Sfoglia il codice sorgente

[feat]键盘插件,修改悬浮窗默认位置

hezihao 8 mesi fa
parent
commit
fd45567d72

+ 10 - 4
plugins/keyboard_android/android/src/main/kotlin/com/atmob/keyboard_android/floating/FloatingButtonService.kt

@@ -5,7 +5,6 @@ import android.app.Service
 import android.content.Context
 import android.content.Intent
 import android.graphics.PixelFormat
-import android.net.Uri
 import android.os.Build
 import android.os.IBinder
 import android.provider.Settings
@@ -15,6 +14,7 @@ import android.view.MotionEvent
 import android.view.View
 import android.view.WindowManager
 import android.widget.ImageView
+import androidx.core.net.toUri
 import com.atmob.keyboard_android.R
 import com.atmob.keyboard_android.ext.click
 import com.atmob.keyboard_android.keyboard.InputMethodPickerActivity
@@ -36,7 +36,7 @@ class FloatingButtonService : Service() {
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(context)) {
                 val intent = Intent(
                     Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
-                    Uri.parse("package:${context.packageName}")
+                    "package:${context.packageName}".toUri()
                 )
                 // 重要:在非 Activity 里启动 Activity 需要添加 FLAG_ACTIVITY_NEW_TASK
                 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
@@ -84,7 +84,7 @@ class FloatingButtonService : Service() {
         val screenWidth = resources.displayMetrics.widthPixels
         val screenHeight = resources.displayMetrics.heightPixels
         // 初始位置在屏幕右侧
-        layoutParams.x = screenWidth - 200
+        layoutParams.x = screenWidth
         // 初始位置在屏幕中间
         layoutParams.y = screenHeight / 2
 
@@ -151,7 +151,13 @@ class FloatingButtonService : Service() {
     private fun snapToEdge() {
         val screenWidth = resources.displayMetrics.widthPixels
         val middle = screenWidth / 2f
-        layoutParams.x = if (layoutParams.x < middle) 0 else screenWidth - floatingView.width
+        layoutParams.x = if (layoutParams.x < middle) {
+            // 屏幕左边
+            0
+        } else {
+            // 屏幕右边
+            screenWidth - floatingView.width
+        }
         windowManager.updateViewLayout(floatingView, layoutParams)
     }