Skip to content

Commit

Permalink
设置Bubble属性时不再立即触发刷新,必须主动调用requestUpdateBubble,避免重复计算
Browse files Browse the repository at this point in the history
  • Loading branch information
cpiz committed Oct 31, 2016
1 parent ea1383e commit bad7257
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public int getSuperPaddingBottom() {
}

@Override
public void updateDrawable() {
mBubbleImpl.updateDrawable();
public void requestUpdateBubble() {
mBubbleImpl.requestUpdateBubble();
}
}
44 changes: 2 additions & 42 deletions library/src/main/java/com/cpiz/android/bubbleview/BubbleImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class BubbleImpl implements BubbleStyle {
private View.OnLayoutChangeListener mOnLayoutChangeListener = new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
updateDrawable();
requestUpdateBubble();
}
};

Expand Down Expand Up @@ -76,76 +76,43 @@ void init(View view, Context context, AttributeSet attrs) {
updateDrawable(mParentView.getWidth(), mParentView.getHeight(), false);
}

/**
* 设置箭头朝向
*
* @param arrowDirection 上下左右或者无
*/
@Override
public void setArrowDirection(ArrowDirection arrowDirection) {
mArrowDirection = arrowDirection;
updateDrawable();
}

@Override
public ArrowDirection getArrowDirection() {
return mArrowDirection;
}

/**
* 设置箭头三角形厚度
*
* @param arrowHeight 箭头厚度
*/
@Override
public void setArrowHeight(float arrowHeight) {
mArrowHeight = arrowHeight;
updateDrawable();
}

@Override
public float getArrowHeight() {
return mArrowHeight;
}

/**
* 设置箭头三角形底宽
*
* @param arrowWidth 箭头底边宽度
*/
@Override
public void setArrowWidth(float arrowWidth) {
mArrowWidth = arrowWidth;
updateDrawable();
}

@Override
public float getArrowWidth() {
return mArrowWidth;
}

/**
* 设置箭头在边线上的位置策略
*
* @param policy 箭头位置策略
*/
public void setArrowPosPolicy(ArrowPosPolicy policy) {
mArrowPosPolicy = policy;
updateDrawable();
}

/**
* 设置箭头在所在边线上的偏移距离
* 视 ArrowPosPolicy 而定,为 TargetCenter 或 SelfCenter 时无意义
*
* @param delta 基于箭头位置策略,相应的偏差
* 朝上/下时在X轴方向偏移,朝左/右时在Y轴方向偏移
* 值必须 >0,视 ArrowPosPolicy 从首段或尾端开始偏移
*/
@Override
public void setArrowPosDelta(float delta) {
mArrowPosDelta = delta;
updateDrawable();
}

