@@ -17,6 +17,10 @@ import android.graphics.drawable.Drawable
17
17
import android.os.Build
18
18
import android.os.VibrationEffect
19
19
import android.os.Vibrator
20
+ import android.text.Layout
21
+ import android.text.StaticLayout
22
+ import android.text.TextPaint
23
+ import android.text.TextUtils
20
24
import android.util.AttributeSet
21
25
import android.util.Log
22
26
import android.util.TypedValue
@@ -38,6 +42,7 @@ import com.ncorti.slidetoact.SlideToActIconUtil.createIconAnimator
38
42
import com.ncorti.slidetoact.SlideToActIconUtil.loadIconCompat
39
43
import com.ncorti.slidetoact.SlideToActIconUtil.stopIconAnimation
40
44
import com.ncorti.slidetoact.SlideToActIconUtil.tintIconCompat
45
+ import kotlin.math.max
41
46
42
47
/* *
43
48
* Class representing the custom view, SlideToActView.
@@ -235,7 +240,7 @@ class SlideToActView @JvmOverloads constructor(
235
240
private val mInnerPaint: Paint = Paint (Paint .ANTI_ALIAS_FLAG )
236
241
237
242
/* * Paint used for text elements */
238
- private var mTextPaint: Paint = Paint (Paint .ANTI_ALIAS_FLAG )
243
+ private var mTextPaint: TextPaint = TextPaint (Paint .ANTI_ALIAS_FLAG )
239
244
240
245
/* * TextView used for text elements */
241
246
private var mTextView: TextView
@@ -482,14 +487,47 @@ class SlideToActView @JvmOverloads constructor(
482
487
mTextPaint.alpha = (255 * mPositionPercInv).toInt()
483
488
// Checking if the TextView has a Transformation method applied (e.g. AllCaps).
484
489
val textToDraw = mTextView.transformationMethod?.getTransformation(text, mTextView) ? : text
485
- canvas.drawText(
486
- textToDraw,
487
- 0 ,
488
- textToDraw.length,
489
- mTextXPosition,
490
- mTextYPosition,
491
- mTextPaint
492
- )
490
+ val leftOffset =
491
+ (mAreaHeight - 2 * mActualAreaMargin).toFloat() / mAreaHeight.toFloat() *
492
+ mBorderRadius.toFloat() * 2
493
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
494
+ val maxWidth = mAreaWidth - (2 * mActualAreaWidth) - leftOffset
495
+ val textLayout = StaticLayout .Builder
496
+ .obtain(textToDraw, 0 , textToDraw.length, mTextPaint, maxWidth.toInt())
497
+ .setAlignment(Layout .Alignment .ALIGN_NORMAL )
498
+ .setEllipsize(TextUtils .TruncateAt .END )
499
+ .setEllipsizedWidth(maxWidth.toInt() - mTextPaint.textSize.toInt())
500
+ .setMaxLines(
501
+ max(
502
+ 1f ,
503
+ mAreaHeight / (mTextPaint.descent() - mTextPaint.ascent())
504
+ ).toInt()
505
+ )
506
+ .build()
507
+ canvas.save()
508
+
509
+ val dY = (height / 2 ) - (textLayout.height / 2 )
510
+ val dX = (width / 2 ) - (textLayout.width / 2 )
511
+
512
+ val mTextLeft = mActualAreaWidth + leftOffset + dX
513
+ val mTextTop = 0 + dY
514
+ val mTextRight = mAreaWidth - mActualAreaWidth - dX
515
+
516
+ canvas.translate(mTextLeft.toFloat(), mTextTop.toFloat())
517
+ canvas.translate(((mTextRight - mTextLeft) / 2 ).toFloat(), 0f )
518
+ textLayout.draw(canvas)
519
+
520
+ canvas.restore()
521
+ } else {
522
+ canvas.drawText(
523
+ textToDraw,
524
+ 0 ,
525
+ textToDraw.length,
526
+ mTextXPosition,
527
+ mTextYPosition,
528
+ mTextPaint
529
+ )
530
+ }
493
531
494
532
// Inner Cursor
495
533
// ratio is used to compute the proper border radius for the inner rect (see #8).
0 commit comments