Skip to content

Commit

Permalink
add ArrowDirection enum
Browse files Browse the repository at this point in the history
  • Loading branch information
MasayukiSuda committed Jun 14, 2016
1 parent adbbd51 commit 3ad6b62
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 50 deletions.
30 changes: 30 additions & 0 deletions bl/src/main/java/com/daasuu/bl/ArrowDirection.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
14 changes: 7 additions & 7 deletions bl/src/main/java/com/daasuu/bl/Bubble.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down
53 changes: 13 additions & 40 deletions bl/src/main/java/com/daasuu/bl/BubbleLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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();
Expand Down Expand Up @@ -221,7 +194,7 @@ public BubbleLayout setStrokeColor(int strokeColor) {
return this;
}

public int getArrowDirection() {
public ArrowDirection getArrowDirection() {
return mArrowDirection;
}

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]);
}
Expand Down

0 comments on commit 3ad6b62

Please sign in to comment.