Skip to content

Commit

Permalink
优化Bubble弹窗
Browse files Browse the repository at this point in the history
  • Loading branch information
junixapp committed Nov 19, 2022
1 parent 9a1a213 commit b497e13
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand All @@ -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()))
Expand All @@ -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())
Expand Down
13 changes: 11 additions & 2 deletions app/src/main/res/layout/fragment_quickstart.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
<View
<TextView
android:id="@+id/vv"
android:layout_marginEnd="55dp"
android:text="1"
android:gravity="center"
android:background="#f00"
android:layout_width="35dp"
android:layout_height="35dp"/>
Expand All @@ -207,6 +209,13 @@
android:layout_height="wrap_content"
android:text="垂直带气泡Attach弹窗"
android:textAllCaps="false" />
<TextView
android:id="@+id/vv2"
android:text="1"
android:gravity="center"
android:background="#f00"
android:layout_width="55dp"
android:layout_height="35dp"/>
</LinearLayout>

<LinearLayout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -98,17 +99,17 @@ public void doAttach() {
//假设下方放不下,超出window高度
boolean isTallerThanWindowHeight = (popupInfo.touchPoint.y + getPopupContentView().getMeasuredHeight()) > 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;
}
Expand All @@ -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上方
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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();

Expand Down

0 comments on commit b497e13

Please sign in to comment.