Skip to content

Commit

Permalink
BubbleView的ArrowDirection默认属性设置为Auto
Browse files Browse the repository at this point in the history
支持BubblePopupWindow设置margin为负数时也能正确显示箭头
  • Loading branch information
cpiz committed Oct 31, 2016
1 parent 23399c2 commit cf05c28
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,6 @@ public int getOpacity() {
private void updatePath(Shape shape, Path path) {
path.reset();
switch (mArrowDirection) {
case None:
buildWithNoneArrow(shape, path);
break;
case Up:
buildWithUpArrow(shape, path);
break;
Expand All @@ -265,6 +262,9 @@ private void updatePath(Shape shape, Path path) {
case Right:
buildWithRightArrow(shape, path);
break;
default:
buildWithNoneArrow(shape, path);
break;
}
}

Expand Down
19 changes: 14 additions & 5 deletions library/src/main/java/com/cpiz/android/bubbleview/BubbleImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class BubbleImpl implements BubbleStyle {
private View mParentView;
private BubbleCallback mHolderCallback;
private BubbleDrawable mBubbleDrawable = new BubbleDrawable();
private ArrowDirection mArrowDirection = ArrowDirection.None;
private ArrowDirection mArrowDirection = ArrowDirection.Auto;
private ArrowDirection mDrawableArrowDirection = ArrowDirection.None;
private ArrowPosPolicy mArrowPosPolicy = ArrowPosPolicy.TargetCenter;
private WeakReference<View> mArrowToViewRef = null;
private int mArrowToViewId = 0;
Expand Down Expand Up @@ -53,7 +54,7 @@ void init(View view, Context context, AttributeSet attrs) {

if (attrs != null) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BubbleStyle);
mArrowDirection = ArrowDirection.valueOf(ta.getInt(R.styleable.BubbleStyle_bb_arrowDirection, ArrowDirection.None.getValue()));
mArrowDirection = ArrowDirection.valueOf(ta.getInt(R.styleable.BubbleStyle_bb_arrowDirection, ArrowDirection.Auto.getValue()));
mArrowHeight = ta.getDimension(R.styleable.BubbleStyle_bb_arrowHeight, dp2px(6));
mArrowWidth = ta.getDimension(R.styleable.BubbleStyle_bb_arrowWidth, dp2px(10));
mArrowPosPolicy = ArrowPosPolicy.valueOf(ta.getInt(R.styleable.BubbleStyle_bb_arrowPosPolicy, ArrowPosPolicy.TargetCenter.getValue()));
Expand Down Expand Up @@ -267,7 +268,7 @@ public void setPadding(int left, int top, int right, int bottom) {
}

mPaddingLeftOffset = mPaddingTopOffset = mPaddingRightOffset = mPaddingBottomOffset = 0;
switch (mArrowDirection) {
switch (mDrawableArrowDirection) {
case Left:
mPaddingLeftOffset += mArrowHeight;
break;
Expand All @@ -280,6 +281,10 @@ public void setPadding(int left, int top, int right, int bottom) {
case Down:
mPaddingBottomOffset += mArrowHeight;
break;
case Auto:
case None:
default:
break;
}

mHolderCallback.setSuperPadding(
Expand Down Expand Up @@ -325,14 +330,18 @@ void updateDrawable(int width, int height, boolean drawImmediately) {
setArrowToRef(arrowToView);
}

mDrawableArrowDirection = mArrowDirection;
if (arrowToView != null) {
arrowToView.getLocationOnScreen(mLocation);
mRectTo.set(mLocation[0], mLocation[1],
mLocation[0] + arrowToView.getWidth(), mLocation[1] + arrowToView.getHeight());

mParentView.getLocationOnScreen(mLocation);
mRectSelf.set(mLocation[0], mLocation[1], mLocation[0] + width, mLocation[1] + height);
mArrowDirection = getAutoArrowDirection(mRectSelf, mRectTo);

if (mDrawableArrowDirection == ArrowDirection.Auto) {
mDrawableArrowDirection = getAutoArrowDirection(mRectSelf, mRectTo);
}

arrowToOffsetX = mRectTo.centerX() - mRectSelf.centerX();
arrowToOffsetY = mRectTo.centerY() - mRectSelf.centerY();
Expand All @@ -346,7 +355,7 @@ void updateDrawable(int width, int height, boolean drawImmediately) {
mBubbleDrawable.setBorderWidth(mBorderWidth);
mBubbleDrawable.setFillPadding(mFillPadding);
mBubbleDrawable.setBorderColor(mBorderColor);
mBubbleDrawable.setArrowDirection(mArrowDirection);
mBubbleDrawable.setArrowDirection(mDrawableArrowDirection);
mBubbleDrawable.setArrowPosPolicy(mArrowPosPolicy);
mBubbleDrawable.setArrowTo(arrowToOffsetX, arrowToOffsetY);
mBubbleDrawable.setArrowPosDelta(mArrowPosDelta);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@ public void showArrowTo(View anchor, RelativePos relativePos, int marginH, int m
if (contentWidth > outProp.maxWidth) {
setWidth(outProp.maxWidth);
}
showAtLocation(anchor, outProp.gravity, outProp.x, outProp.y);
mBubbleView.setArrowDirection(outProp.direction);
mBubbleView.setArrowPosPolicy(outProp.arrowPosPolicy);
mBubbleView.setArrowTo(anchor);
mBubbleView.setArrowPosDelta(mArrowPosDelta);
mBubbleView.requestUpdateBubble();
showAtLocation(anchor, outProp.gravity, outProp.x, outProp.y);

if (mDelayMillis > 0) {
setCancelOnLater(mDelayMillis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ public interface BubbleStyle {
* 箭头朝向定义
*/
enum ArrowDirection {

/**
* 无箭头
*/
None(0),
None(-1),
/**
* 自动确定指向
*/
Auto(0),
Left(1),
Up(2),
Right(3),
Expand All @@ -44,7 +49,7 @@ public int getValue() {
public static ArrowDirection valueOf(int value) {
ArrowDirection type = intToTypeDict.get(value);
if (type == null)
return ArrowDirection.None;
return ArrowDirection.Auto;
return type;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public void onCheckedChanged(RadioGroup group, int checkedId) {
mSeekbarMarginH.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mMarginH = dp2px(progress);
mMarginH = dp2px(progress - 10);
showPopupBubble();
}

Expand All @@ -218,12 +218,12 @@ public void onStartTrackingTouch(SeekBar seekBar) {
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
mMarginH = dp2px(mSeekbarMarginH.getProgress());
mMarginH = dp2px(mSeekbarMarginH.getProgress() - 10);

mSeekbarMarginV.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mMarginV = dp2px(progress);
mMarginV = dp2px(progress - 10);
showPopupBubble();
}

Expand All @@ -235,7 +235,7 @@ public void onStartTrackingTouch(SeekBar seekBar) {
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
mMarginV = dp2px(mSeekbarMarginV.getProgress());
mMarginV = dp2px(mSeekbarMarginV.getProgress() - 10);

mSeekbarPadding.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
Expand Down
4 changes: 2 additions & 2 deletions sample/src/main/res/layout/activity_popup_window_sample.xml
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:max="50"
android:progress="0"/>
android:progress="10"/>

<TextView
android:layout_width="wrap_content"
Expand All @@ -256,7 +256,7 @@
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:max="50"
android:progress="0"/>
android:progress="10"/>
</LinearLayout>

<View
Expand Down

0 comments on commit cf05c28

Please sign in to comment.