diff --git a/bl/src/main/java/com/daasuu/bl/ArrowDirection.java b/bl/src/main/java/com/daasuu/bl/ArrowDirection.java index 5af2444..883f7cf 100644 --- a/bl/src/main/java/com/daasuu/bl/ArrowDirection.java +++ b/bl/src/main/java/com/daasuu/bl/ArrowDirection.java @@ -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; diff --git a/bl/src/main/java/com/daasuu/bl/Bubble.java b/bl/src/main/java/com/daasuu/bl/Bubble.java index f1c159d..fd0aa09 100644 --- a/bl/src/main/java/com/daasuu/bl/Bubble.java +++ b/bl/src/main/java/com/daasuu/bl/Bubble.java @@ -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; @@ -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; @@ -335,4 +337,4 @@ private void initBottomSquarePath(RectF rect, Path path, float strokeWidth) { path.close(); } -} \ No newline at end of file +} diff --git a/bl/src/main/java/com/daasuu/bl/BubbleLayout.java b/bl/src/main/java/com/daasuu/bl/BubbleLayout.java index fc6c98f..01e95d7 100644 --- a/bl/src/main/java/com/daasuu/bl/BubbleLayout.java +++ b/bl/src/main/java/com/daasuu/bl/BubbleLayout.java @@ -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); } @@ -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; } @@ -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; } diff --git a/bl/src/main/res/values/attrs.xml b/bl/src/main/res/values/attrs.xml index 586a1c4..09c4c00 100644 --- a/bl/src/main/res/values/attrs.xml +++ b/bl/src/main/res/values/attrs.xml @@ -18,6 +18,8 @@ + + - \ No newline at end of file + diff --git a/sample/src/main/java/com/daasuu/bubblelayout/MainActivity.java b/sample/src/main/java/com/daasuu/bubblelayout/MainActivity.java index 831e1db..81a44d4 100644 --- a/sample/src/main/java/com/daasuu/bubblelayout/MainActivity.java +++ b/sample/src/main/java/com/daasuu/bubblelayout/MainActivity.java @@ -1,12 +1,12 @@ 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; @@ -14,9 +14,17 @@ 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) { @@ -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); } }); diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index a6ce6af..73cebc9 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -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" />