Skip to content

Commit

Permalink
1,增加调整悬浮窗大小的功能。
Browse files Browse the repository at this point in the history
  • Loading branch information
keluokeda committed Nov 14, 2022
1 parent 05aaff1 commit 920d87d
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.ke.hs_tracker.module.service

import android.view.MotionEvent
import android.view.View
import android.view.WindowManager

class ScaleTouchListener constructor(
private val windowManager: WindowManager,
private val rootView: View,
private val layoutParams: WindowManager.LayoutParams
) : View.OnTouchListener {
private var x = 0
private var y = 0

override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
when (motionEvent.action) {
MotionEvent.ACTION_DOWN -> {
x = motionEvent.rawX.toInt()
y = motionEvent.rawY.toInt()

}
MotionEvent.ACTION_MOVE -> {
val nowX = motionEvent.rawX.toInt()
val nowY = motionEvent.rawY.toInt()
val movedX = nowX - x
val movedY = nowY - y
x = nowX
y = nowY
layoutParams.apply {
// x += movedX
// y += movedY
width += movedX
height += movedY

}
//更新悬浮球控件位置
// windowManager.updateViewLayout(rootView, layoutParams)
windowManager.updateViewLayout(rootView, layoutParams)
}
else -> {

}
}
return true
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.os.IBinder
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup.LayoutParams
import android.view.WindowManager
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
Expand Down Expand Up @@ -54,8 +53,8 @@ class WindowService : LifecycleService() {
layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
}

layoutParams.width = 400
layoutParams.height = LayoutParams.WRAP_CONTENT
layoutParams.width = resources.getDimension(R.dimen.module_floating_window_width).toInt()
layoutParams.height = resources.getDimension(R.dimen.module_floating_window_height).toInt()
//需要设置 这个 不然空白地方无法点击
layoutParams.flags =
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
Expand All @@ -67,6 +66,10 @@ class WindowService : LifecycleService() {

binding.recyclerView.adapter = deckAdapter

binding.scale.setOnTouchListener(
ScaleTouchListener(windowManager, binding.root, layoutParams)
)

// binding.zoom.setOnClickListener {
// binding.recyclerView.isVisible = !binding.recyclerView.isVisible
// }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#FFFFFF"
android:viewportWidth="24.0"
android:viewportHeight="24.0">

<path
android:fillColor="@android:color/white"
android:pathData="M20,9H4v2h16V9zM4,15h16v-2H4V15z" />

</vector>
28 changes: 26 additions & 2 deletions module/src/main/res/layout/module_floating_window.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
android:layout_height="wrap_content"
android:contentDescription="@null"
android:padding="8dp"
android:src="@drawable/module_baseline_zoom_out_map_white_24dp"
android:src="@drawable/module_baseline_drag_handle_white_24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/header" />

Expand All @@ -52,9 +52,33 @@
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="0dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/zoom"
tools:itemCount="100"
tools:listitem="@layout/module_item_card" />

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/scale"
android:layout_width="24dp"
android:layout_height="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent">

<View
android:layout_width="4dp"
android:layout_height="match_parent"
android:background="@color/module_white"
app:layout_constraintEnd_toEndOf="parent" />

<View
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="@color/module_white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>


</androidx.constraintlayout.widget.ConstraintLayout>
2 changes: 2 additions & 0 deletions module/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="module_chart_height">300dp</dimen>
<dimen name="module_floating_window_width">150dp</dimen>
<dimen name="module_floating_window_height">300dp</dimen>
</resources>

0 comments on commit 920d87d

Please sign in to comment.