Skip to content

Commit

Permalink
added OnMaxMinFontSizeReached and implemented behavior to stop resizi…
Browse files Browse the repository at this point in the history
…ng when min/max reached
  • Loading branch information
mzorz committed Nov 27, 2019
1 parent 844ba01 commit 7c5a523
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import com.automattic.photoeditor.gesture.MultiTouchListener.OnMultiTouchListene
import com.automattic.photoeditor.gesture.TextViewSizeAwareTouchListener
import com.automattic.photoeditor.gesture.TextViewSizeAwareTouchListener.OnDeleteViewListener
import com.automattic.photoeditor.util.BitmapUtil
import com.automattic.photoeditor.views.AutoResizeTextView
import com.automattic.photoeditor.views.PhotoEditorView
import com.automattic.photoeditor.views.ViewType
import com.automattic.photoeditor.views.ViewType.EMOJI
Expand Down Expand Up @@ -245,7 +246,7 @@ class PhotoEditor private constructor(builder: Builder) :
fun addText(text: String, colorCodeTextView: Int, textTypeface: Typeface? = null, fontSizeSp: Float = 18f) {
brushDrawingView.brushDrawingMode = false
getLayout(ViewType.TEXT)?.apply {
val textInputTv = findViewById<TextView>(R.id.tvPhotoEditorText)
val textInputTv = findViewById<AutoResizeTextView>(R.id.tvPhotoEditorText)

textInputTv.text = text
textInputTv.setTextColor(colorCodeTextView)
Expand All @@ -269,6 +270,7 @@ class PhotoEditor private constructor(builder: Builder) :
}
)
setOnTouchListener(touchListenerInstance)
textInputTv.setMaxMinFontSizeReachedListener(touchListenerInstance)
addViewToParent(this, ViewType.TEXT)

