Skip to content

Commit

Permalink
Fix calculate auto text position
Browse files Browse the repository at this point in the history
  • Loading branch information
faruktoptas committed Oct 11, 2018
1 parent dc48c3b commit 747ae80
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class MainActivity : BaseActivity() {
FancyShowCaseView.Builder(this)
.focusOn(it)
.title(spanned)
.enableAutoTextPosition()
.build()
.show()
}
Expand Down
28 changes: 26 additions & 2 deletions library/src/main/java/me/toptas/fancyshowcase/Calculator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ import android.content.Context
import android.os.Build
import android.util.DisplayMetrics
import android.view.View
import android.widget.RelativeLayout

/**
* Geometric calculations for position, size and radius
*/

class Calculator(activity: Activity, focusShape: FocusShape, view: View?, radiusFactor: Double,
fitSystemWindows: Boolean) {
class Calculator(activity: Activity,
focusShape: FocusShape,
view: View?,
radiusFactor: Double,
fitSystemWindows: Boolean) {

/**
* @return Width of background bitmap
Expand Down Expand Up @@ -131,6 +135,26 @@ import android.view.View
mHasFocus = true
}

fun calcAutoTextPosition(view: View) {
val top = roundRectTop(0, 0.0)
val bottom = roundRectBottom(0, 0.0)

val spaceAbove = top.toInt()
val spaceBelow = bitmapHeight - bottom.toInt()
val params = view.layoutParams as RelativeLayout.LayoutParams

if (spaceAbove > spaceBelow) {
params.bottomMargin = bitmapHeight - (circleCenterY + viewRadius)
params.topMargin = 0
params.height = top.toInt()
} else {
params.topMargin = circleCenterY + viewRadius
params.bottomMargin = 0
params.height = (bitmapHeight - top).toInt()
}
view.layoutParams = params
}

/**
* @return True if there is a view to focus
*/
Expand Down
17 changes: 15 additions & 2 deletions library/src/main/java/me/toptas/fancyshowcase/FancyShowCaseView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class FancyShowCaseView : FrameLayout, ViewTreeObserver.OnGlobalLayoutListener {
private var mFocusShape: FocusShape = FocusShape.CIRCLE
private var viewInflateListener: OnViewInflateListener? = null
private var delay: Long = 0
private var autoPosText = false
private val mAnimationDuration = 400
private var mFocusAnimationMaxValue = 20
private var mFocusAnimationStep: Int = 1
Expand Down Expand Up @@ -183,7 +184,8 @@ class FancyShowCaseView : FrameLayout, ViewTreeObserver.OnGlobalLayoutListener {
_animationEnabled: Boolean,
_focusAnimationMaxValue: Int,
_focusAnimationStep: Int,
_delay: Long) : super(_activity) {
_delay: Long,
_autoPosText: Boolean) : super(_activity) {

requireNotNull(_activity)
id = _id
Expand Down Expand Up @@ -219,6 +221,7 @@ class FancyShowCaseView : FrameLayout, ViewTreeObserver.OnGlobalLayoutListener {
mFocusAnimationMaxValue = _focusAnimationMaxValue
mFocusAnimationStep = _focusAnimationStep
delay = _delay
autoPosText = _autoPosText

initializeParameters()
}
Expand Down Expand Up @@ -470,6 +473,10 @@ class FancyShowCaseView : FrameLayout, ViewTreeObserver.OnGlobalLayoutListener {
} else {
textView.text = title
}

if (autoPosText) {
calculator?.calcAutoTextPosition(textView)
}
}
})

Expand Down Expand Up @@ -611,6 +618,7 @@ class FancyShowCaseView : FrameLayout, ViewTreeObserver.OnGlobalLayoutListener {
private var mFocusAnimationMaxValue = 20
private var mFocusAnimationStep = 1
private var delay: Long = 0
private var autoPosText = false


/**
Expand Down Expand Up @@ -867,6 +875,11 @@ class FancyShowCaseView : FrameLayout, ViewTreeObserver.OnGlobalLayoutListener {
return this
}

fun enableAutoTextPosition(): Builder {
autoPosText = true
return this
}

/**
* builds the builder
*
Expand All @@ -877,7 +890,7 @@ class FancyShowCaseView : FrameLayout, ViewTreeObserver.OnGlobalLayoutListener {
focusCircleRadiusFactor, mBackgroundColor, mFocusBorderColor, mFocusBorderSize, mCustomViewRes, viewInflateListener,
mEnterAnimation, mExitAnimation, mAnimationListener, mCloseOnTouch, mEnableTouchOnFocusedView, fitSystemWindows, mFocusShape, mDismissListener, mRoundRectRadius,
mFocusPositionX, mFocusPositionY, mFocusCircleRadius, mFocusRectangleWidth, mFocusRectangleHeight, focusAnimationEnabled,
mFocusAnimationMaxValue, mFocusAnimationStep, delay)
mFocusAnimationMaxValue, mFocusAnimationStep, delay, autoPosText)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright (c) 2018. Faruk Toptaş
~
~ Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -24,8 +23,9 @@
<TextView
android:id="@+id/fscv_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:gravity="center_vertical"
android:padding="10dp"
android:textSize="24sp"
tools:text="Title" />
Expand Down

0 comments on commit 747ae80

Please sign in to comment.