Skip to content

Commit

Permalink
Merge pull request princekin-f#159 from xiongmaozhijin/master
Browse files Browse the repository at this point in the history
增加layout大小改变时,view的对齐方式
  • Loading branch information
princekin-f authored Jul 8, 2021
2 parents 9a9e7e3 + f423044 commit 5ab66a0
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 14 deletions.
10 changes: 10 additions & 0 deletions easyfloat/src/main/java/com/lzf/easyfloat/EasyFloat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ class EasyFloat {
config.offsetPair = Pair(offsetX, offsetY)
}

/**
* 当layout大小变化后,整体view的位置的对齐方式
* 比如,当设置为 Gravity.END 时,当view的宽度变小或者变大时,都将会以原有的右边对齐 <br/>
* 默认对齐方式为左上角
* @param gravity 对齐方式
*/
fun setLayoutChangedGravity(gravity: Int) = apply {
config.layoutChangedGravity = gravity;
}

/**
* 设置浮窗的起始坐标,优先级高于setGravity
* @param x 起始水平坐标
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.graphics.PixelFormat
import android.graphics.Rect
import android.os.Build
import android.os.IBinder
import android.util.Log
import android.view.*
import android.view.WindowManager.LayoutParams.*
import android.widget.EditText
Expand All @@ -34,15 +35,30 @@ internal class FloatingWindowHelper(val context: Context, var config: FloatConfi
var frameLayout: ParentFrameLayout? = null
private lateinit var touchUtils: TouchUtils
private var enterAnimator: Animator? = null
var lastLayoutMeasureWidth = -1
var lastLayoutMeasureHeight = -1;

fun createWindow() = try {
fun createWindow() {
touchUtils = TouchUtils(context, config)
initParams()
addView()
config.isShow = true
} catch (e: Exception) {
config.callbacks?.createdResult(false, "$e", null)
config.floatCallbacks?.builder?.createdResult?.invoke(false, "$e", null)
if (getToken() == null) {
val activity = if (context is Activity) context else LifecycleUtils.getTopActivity()
activity?.findViewById<View>(android.R.id.content)?.post {
createWindowInner()
}
} else {
createWindowInner()
}
}

private fun createWindowInner() {
try {
initParams()
addView()
config.isShow = true
} catch (e: Exception) {
config.callbacks?.createdResult(false, "$e", null)
config.floatCallbacks?.builder?.createdResult?.invoke(false, "$e", null)
}
}

private fun initParams() {
Expand Down Expand Up @@ -112,6 +128,8 @@ internal class FloatingWindowHelper(val context: Context, var config: FloatConfi
frameLayout?.layoutListener = object : ParentFrameLayout.OnLayoutListener {
override fun onLayout() {
setGravity(frameLayout)
lastLayoutMeasureWidth = frameLayout?.measuredWidth ?: -1
lastLayoutMeasureHeight = frameLayout?.measuredHeight ?: -1
config.apply {
// 如果设置了过滤当前页,或者后台显示前台创建、前台显示后台创建,隐藏浮窗,否则执行入场动画
if (filterSelf
Expand All @@ -130,6 +148,51 @@ internal class FloatingWindowHelper(val context: Context, var config: FloatConfi
}
}
}

// 监听frameLayout布局完成
frameLayout?.apply {
viewTreeObserver?.addOnGlobalLayoutListener {
val filterInvalidVal = lastLayoutMeasureWidth == -1 || lastLayoutMeasureHeight == -1
val filterEqualVal = lastLayoutMeasureWidth == this.measuredWidth && lastLayoutMeasureHeight == this.measuredHeight
if (filterInvalidVal || filterEqualVal) {
return@addOnGlobalLayoutListener
}

// 水平方向
if (config.layoutChangedGravity.and(Gravity.START) == Gravity.START) {
// ignore

} else if (config.layoutChangedGravity.and(Gravity.END) == Gravity.END) {
val diffChangedSize = this.measuredWidth - lastLayoutMeasureWidth
params.x = params.x - diffChangedSize

} else if (config.layoutChangedGravity.and(Gravity.CENTER_HORIZONTAL) == Gravity.CENTER_HORIZONTAL ||
config.layoutChangedGravity.and(Gravity.CENTER) == Gravity.CENTER) {
val diffChangedCenter = lastLayoutMeasureWidth / 2 - this.measuredWidth / 2
params.x += diffChangedCenter
}

// 垂直方向
if (config.layoutChangedGravity.and(Gravity.TOP) == Gravity.TOP) {
// ignore

} else if (config.layoutChangedGravity.and(Gravity.BOTTOM) == Gravity.BOTTOM) {
val diffChangedSize = this.measuredHeight - lastLayoutMeasureHeight
params.y = params.y - diffChangedSize

} else if (config.layoutChangedGravity.and(Gravity.CENTER_VERTICAL) == Gravity.CENTER_VERTICAL ||
config.layoutChangedGravity.and(Gravity.CENTER) == Gravity.CENTER) {
val diffChangedCenter = lastLayoutMeasureHeight / 2 - this.measuredHeight / 2
params.y += diffChangedCenter
}

lastLayoutMeasureWidth = this.measuredWidth
lastLayoutMeasureHeight = this.measuredHeight

// 更新浮窗位置信息
windowManager.updateViewLayout(frameLayout, params)
}
}
}

private fun initEditText() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lzf.easyfloat.data

import android.view.Gravity
import android.view.View
import com.lzf.easyfloat.anim.DefaultAnimator
import com.lzf.easyfloat.enums.ShowPattern
Expand Down Expand Up @@ -75,6 +76,8 @@ data class FloatConfig(
// 是否设置,当前创建的页面也被过滤
internal var filterSelf: Boolean = false,
// 是否需要显示,当过滤信息匹配上时,该值为false(用户手动调用隐藏,该值也为false,相当于手动过滤)
internal var needShow: Boolean = true
internal var needShow: Boolean = true,

// 当layout大小变化后,整体view的位置的摆放
var layoutChangedGravity: Int = Gravity.TOP.or(Gravity.START)
)
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.lzf.easyfloat.permission.PermissionUtils
import com.lzf.easyfloat.utils.DragUtils
import com.lzf.easyfloat.widget.BaseSwitchView
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.float_seekbar.*
import kotlin.math.max


Expand Down Expand Up @@ -53,6 +54,9 @@ class MainActivity : BaseActivity(), View.OnClickListener {
openSecond.setOnClickListener(this)
openSwipeTest.setOnClickListener(this)
openBorderTest.setOnClickListener(this)

// 测试activity中onCreate就启动浮框
// showActivity2()
}

override fun onClick(v: View?) {
Expand Down Expand Up @@ -157,7 +161,9 @@ class MainActivity : BaseActivity(), View.OnClickListener {
EasyFloat.with(this)
.setTag(tag)
.setGravity(Gravity.CENTER)
.setLayoutChangedGravity(Gravity.END)
.setLayout(R.layout.float_seekbar) {
val layoutContent = it.findViewById<View>(R.id.layoutContent);
it.findViewById<ImageView>(R.id.ivClose).setOnClickListener {
EasyFloat.dismiss(tag)
}
Expand All @@ -176,6 +182,10 @@ class MainActivity : BaseActivity(), View.OnClickListener {

override fun onStopTrackingTouch(seekBar: SeekBar?) {}
})
it.findViewById<TextView>(R.id.viewOther).setOnClickListener { otherView->
if (layoutContent.visibility == View.VISIBLE) layoutContent.visibility = View.GONE
else layoutContent.visibility = View.VISIBLE
}
}
.show()
}
Expand Down
30 changes: 24 additions & 6 deletions example/src/main/res/layout/float_seekbar.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="150dp"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutContent"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:background="@color/translucent"
android:orientation="vertical">

<TextView
<TextView
android:id="@+id/tvProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand All @@ -15,15 +21,15 @@
android:textColor="@color/pinkRed"
android:textSize="20sp" />

<ImageView
<ImageView
android:id="@+id/ivClose"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_alignParentEnd="true"
android:padding="10dp"
android:src="@drawable/icon_x" />

<SeekBar
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -33,4 +39,16 @@
android:progress="20" />


</RelativeLayout>
</RelativeLayout>


<TextView
android:id="@+id/viewOther"
android:layout_width="wrap_content"
android:text="简化模式"
android:gravity="center"
android:textColor="@color/white"
android:layout_height="55dp"
android:background="@color/translucent" />

</LinearLayout>

0 comments on commit 5ab66a0

Please sign in to comment.