// now open TextEditor right away
Expand Down Expand Up @@ -333,7 +335,7 @@ class PhotoEditor private constructor(builder: Builder) :
fun addEmoji(emojiTypeface: Typeface?, emojiName: String) {
brushDrawingView.brushDrawingMode = false
getLayout(ViewType.EMOJI)?.apply {
val emojiTextView = findViewById<TextView>(R.id.tvPhotoEditorText)
val emojiTextView = findViewById<AutoResizeTextView>(R.id.tvPhotoEditorText)

if (emojiTypeface != null) {
emojiTextView.typeface = emojiTypeface
Expand Down Expand Up @@ -364,6 +366,7 @@ class PhotoEditor private constructor(builder: Builder) :

val touchListenerInstance = newTextViewSizeAwareTouchListener
setOnTouchListener(touchListenerInstance)
emojiTextView.setMaxMinFontSizeReachedListener(touchListenerInstance)
addViewToParent(this, ViewType.EMOJI)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import com.automattic.photoeditor.OnPhotoEditorListener
import com.automattic.photoeditor.views.AutoResizeTextView.OnMaxMinFontSizeReached
import com.automattic.photoeditor.views.ViewType

class TextViewSizeAwareTouchListener(
Expand All @@ -14,7 +15,7 @@ class TextViewSizeAwareTouchListener(
private val deleteView: View?,
private val onDeleteViewListener: OnDeleteViewListener?,
private val onPhotoEditorListener: OnPhotoEditorListener?
) : View.OnTouchListener {
) : View.OnTouchListener, OnMaxMinFontSizeReached {
private var originX = 0f
private var originY = 0f
private var secondOriginX = 0f
Expand All @@ -33,6 +34,9 @@ class TextViewSizeAwareTouchListener(
private var onGestureControl: OnGestureControl? = null
private val gestureListener: GestureDetector

private var maxFontSizeReached: Boolean = false
private var minFontSizeReached: Boolean = false

init {
rotationDetector = RotationGestureDetector()
if (deleteView != null) {
Expand Down Expand Up @@ -72,6 +76,25 @@ class TextViewSizeAwareTouchListener(
}
}

override fun onMaxFontSizeReached() {
maxFontSizeReached = true
minFontSizeReached = false
}

override fun onMinFontSizeReached() {
minFontSizeReached = true
maxFontSizeReached = false
}

override fun onFontSizeChangedWithinMinMaxRange() {
minFontSizeReached = false
maxFontSizeReached = false
}

private fun isMinOrMaxFontSizeReached(): Boolean {
return minFontSizeReached || maxFontSizeReached
}

@SuppressLint("ClickableViewAccessibility")
override fun onTouch(view: View, event: MotionEvent): Boolean {
rotationDetector.onTouchEvent(view, event)
Expand Down Expand Up @@ -113,24 +136,26 @@ class TextViewSizeAwareTouchListener(
var newHeight = (diffY * view.measuredHeight.toFloat() / lastDiffY).toInt()

if (newWidth > minWidth && newHeight > minHeight) {
val parentWidth = (view.parent as View).width
val parentHeight = (view.parent as View).height
val params = view.layoutParams

if (newWidth + view.x > parentWidth) {
newWidth = parentWidth - view.x.toInt()
if (!isMinOrMaxFontSizeReached()) {
val parentWidth = (view.parent as View).width
val parentHeight = (view.parent as View).height
val params = view.layoutParams

if (newWidth + view.x > parentWidth) {
newWidth = parentWidth - view.x.toInt()
}
if (newHeight + view.y > parentHeight) {
newHeight = parentHeight - view.y.toInt()
}

params.width = newWidth
params.height = newHeight

view.layoutParams = params
// note: requestLayout() is needed to get AutoResizeTextView to recalculate its fontSize after a
// change in view's width/height is made
view.requestLayout()
}
if (newHeight + view.y > parentHeight) {
newHeight = parentHeight - view.y.toInt()
}

params.width = newWidth
params.height = newHeight

view.layoutParams = params
// note: requestLayout() is needed to get AutoResizeTextView to recalculate its fontSize after a
// change in view's width/height is made
view.requestLayout()
lastDiffX = diffX
lastDiffY = diffY
}
Expand All @@ -144,6 +169,8 @@ class TextViewSizeAwareTouchListener(
}
MotionEvent.ACTION_UP -> {
originUp = true
minFontSizeReached = false
maxFontSizeReached = false

deleteView?.let {
if (isViewInBounds(it, event.rawX.toInt(), event.rawY.toInt())) {
Expand All @@ -155,6 +182,8 @@ class TextViewSizeAwareTouchListener(
}
MotionEvent.ACTION_POINTER_UP -> {
secondOriginUp = true
minFontSizeReached = false
maxFontSizeReached = false
}
}
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr
field = minSize
adjustTextSize()
}
private var maxMinFontSizeReachedListener: OnMaxMinFontSizeReached? = null
private var spacingMult = 1.0f
private var spacingAdd = 0.0f
private var widthLimit: Int = 0
Expand Down Expand Up @@ -198,9 +199,30 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr

private fun superSetTextSize(startSize: Int) {
val textSize = binarySearch(startSize, maxTextSize.toInt(), sizeTester, availableSpaceRect)
maxMinFontSizeReachedListener?.let {
if (isBestFontSizeMatchCloseEnoughToMax(textSize)) {
it.onMaxFontSizeReached()
} else if (isBestFontSizeMatchCloseEnoughToMin(textSize)) {
it.onMinFontSizeReached()
} else {
it.onFontSizeChangedWithinMinMaxRange()
}
}
super.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize.toFloat())
}

private fun isBestFontSizeMatchCloseEnoughToMax(textSize: Int): Boolean {
// binary search compares integer to float so there could be 1 point of roundup difference between maxFontSize
// and the available space on screen in which the font actually fits
return textSize >= maxTextSize.toInt()-1
}

private fun isBestFontSizeMatchCloseEnoughToMin(textSize: Int): Boolean {
// binary search compares integer to float so there could be 1 point of roundup difference between minFontSize
// and the available space on screen in which the font actually fits
return textSize <= minTextSize.toInt()+1
}

private fun binarySearch(start: Int, end: Int, sizeTester: SizeTester, availableSpace: RectF): Int {
var lastBest = start
var lo = start
Expand Down Expand Up @@ -234,6 +256,16 @@ class AutoResizeTextView @JvmOverloads constructor(context: Context, attrs: Attr
adjustTextSize()
}

fun setMaxMinFontSizeReachedListener(listener: OnMaxMinFontSizeReached) {
maxMinFontSizeReachedListener = listener
}

interface OnMaxMinFontSizeReached {
fun onMaxFontSizeReached()
fun onMinFontSizeReached()
fun onFontSizeChangedWithinMinMaxRange()
}

companion object {
private val NO_LINE_LIMIT = -1
}
Expand Down

0 comments on commit 7c5a523

Please sign in to comment.