Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/support top right + bottom right bubble directions #25

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions bl/src/main/java/com/daasuu/bl/ArrowDirection.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ public enum ArrowDirection {
RIGHT(1),
TOP(2),
BOTTOM(3),
//CENTER
// CENTER
LEFT_CENTER(4),
RIGHT_CENTER(5),
TOP_CENTER(6),
BOTTOM_CENTER(7);
BOTTOM_CENTER(7),
// HORIZONTAL > RIGHT
TOP_RIGHT(8),
BOTTOM_RIGHT(9);


private int value;
Expand Down
4 changes: 3 additions & 1 deletion bl/src/main/java/com/daasuu/bl/Bubble.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ private void initPath(ArrowDirection arrowDirection, Path path, float strokeWidt
break;
case TOP:
case TOP_CENTER:
case TOP_RIGHT:
if (mCornersRadius <= 0) {
initTopSquarePath(mRect, path, strokeWidth);
break;
Expand Down Expand Up @@ -134,6 +135,7 @@ private void initPath(ArrowDirection arrowDirection, Path path, float strokeWidt
break;
case BOTTOM:
case BOTTOM_CENTER:
case BOTTOM_RIGHT:
if (mCornersRadius <= 0) {
initBottomSquarePath(mRect, path, strokeWidth);
break;
Expand Down Expand Up @@ -335,4 +337,4 @@ private void initBottomSquarePath(RectF rect, Path path, float strokeWidth) {
path.close();
}

}
}
15 changes: 12 additions & 3 deletions bl/src/main/java/com/daasuu/bl/BubbleLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,23 @@ private void initDrawable(int left, int right, int top, int bottom) {
if (right < left || bottom < top) return;

RectF rectF = new RectF(left, top, right, bottom);
float arrowPosition = mArrowPosition;
switch (mArrowDirection) {
case LEFT_CENTER:
case RIGHT_CENTER:
mArrowPosition = (bottom - top) / 2 - mArrowHeight / 2;
arrowPosition = (bottom - top) / 2f - mArrowHeight / 2;
break;
case TOP_CENTER:
case BOTTOM_CENTER:
mArrowPosition = (right - left) / 2 - mArrowWidth / 2;
arrowPosition = (right - left) / 2f - mArrowWidth / 2;
break;
case TOP_RIGHT:
case BOTTOM_RIGHT:
arrowPosition = right - mArrowPosition - mArrowWidth / 2;
default:
break;
}
mBubble = new Bubble(rectF, mArrowWidth, mCornersRadius, mArrowHeight, mArrowPosition,
mBubble = new Bubble(rectF, mArrowWidth, mCornersRadius, mArrowHeight, arrowPosition,
mStrokeWidth, mStrokeColor, mBubbleColor, mArrowDirection);
}

Expand All @@ -107,10 +112,12 @@ private void initPadding() {
break;
case TOP:
case TOP_CENTER:
case TOP_RIGHT:
paddingTop += mArrowHeight;
break;
case BOTTOM:
case BOTTOM_CENTER:
case BOTTOM_RIGHT:
paddingBottom += mArrowHeight;
break;
}
Expand Down Expand Up @@ -139,10 +146,12 @@ private void resetPadding() {
break;
case TOP:
case TOP_CENTER:
case TOP_RIGHT:
paddingTop -= mArrowHeight;
break;
case BOTTOM:
case BOTTOM_CENTER:
case BOTTOM_RIGHT:
paddingBottom -= mArrowHeight;
break;
}
Expand Down
4 changes: 3 additions & 1 deletion bl/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<enum name="right_center" value="5"/>
<enum name="top_center" value="6"/>
<enum name="bottom_center" value="7"/>
<enum name="top_right" value="8"/>
<enum name="bottom_right" value="9"/>
</attr>
</declare-styleable>
</resources>
</resources>
37 changes: 27 additions & 10 deletions sample/src/main/java/com/daasuu/bubblelayout/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package com.daasuu.bubblelayout;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import android.view.Gravity;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.*;

import com.daasuu.bl.ArrowDirection;
import com.daasuu.bl.BubbleLayout;
import com.daasuu.bl.BubblePopupHelper;

import java.util.Random;

import static com.daasuu.bl.ArrowDirection.*;

public class MainActivity extends AppCompatActivity {

private PopupWindow popupWindow;
private ArrowDirection[] randomArrowDirections = {
TOP,
BOTTOM,
TOP_RIGHT,
BOTTOM_RIGHT
};

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -26,20 +34,29 @@ protected void onCreate(Bundle savedInstanceState) {
Button button = (Button) findViewById(R.id.btn_popup);

final BubbleLayout bubbleLayout = (BubbleLayout) LayoutInflater.from(this).inflate(R.layout.layout_sample_popup, null);
bubbleLayout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
final int bubbleWidth = bubbleLayout.getMeasuredWidth();

popupWindow = BubblePopupHelper.create(this, bubbleLayout);
final Random random = new Random();

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int[] location = new int[2];
v.getLocationInWindow(location);
if (random.nextBoolean()) {
bubbleLayout.setArrowDirection(ArrowDirection.TOP);
} else {
bubbleLayout.setArrowDirection(ArrowDirection.BOTTOM);
int xoff = 0;
int yoff = 0;
ArrowDirection direction = randomArrowDirections[random.nextInt(randomArrowDirections.length)];
switch (direction) {
case TOP_RIGHT:
case BOTTOM_RIGHT:
xoff = v.getWidth() - bubbleWidth;
break;
case TOP:
case BOTTOM:
}
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], v.getHeight() + location[1]);
bubbleLayout.setArrowDirection(direction);
bubbleLayout.setArrowPosition(v.getWidth() / 2f);
popupWindow.showAsDropDown(v, xoff, yoff);
}
});

Expand Down
1 change: 1 addition & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
android:id="@+id/btn_popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="32dp"
android:text="Bubble Popup" />

Expand Down