public ArrowPosPolicy getArrowPosPolicy() {
Expand All @@ -166,14 +133,12 @@ public float getArrowPosDelta() {
public void setArrowTo(int targetViewId) {
mArrowToViewId = targetViewId;
setArrowToRef(null); // 先不设置,在updateDrawable会重新寻找
updateDrawable();
}

@Override
public void setArrowTo(View targetView) {
mArrowToViewId = targetView != null ? targetView.getId() : 0;
setArrowToRef(targetView);
updateDrawable();
}

public View getArrowTo() {
Expand All @@ -188,7 +153,6 @@ public View getArrowTo() {
@Override
public void setFillColor(int fillColor) {
mFillColor = fillColor;
updateDrawable();
}

@Override
Expand All @@ -204,7 +168,6 @@ public int getFillColor() {
@Override
public void setBorderColor(int borderColor) {
mBorderColor = borderColor;
updateDrawable();
}

@Override
Expand All @@ -220,7 +183,6 @@ public int getBorderColor() {
@Override
public void setBorderWidth(float borderWidth) {
mBorderWidth = borderWidth;
updateDrawable();
}

@Override
Expand All @@ -236,7 +198,6 @@ public float getBorderWidth() {
@Override
public void setFillPadding(float fillPadding) {
mFillPadding = fillPadding;
updateDrawable();
}

@Override
Expand All @@ -259,7 +220,6 @@ public void setCornerRadius(float topLeft, float topRight, float bottomRight, fl
mCornerTopRightRadius = topRight;
mCornerBottomRightRadius = bottomRight;
mCornerBottomLeftRadius = bottomLeft;
updateDrawable();
}

@Override
Expand Down Expand Up @@ -403,7 +363,7 @@ void updateDrawable(int width, int height, boolean drawImmediately) {
}

@Override
public void updateDrawable() {
public void requestUpdateBubble() {
updateDrawable(mParentView.getWidth(), mParentView.getHeight(), true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public int getSuperPaddingBottom() {
}

@Override
public void updateDrawable() {
mBubbleImpl.updateDrawable();
public void requestUpdateBubble() {
mBubbleImpl.requestUpdateBubble();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,6 @@ public void showArrowTo(View anchor, RelativePos relativePos, int marginH, int m
final int navigationBarHeight = getDeltaNavigationBarHeight(anchor);
final Rect anchorRect = getRectInWindow(anchor);

ArrowDirection direction = relativePos.getArrowDirection();
mBubbleView.setArrowDirection(direction);
getContentView().measure(
View.MeasureSpec.makeMeasureSpec(screenWidth - 2 * mPadding, View.MeasureSpec.AT_MOST),
View.MeasureSpec.makeMeasureSpec(screenHeight - 2 * mPadding, View.MeasureSpec.AT_MOST));
Expand All @@ -190,6 +188,7 @@ public void showArrowTo(View anchor, RelativePos relativePos, int marginH, int m
showAtLocation(anchor, outProp.gravity, outProp.x, outProp.y);
mBubbleView.setArrowPosPolicy(outProp.arrowPosPolicy);
mBubbleView.setArrowTo(anchor);
mBubbleView.requestUpdateBubble();

if (mDelayMillis > 0) {
setCancelOnLater(mDelayMillis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public int getSuperPaddingBottom() {
}

@Override
public void updateDrawable() {
mBubbleImpl.updateDrawable();
public void requestUpdateBubble() {
mBubbleImpl.requestUpdateBubble();
}
}
33 changes: 29 additions & 4 deletions library/src/main/java/com/cpiz/android/bubbleview/BubbleStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
*/
public interface BubbleStyle {
/**
* 箭头朝向枚举
* 箭头朝向定义
*/
enum ArrowDirection {
/**
* 无箭头
*/
None(0),
Left(1),
Up(2),
Expand Down Expand Up @@ -62,10 +65,28 @@ public boolean isDown() {
}
}

/**
* 箭头位置策略定义
*/
enum ArrowPosPolicy {
/**
* 箭头指向目标View的中心点
*/
TargetCenter(0),

/**
* 箭头从自己的中心点发出
*/
SelfCenter(1),

/**
* 结合setArrowPosDelta,箭头从所在轴的头端开始偏移
*/
SelfBegin(2),

/**
* 结合setArrowPosDelta,箭头从所在轴的尾端开始偏移
*/
SelfEnd(3);

private static final SparseArray<ArrowPosPolicy> intToTypeDict = new SparseArray<>();
Expand Down Expand Up @@ -135,8 +156,8 @@ public static ArrowPosPolicy valueOf(int value) {
* 视 ArrowPosPolicy 而定,为 TargetCenter 或 SelfCenter 时无意义
*
* @param delta 基于箭头位置策略,相应的偏差
* 朝上/下时在X轴方向偏移,朝左/右时在Y轴方向偏移
* 值必须 >0,视 ArrowPosPolicy 从首段或尾端开始偏移
* 朝上/下时在X轴方向偏移,朝左/右时在Y轴方向偏移
* 值必须 >0,视 ArrowPosPolicy 从首段或尾端开始偏移
*/
void setArrowPosDelta(float delta);

Expand Down Expand Up @@ -230,5 +251,9 @@ public static ArrowPosPolicy valueOf(int value) {

int getPaddingBottom();

void updateDrawable();
/**
* 请求刷新UI样式
* 设置好以上属性后,调用该函数进行刷新
*/
void requestUpdateBubble();
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public int getSuperPaddingBottom() {
}

@Override
public void updateDrawable() {
mBubbleImpl.updateDrawable();
public void requestUpdateBubble() {
mBubbleImpl.requestUpdateBubble();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ protected void onCreate(Bundle savedInstanceState) {

@OnClick({R.id.view_anchor})
public void onClick(View view) {
mBubbleTextSample.updateDrawable();
mBubbleTextSample.requestUpdateBubble();
}
}

0 comments on commit bad7257

Please sign in to comment.