From b497e13c5180a471d68620d65afffd853bfcb15a Mon Sep 17 00:00:00 2001
From: li-xiaojun <16167479@qq.com>
Date: Sat, 19 Nov 2022 19:19:28 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96Bubble=E5=BC=B9=E7=AA=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../xpopupdemo/fragment/QuickStartDemo.java | 13 ++--
.../main/res/layout/fragment_quickstart.xml | 13 +++-
.../xpopup/core/BubbleAttachPopupView.java | 66 ++++++++++---------
3 files changed, 56 insertions(+), 36 deletions(-)
diff --git a/app/src/main/java/com/lxj/xpopupdemo/fragment/QuickStartDemo.java b/app/src/main/java/com/lxj/xpopupdemo/fragment/QuickStartDemo.java
index 1f97b72a..00b99c14 100644
--- a/app/src/main/java/com/lxj/xpopupdemo/fragment/QuickStartDemo.java
+++ b/app/src/main/java/com/lxj/xpopupdemo/fragment/QuickStartDemo.java
@@ -376,13 +376,20 @@ public void onSelect(int position, String text) {
.hasShadowBg(false) // 去掉半透明背景
.asCustom(new CustomHorizontalBubbleAttachPopup(getContext()))
.show();
+// new XPopup.Builder(getContext())
+// .isTouchThrough(true)
+// .isDestroyOnDismiss(true) //对于只使用一次的弹窗,推荐设置这个
+// .atView(view.findViewById(R.id.vv))
+// .hasShadowBg(false) // 去掉半透明背景
+//// .offsetX(XPopupUtils.dp2px(getContext(), 20))
+// .asCustom(new CustomBubbleAttachPopup(getContext()))
+// .show();
break;
case R.id.btnBubbleAttachPopup2: //垂直方向带气泡弹窗
new XPopup.Builder(getContext())
.isTouchThrough(true)
.isDestroyOnDismiss(true) //对于只使用一次的弹窗,推荐设置这个
- .atView(view.findViewById(R.id.vv))
- .offsetX(-XPopupUtils.dp2px(getContext(), 30))
+ .atView(view.findViewById(R.id.vv2))
.hasShadowBg(false) // 去掉半透明背景
// .offsetX(XPopupUtils.dp2px(getContext(), 20))
.asCustom(new CustomBubbleAttachPopup(getContext()))
@@ -394,7 +401,6 @@ public void onSelect(int position, String text) {
// .asCustom(new CustomDrawerPopupView(getContext()))
// .hasShadowBg(false)
// .maxWidth(100)
- .dismissOnTouchOutside(false)
.isViewMode(true) //使用了Fragment,必须开启View模式
.asCustom(new PagerDrawerPopup(getContext()))
// .asCustom(new ListDrawerPopupView(getContext()))
@@ -405,7 +411,6 @@ public void onSelect(int position, String text) {
// .isDestroyOnDismiss(true) //对于只使用一次的弹窗,推荐设置这个
.autoOpenSoftInput(true)
// .popupWidth(300)
- .dismissOnTouchOutside(false)
.popupPosition(PopupPosition.Right)//右边
// .hasStatusBarShadow(true) //启用状态栏阴影
.setPopupCallback(new DemoXPopupListener())
diff --git a/app/src/main/res/layout/fragment_quickstart.xml b/app/src/main/res/layout/fragment_quickstart.xml
index 30713014..138a0529 100644
--- a/app/src/main/res/layout/fragment_quickstart.xml
+++ b/app/src/main/res/layout/fragment_quickstart.xml
@@ -189,13 +189,15 @@
android:layout_marginTop="10dp"
android:layout_width="0dp"
android:layout_weight="1"
- android:layout_marginEnd="55dp"
android:background="#ddd"
android:layout_height="wrap_content"
android:text="横向带气泡Attach弹窗"
android:textAllCaps="false" />
-
@@ -207,6 +209,13 @@
android:layout_height="wrap_content"
android:text="垂直带气泡Attach弹窗"
android:textAllCaps="false" />
+
maxY;
if (isTallerThanWindowHeight) {
- isShowUp = popupInfo.touchPoint.y > XPopupUtils.getScreenHeight(getContext()) / 2;
+ isShowUp = popupInfo.touchPoint.y > XPopupUtils.getScreenHeight(getContext()) / 2f;
} else {
isShowUp = false;
}
- isShowLeft = popupInfo.touchPoint.x < XPopupUtils.getAppWidth(getContext()) / 2;
+ isShowLeft = popupInfo.touchPoint.x > XPopupUtils.getAppWidth(getContext()) / 2f;
//限制最大宽高
ViewGroup.LayoutParams params = getPopupContentView().getLayoutParams();
int maxHeight = (int) (isShowUpToTarget() ? (popupInfo.touchPoint.y - getStatusBarHeight() - overflow)
: (XPopupUtils.getScreenHeight(getContext()) - popupInfo.touchPoint.y - overflow));
- int maxWidth = (int) (isShowLeft ? (XPopupUtils.getAppWidth(getContext()) - popupInfo.touchPoint.x - overflow) : (popupInfo.touchPoint.x - overflow));
+ int maxWidth = (int) (isShowLeft ? (popupInfo.touchPoint.x - overflow) : (XPopupUtils.getAppWidth(getContext()) - popupInfo.touchPoint.x - overflow));
if (getPopupContentView().getMeasuredHeight() > maxHeight) {
params.height = maxHeight;
}
@@ -124,7 +125,7 @@ public void run() {
if (isRTL) {
translationX = -(XPopupUtils.getAppWidth(getContext()) - popupInfo.touchPoint.x - defaultOffsetX - getPopupContentView().getMeasuredWidth() / 2f);
} else {
- translationX = popupInfo.touchPoint.x + defaultOffsetX - getPopupContentView().getMeasuredWidth() / 2f;
+ translationX = popupInfo.touchPoint.x + defaultOffsetX - getPopupContentView().getMeasuredWidth() + bubbleContainer.getShadowRadius();
}
if (isShowUpToTarget()) {
// 应显示在point上方
@@ -139,11 +140,7 @@ public void run() {
}else {
bubbleContainer.setLook(BubbleLayout.Look.TOP);
}
- if(defaultOffsetX==0){
- bubbleContainer.setLookPositionCenter(true);
- }else {
- bubbleContainer.setLookPosition(Math.max(0, (int) (bubbleContainer.getMeasuredWidth()/2f - defaultOffsetX- bubbleContainer.mLookWidth/2)));
- }
+ bubbleContainer.setLookPosition(Math.max(0, (int) (popupInfo.touchPoint.x - defaultOffsetX - translationX - bubbleContainer.mLookWidth/2)));
bubbleContainer.invalidate();
getPopupContentView().setTranslationX(translationX);
@@ -167,39 +164,44 @@ public void run() {
if (isTallerThanWindowHeight) {
//超出可用大小就显示在上方
isShowUp = true;
-// isShowUp = centerY > XPopupUtils.getScreenHeight(getContext()) / 2;
} else {
isShowUp = false;
}
- isShowLeft = centerX < XPopupUtils.getAppWidth(getContext()) / 2;
+ isShowLeft = centerX > XPopupUtils.getAppWidth(getContext()) / 2;
//修正高度,弹窗的高有可能超出window区域
-// if (!isCreated) {
- ViewGroup.LayoutParams params = getPopupContentView().getLayoutParams();
- int maxHeight = isShowUpToTarget() ? (rect.top - getStatusBarHeight() - overflow)
- : (XPopupUtils.getScreenHeight(getContext()) - rect.bottom - overflow);
- int maxWidth = isShowLeft ? (XPopupUtils.getAppWidth(getContext()) - rect.left - overflow) : (rect.right - overflow);
- if (getPopupContentView().getMeasuredHeight() > maxHeight) {
- params.height = maxHeight;
- }
- if (getPopupContentView().getMeasuredWidth() > maxWidth) {
- params.width = maxWidth;
- }
- getPopupContentView().setLayoutParams(params);
-// }
+ ViewGroup.LayoutParams params = getPopupContentView().getLayoutParams();
+ int maxHeight = isShowUpToTarget() ? (rect.top - getStatusBarHeight() - overflow)
+ : (XPopupUtils.getScreenHeight(getContext()) - rect.bottom - overflow);
+ int maxWidth = isShowLeft ? (rect.right - overflow) : (XPopupUtils.getAppWidth(getContext()) - rect.left - overflow);
+ if (getPopupContentView().getMeasuredHeight() > maxHeight) {
+ params.height = maxHeight;
+ }
+ if (getPopupContentView().getMeasuredWidth() > maxWidth) {
+ params.width = maxWidth;
+ }
+ getPopupContentView().setLayoutParams(params);
getPopupContentView().post(new Runnable() {
@Override
public void run() {
if(popupInfo==null) return;
+ // translationX: 在左边就和atView左边对齐,在右边就和其右边对齐
if (isRTL) {
- translationX = -(XPopupUtils.getAppWidth(getContext()) - rect.left - rect.width()/2f - defaultOffsetX - getPopupContentView().getMeasuredWidth() / 2f);
+ if(isShowLeft){
+ translationX = -(XPopupUtils.getAppWidth(getContext()) - rect.right - defaultOffsetX - bubbleContainer.getShadowRadius());
+ }else {
+ translationX = - (XPopupUtils.getAppWidth(getContext()) - rect.left + defaultOffsetX + bubbleContainer.getShadowRadius() - getPopupContentView().getMeasuredWidth());
+ }
} else {
- translationX = rect.left + rect.width()/2f + defaultOffsetX - getPopupContentView().getMeasuredWidth() / 2f;
+ if(isShowLeft){
+ translationX = rect.right + defaultOffsetX - getPopupContentView().getMeasuredWidth() + bubbleContainer.getShadowRadius();
+ }else {
+ translationX = rect.left + defaultOffsetX - bubbleContainer.getShadowRadius();
+ }
}
if (isShowUpToTarget()) {
//说明上面的空间比较大,应显示在atView上方
- // translationX: 在左边就和atView左边对齐,在右边就和其右边对齐
translationY = rect.top - getPopupContentView().getMeasuredHeight() - defaultOffsetY;
} else {
translationY = rect.bottom + defaultOffsetY;
@@ -212,10 +214,14 @@ public void run() {
bubbleContainer.setLook(BubbleLayout.Look.TOP);
}
//箭头对着目标View的中心
- if(defaultOffsetX==0){
- bubbleContainer.setLookPositionCenter(true);
+ if(isRTL){
+ if(isShowLeft){
+ bubbleContainer.setLookPosition(Math.max(0, (int) (-translationX - rect.width()/2 - defaultOffsetX + bubbleContainer.mLookWidth/2)));
+ }else {
+ bubbleContainer.setLookPosition(Math.max(0, (int) ( rect.width()/2 - defaultOffsetX + bubbleContainer.mLookWidth/2)));
+ }
}else {
- bubbleContainer.setLookPosition(Math.max(0, (int) (bubbleContainer.getMeasuredWidth()/2f - defaultOffsetX- bubbleContainer.mLookWidth/2)));
+ bubbleContainer.setLookPosition(Math.max(0, (int) (rect.right - rect.width()/2 - defaultOffsetX - translationX - bubbleContainer.mLookWidth/2)));
}
bubbleContainer.invalidate();