From 3ad6b626edfc81c02ed404c364db9189d0cba772 Mon Sep 17 00:00:00 2001 From: MasayukiSuda Date: Tue, 14 Jun 2016 11:40:37 +0900 Subject: [PATCH] add ArrowDirection enum --- .../java/com/daasuu/bl/ArrowDirection.java | 30 +++++++++++ bl/src/main/java/com/daasuu/bl/Bubble.java | 14 ++--- .../main/java/com/daasuu/bl/BubbleLayout.java | 53 +++++-------------- build.gradle | 2 +- .../com/daasuu/bubblelayout/MainActivity.java | 7 ++- 5 files changed, 56 insertions(+), 50 deletions(-) create mode 100644 bl/src/main/java/com/daasuu/bl/ArrowDirection.java diff --git a/bl/src/main/java/com/daasuu/bl/ArrowDirection.java b/bl/src/main/java/com/daasuu/bl/ArrowDirection.java new file mode 100644 index 0000000..470c26c --- /dev/null +++ b/bl/src/main/java/com/daasuu/bl/ArrowDirection.java @@ -0,0 +1,30 @@ +package com.daasuu.bl; + +/** + * Created by sudamasayuki on 16/06/14. + */ +public enum ArrowDirection { + LEFT(0), + RIGHT(1), + TOP(2), + BOTTOM(3); + + private int value; + + ArrowDirection(int value) { + this.value = value; + } + + public static ArrowDirection fromInt(int value) { + for (ArrowDirection arrowDirection : ArrowDirection.values()) { + if (value == arrowDirection.getValue()) { + return arrowDirection; + } + } + return LEFT; + } + + public int getValue() { + return value; + } +} diff --git a/bl/src/main/java/com/daasuu/bl/Bubble.java b/bl/src/main/java/com/daasuu/bl/Bubble.java index 67fca57..e7dbcb1 100644 --- a/bl/src/main/java/com/daasuu/bl/Bubble.java +++ b/bl/src/main/java/com/daasuu/bl/Bubble.java @@ -25,7 +25,7 @@ class Bubble extends Drawable { private float mArrowPosition; private float mStrokeWidth; - public Bubble(RectF rect, float arrowWidth, float cornersRadius, float arrowHeight, float arrowPosition, float strokeWidth, int strokeColor, int bubbleColor, @BubbleLayout.ArrowDirection int arrowDirection) { + public Bubble(RectF rect, float arrowWidth, float cornersRadius, float arrowHeight, float arrowPosition, float strokeWidth, int strokeColor, int bubbleColor, ArrowDirection arrowDirection) { this.mRect = rect; this.mArrowWidth = arrowWidth; @@ -86,9 +86,9 @@ public int getIntrinsicHeight() { return (int) mRect.height(); } - private void initPath(@BubbleLayout.ArrowDirection int mArrowDirection, Path path, float strokeWidth) { - switch (mArrowDirection) { - case BubbleLayout.ARROW_DIRECTION_LEFT: + private void initPath(ArrowDirection arrowDirection, Path path, float strokeWidth) { + switch (arrowDirection) { + case LEFT: if (mCornersRadius <= 0) { initLeftSquarePath(mRect, path, strokeWidth); break; @@ -101,7 +101,7 @@ private void initPath(@BubbleLayout.ArrowDirection int mArrowDirection, Path pat initLeftRoundedPath(mRect, path, strokeWidth); break; - case BubbleLayout.ARROW_DIRECTION_TOP: + case TOP: if (mCornersRadius <= 0) { initTopSquarePath(mRect, path, strokeWidth); break; @@ -115,7 +115,7 @@ private void initPath(@BubbleLayout.ArrowDirection int mArrowDirection, Path pat initTopRoundedPath(mRect, path, strokeWidth); break; - case BubbleLayout.ARROW_DIRECTION_RIGHT: + case RIGHT: if (mCornersRadius <= 0) { initRightSquarePath(mRect, path, strokeWidth); break; @@ -129,7 +129,7 @@ private void initPath(@BubbleLayout.ArrowDirection int mArrowDirection, Path pat initRightRoundedPath(mRect, path, strokeWidth); break; - case BubbleLayout.ARROW_DIRECTION_BOTTOM: + case BOTTOM: if (mCornersRadius <= 0) { initBottomSquarePath(mRect, path, strokeWidth); break; diff --git a/bl/src/main/java/com/daasuu/bl/BubbleLayout.java b/bl/src/main/java/com/daasuu/bl/BubbleLayout.java index 37b17dc..9d90a15 100644 --- a/bl/src/main/java/com/daasuu/bl/BubbleLayout.java +++ b/bl/src/main/java/com/daasuu/bl/BubbleLayout.java @@ -5,7 +5,6 @@ import android.graphics.Canvas; import android.graphics.Color; import android.graphics.RectF; -import android.support.annotation.IntDef; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.widget.FrameLayout; @@ -18,19 +17,7 @@ public class BubbleLayout extends FrameLayout { public static float DEFAULT_STROKE_WIDTH = -1; - public static final int ARROW_DIRECTION_LEFT = 0; - public static final int ARROW_DIRECTION_RIGHT = 1; - public static final int ARROW_DIRECTION_TOP = 2; - public static final int ARROW_DIRECTION_BOTTOM = 3; - - @IntDef(value = { - ARROW_DIRECTION_LEFT, ARROW_DIRECTION_RIGHT, ARROW_DIRECTION_TOP, ARROW_DIRECTION_BOTTOM - }) - @interface ArrowDirection { - } - - @ArrowDirection - private int mArrowDirection; + private ArrowDirection mArrowDirection; private Bubble mBubble; private float mArrowWidth; @@ -66,22 +53,8 @@ public BubbleLayout(Context context, AttributeSet attrs, int defStyleAttr) { a.getDimension(R.styleable.BubbleLayout_bl_strokeWidth, DEFAULT_STROKE_WIDTH); mStrokeColor = a.getColor(R.styleable.BubbleLayout_bl_strokeColor, Color.GRAY); - int location = a.getInt(R.styleable.BubbleLayout_bl_arrowDirection, ARROW_DIRECTION_LEFT); - switch (location) { - case ARROW_DIRECTION_RIGHT: - mArrowDirection = ARROW_DIRECTION_RIGHT; - break; - case ARROW_DIRECTION_TOP: - mArrowDirection = ARROW_DIRECTION_TOP; - break; - case ARROW_DIRECTION_BOTTOM: - mArrowDirection = ARROW_DIRECTION_BOTTOM; - break; - case ARROW_DIRECTION_LEFT: - default: - mArrowDirection = ARROW_DIRECTION_LEFT; - break; - } + int location = a.getInt(R.styleable.BubbleLayout_bl_arrowDirection, ArrowDirection.LEFT.getValue()); + mArrowDirection = ArrowDirection.fromInt(location); a.recycle(); initPadding(); @@ -113,16 +86,16 @@ private void initPadding() { int paddingTop = getPaddingTop(); int paddingBottom = getPaddingBottom(); switch (mArrowDirection) { - case ARROW_DIRECTION_LEFT: + case LEFT: paddingLeft += mArrowWidth; break; - case ARROW_DIRECTION_RIGHT: + case RIGHT: paddingRight += mArrowWidth; break; - case ARROW_DIRECTION_TOP: + case TOP: paddingTop += mArrowHeight; break; - case ARROW_DIRECTION_BOTTOM: + case BOTTOM: paddingBottom += mArrowHeight; break; } @@ -141,16 +114,16 @@ private void resetPadding() { int paddingTop = getPaddingTop(); int paddingBottom = getPaddingBottom(); switch (mArrowDirection) { - case ARROW_DIRECTION_LEFT: + case LEFT: paddingLeft -= mArrowWidth; break; - case ARROW_DIRECTION_RIGHT: + case RIGHT: paddingRight -= mArrowWidth; break; - case ARROW_DIRECTION_TOP: + case TOP: paddingTop -= mArrowHeight; break; - case ARROW_DIRECTION_BOTTOM: + case BOTTOM: paddingBottom -= mArrowHeight; break; } @@ -168,7 +141,7 @@ static float convertDpToPixel(float dp, Context context) { return dp * (metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT); } - public BubbleLayout setArrowDirection(@ArrowDirection int arrowDirection) { + public BubbleLayout setArrowDirection(ArrowDirection arrowDirection) { resetPadding(); mArrowDirection = arrowDirection; initPadding(); @@ -221,7 +194,7 @@ public BubbleLayout setStrokeColor(int strokeColor) { return this; } - public int getArrowDirection() { + public ArrowDirection getArrowDirection() { return mArrowDirection; } diff --git a/build.gradle b/build.gradle index 6b25711..eea0f5a 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.1.0' + classpath 'com.android.tools.build:gradle:2.1.2' classpath 'com.novoda:bintray-release:0.3.4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/sample/src/main/java/com/daasuu/bubblelayout/MainActivity.java b/sample/src/main/java/com/daasuu/bubblelayout/MainActivity.java index 3316b4a..4226481 100644 --- a/sample/src/main/java/com/daasuu/bubblelayout/MainActivity.java +++ b/sample/src/main/java/com/daasuu/bubblelayout/MainActivity.java @@ -7,8 +7,11 @@ import android.view.View; import android.widget.Button; import android.widget.PopupWindow; + +import com.daasuu.bl.ArrowDirection; import com.daasuu.bl.BubbleLayout; import com.daasuu.bl.BubblePopupHelper; + import java.util.Random; public class MainActivity extends AppCompatActivity { @@ -32,9 +35,9 @@ public void onClick(View v) { int[] location = new int[2]; v.getLocationInWindow(location); if (random.nextBoolean()) { - bubbleLayout.setArrowDirection(BubbleLayout.ARROW_DIRECTION_TOP); + bubbleLayout.setArrowDirection(ArrowDirection.TOP); } else { - bubbleLayout.setArrowDirection(BubbleLayout.ARROW_DIRECTION_BOTTOM); + bubbleLayout.setArrowDirection(ArrowDirection.BOTTOM); } popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], v.getHeight() + location[1]); }