Skip to content

Commit

Permalink
Apply upstream PR cortinico#146
Browse files Browse the repository at this point in the history
  • Loading branch information
not-chicken committed Aug 24, 2022
1 parent 7f97945 commit 7f78fc5
Showing 1 changed file with 47 additions and 9 deletions.
56 changes: 47 additions & 9 deletions slidetoact/src/main/java/com/ncorti/slidetoact/SlideToActView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ import com.ncorti.slidetoact.SlideToActIconUtil.loadIconCompat
import com.ncorti.slidetoact.SlideToActIconUtil.startIconAnimation
import com.ncorti.slidetoact.SlideToActIconUtil.stopIconAnimation
import com.ncorti.slidetoact.SlideToActIconUtil.tintIconCompat
import android.text.Layout
import android.text.StaticLayout
import android.text.TextPaint
import android.text.TextUtils
import kotlin.math.max

/**
* Class representing the custom view, SlideToActView.
Expand Down Expand Up @@ -246,7 +251,7 @@ class SlideToActView @JvmOverloads constructor(
private val mInnerPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)

/** Paint used for text elements */
private var mTextPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
private var mTextPaint: TextPaint = TextPaint(Paint.ANTI_ALIAS_FLAG)

/** TextView used for text elements */
private var mTextView: TextView
Expand Down Expand Up @@ -510,14 +515,47 @@ class SlideToActView @JvmOverloads constructor(
mTextPaint.alpha = (255 * mPositionPercInv).toInt()
// Checking if the TextView has a Transformation method applied (e.g. AllCaps).
val textToDraw = mTextView.transformationMethod?.getTransformation(text, mTextView) ?: text
canvas.drawText(
textToDraw,
0,
textToDraw.length,
mTextXPosition,
mTextYPosition,
mTextPaint
)
val leftOffset =
(mAreaHeight - 2 * mActualAreaMargin).toFloat() / mAreaHeight.toFloat() *
mBorderRadius.toFloat() * 2
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val maxWidth = mAreaWidth - (2 * mActualAreaWidth) - leftOffset
val textLayout = StaticLayout.Builder
.obtain(textToDraw, 0, textToDraw.length, mTextPaint, maxWidth.toInt())
.setAlignment(Layout.Alignment.ALIGN_NORMAL)
.setEllipsize(TextUtils.TruncateAt.END)
.setEllipsizedWidth(maxWidth.toInt() - mTextPaint.textSize.toInt())
.setMaxLines(
max(
1f,
mAreaHeight / (mTextPaint.descent() - mTextPaint.ascent())
).toInt()
)
.build()
canvas.save()

val dY = (height / 2) - (textLayout.height / 2)
val dX = (width / 2) - (textLayout.width / 2)

val mTextLeft = mActualAreaWidth + leftOffset + dX
val mTextTop = 0 + dY
val mTextRight = mAreaWidth - mActualAreaWidth - dX

canvas.translate(mTextLeft.toFloat(), mTextTop.toFloat())
canvas.translate(((mTextRight - mTextLeft) / 2).toFloat(), 0f)
textLayout.draw(canvas)

canvas.restore()
} else {
canvas.drawText(
textToDraw,
0,
textToDraw.length,
mTextXPosition,
mTextYPosition,
mTextPaint
)
}

// Inner Cursor
// ratio is used to compute the proper border radius for the inner rect (see #8).
Expand Down

0 comments on commit 7f78fc5

Please sign in to comment.