Skip to content

Commit b10c70d

Browse files
committed
Added "arrowDirection" option. Useful with Gravity.CENTER #14
1 parent d0f5b3a commit b10c70d

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

build.gradle

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.1.2'
8+
/*
9+
Before changing "Android Gradle Plugin" version, check he last estable version
10+
https://developer.android.com/studio/releases/gradle-plugin.html#revisions
11+
*/
12+
classpath 'com.android.tools.build:gradle:2.1.0'
913
// NOTE: Do not place your application dependencies here; they belong
1014
// in the individual module build.gradle files
1115
}

library/src/main/java/io/github/douglasjunior/androidSimpleTooltip/ArrowDrawable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838
* ArrowDrawable
3939
* Created by douglas on 09/05/16.
4040
*/
41-
class ArrowDrawable extends ColorDrawable {
41+
public class ArrowDrawable extends ColorDrawable {
4242

43-
public static final int LEFT = 0, TOP = 1, RIGHT = 2, BOTTOM = 3;
43+
public static final int LEFT = 0, TOP = 1, RIGHT = 2, BOTTOM = 3, AUTO = 4;
4444

4545
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
4646
private final int mBackgroundColor;

library/src/main/java/io/github/douglasjunior/androidSimpleTooltip/SimpleTooltip.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public class SimpleTooltip implements PopupWindow.OnDismissListener {
8282
private OnShowListener mOnShowListener;
8383
private PopupWindow mPopupWindow;
8484
private final int mGravity;
85+
private final int mArrowDirection;
8586
private final boolean mDismissOnInsideTouch;
8687
private final boolean mDismissOnOutsideTouch;
8788
private final boolean mModal;
@@ -112,6 +113,7 @@ public class SimpleTooltip implements PopupWindow.OnDismissListener {
112113
private SimpleTooltip(Builder builder) {
113114
mContext = builder.context;
114115
mGravity = builder.gravity;
116+
mArrowDirection = builder.arrowDirection;
115117
mDismissOnInsideTouch = builder.dismissOnInsideTouch;
116118
mDismissOnOutsideTouch = builder.dismissOnOutsideTouch;
117119
mModal = builder.modal;
@@ -227,23 +229,25 @@ private void configContentView() {
227229

228230
LinearLayout linearLayout = new LinearLayout(mContext);
229231
linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
230-
linearLayout.setOrientation(mGravity == Gravity.START || mGravity == Gravity.END ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
232+
linearLayout.setOrientation(mArrowDirection == ArrowDrawable.LEFT || mArrowDirection == ArrowDrawable.RIGHT ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL);
231233
int layoutPadding = (int) (mAnimated ? mAnimationPadding : 0);
232234
linearLayout.setPadding(layoutPadding, layoutPadding, layoutPadding, layoutPadding);
233235

234236
if (mShowArrow) {
235237
mArrowView = new ImageView(mContext);
236238
mArrowView.setImageDrawable(mArrowDrawable);
237239
LinearLayout.LayoutParams arrowLayoutParams;
238-
if (mGravity == Gravity.TOP || mGravity == Gravity.BOTTOM) {
240+
241+
if (mArrowDirection == ArrowDrawable.TOP || mArrowDirection == ArrowDrawable.BOTTOM) {
239242
arrowLayoutParams = new LinearLayout.LayoutParams((int) mArrowWidth, (int) mArrowHeight, 0);
240243
} else {
241244
arrowLayoutParams = new LinearLayout.LayoutParams((int) mArrowHeight, (int) mArrowWidth, 0);
242245
}
246+
243247
arrowLayoutParams.gravity = Gravity.CENTER;
244248
mArrowView.setLayoutParams(arrowLayoutParams);
245249

246-
if (mGravity == Gravity.TOP || mGravity == Gravity.START) {
250+
if (mArrowDirection == ArrowDrawable.BOTTOM || mArrowDirection == ArrowDrawable.RIGHT) {
247251
linearLayout.addView(mContentView);
248252
linearLayout.addView(mArrowView);
249253
} else {
@@ -388,7 +392,7 @@ public void onGlobalLayout() {
388392
RectF achorRect = SimpleTooltipUtils.calculeRectOnScreen(mAnchorView);
389393
RectF contentViewRect = SimpleTooltipUtils.calculeRectOnScreen(mContentLayout);
390394
float x, y;
391-
if (mGravity == Gravity.BOTTOM || mGravity == Gravity.TOP) {
395+
if (mArrowDirection == ArrowDrawable.TOP || mArrowDirection == ArrowDrawable.BOTTOM) {
392396
x = mContentLayout.getPaddingLeft() + SimpleTooltipUtils.pxFromDp(2);
393397
float centerX = (contentViewRect.width() / 2f) - (mArrowView.getWidth() / 2f);
394398
float newX = centerX - (contentViewRect.centerX() - achorRect.centerX());
@@ -400,7 +404,7 @@ public void onGlobalLayout() {
400404
}
401405
}
402406
y = mArrowView.getTop();
403-
y = y + (mGravity == Gravity.TOP ? -1 : +1);
407+
y = y + (mArrowDirection == ArrowDrawable.BOTTOM ? -1 : +1);
404408
} else {
405409
y = mContentLayout.getPaddingTop() + SimpleTooltipUtils.pxFromDp(2);
406410
float centerY = (contentViewRect.height() / 2f) - (mArrowView.getHeight() / 2f);
@@ -413,7 +417,7 @@ public void onGlobalLayout() {
413417
}
414418
}
415419
x = mArrowView.getLeft();
416-
x = x + (mGravity == Gravity.START ? -1 : +1);
420+
x = x + (mArrowDirection == ArrowDrawable.RIGHT ? -1 : +1);
417421
}
418422
SimpleTooltipUtils.setX(mArrowView, (int) x);
419423
SimpleTooltipUtils.setY(mArrowView, (int) y);
@@ -519,6 +523,7 @@ public static class Builder {
519523
private int textViewId = android.R.id.text1;
520524
private CharSequence text = "";
521525
private View anchorView;
526+
private int arrowDirection = ArrowDrawable.AUTO;
522527
private int gravity = Gravity.BOTTOM;
523528
private boolean transparentOverlay = true;
524529
private float maxWidth;
@@ -559,10 +564,6 @@ public SimpleTooltip build() throws IllegalArgumentException {
559564
if (arrowColor == 0) {
560565
arrowColor = SimpleTooltipUtils.getColor(context, mDefaultArrowColorRes);
561566
}
562-
if (arrowDrawable == null) {
563-
int arrowDirection = SimpleTooltipUtils.tooltipGravityToArrowDirection(gravity);
564-
arrowDrawable = new ArrowDrawable(arrowColor, arrowDirection);
565-
}
566567
if (margin < 0) {
567568
margin = context.getResources().getDimension(mDefaultMarginRes);
568569
}
@@ -579,6 +580,10 @@ public SimpleTooltip build() throws IllegalArgumentException {
579580
animated = false;
580581
}
581582
if (showArrow) {
583+
if (arrowDirection == ArrowDrawable.AUTO)
584+
arrowDirection = SimpleTooltipUtils.tooltipGravityToArrowDirection(gravity);
585+
if (arrowDrawable == null)
586+
arrowDrawable = new ArrowDrawable(arrowColor, arrowDirection);
582587
if (arrowWidth == 0)
583588
arrowWidth = context.getResources().getDimension(mDefaultArrowWidthRes);
584589
if (arrowHeight == 0)
@@ -734,6 +739,20 @@ public Builder gravity(int gravity) {
734739
return this;
735740
}
736741

742+
/**
743+
* <div class="pt">Define a direção em que a seta será criada.
744+
* As opções existentes são <tt>ArrowDrawable.LEFT</tt>, <tt>ArrowDrawable.TOP</tt>, <tt>ArrowDrawable.RIGHT</tt>, <tt>ArrowDrawable.BOTTOM</tt> e <tt>ArrowDrawable.AUTO</tt>.
745+
* O padrão é <tt>ArrowDrawable.AUTO</tt>. <br>
746+
* Esta opção deve ser utilizada em conjunto com <tt>Builder#showArrow(true)</tt>.</div>
747+
*
748+
* @param arrowDirection <div class="pt">direção em que a seta será criada.</div>
749+
* @return this
750+
*/
751+
public Builder arrowDirection(int arrowDirection) {
752+
this.arrowDirection = arrowDirection;
753+
return this;
754+
}
755+
737756
/**
738757
* <div class="pt">Define se o fundo da tela será escurecido ou transparente enquanto o tooltip estiver aberto. Padrão é <tt>true</tt>.</div>
739758
*

sample/src/main/res/layout/activity_main.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
<LinearLayout
2828
android:layout_width="match_parent"
2929
android:layout_height="wrap_content"
30-
android:orientation="vertical">
30+
android:orientation="vertical"
31+
android:paddingBottom="80dp">
3132

3233
<Button
3334
android:id="@+id/btn_simple"
@@ -110,7 +111,7 @@
110111
android:id="@+id/btn_center"
111112
android:layout_width="wrap_content"
112113
android:layout_height="wrap_content"
113-
android:layout_gravity="center"
114+
android:layout_gravity="end"
114115
android:text="@string/btn_center" />
115116
</LinearLayout>
116117
</ScrollView>

0 commit comments

Comments
 (0)