Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Commit fd63642

Browse files
author
PonyCui
committed
feat: Add StaticLayout maxLines support.
1 parent d0da4d5 commit fd63642

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

library/src/main/java/com/opensource/svgaplayer/drawer/SVGACanvasDrawer.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import android.graphics.*
55
import android.os.Build
66
import android.text.BoringLayout
77
import android.text.StaticLayout
8+
import android.text.TextUtils
89
import android.widget.FrameLayout
910
import android.widget.ImageView
1011
import com.opensource.svgaplayer.SVGADynamicEntity
1112
import com.opensource.svgaplayer.SVGAVideoEntity
1213
import com.opensource.svgaplayer.entities.SVGAVideoShapeEntity
14+
import java.lang.Exception
15+
import java.lang.reflect.Field
1316

1417
/**
1518
* Created by cuiminghui on 2017/3/29.
@@ -271,7 +274,21 @@ internal class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVG
271274
textBitmap = it
272275
} ?: kotlin.run {
273276
it.paint.isAntiAlias = true
274-
var layout = StaticLayout(it.text, 0, it.text.length, it.paint, drawingBitmap.width, it.alignment, it.spacingMultiplier, it.spacingAdd, false)
277+
var layout = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
278+
var lineMax = try {
279+
val field = StaticLayout::class.java.getDeclaredField("mMaximumVisibleLineCount")
280+
field.isAccessible = true
281+
field.getInt(it)
282+
} catch (e: Exception) { Int.MAX_VALUE }
283+
StaticLayout.Builder
284+
.obtain(it.text, 0, it.text.length, it.paint, drawingBitmap.width)
285+
.setAlignment(it.alignment)
286+
.setMaxLines(lineMax)
287+
.setEllipsize(TextUtils.TruncateAt.END)
288+
.build()
289+
} else {
290+
StaticLayout(it.text, 0, it.text.length, it.paint, drawingBitmap.width, it.alignment, it.spacingMultiplier, it.spacingAdd, false)
291+
}
275292
textBitmap = Bitmap.createBitmap(drawingBitmap.width, drawingBitmap.height, Bitmap.Config.ARGB_8888)
276293
val textCanvas = Canvas(textBitmap)
277294
textCanvas.translate(0f, ((drawingBitmap.height - layout.height) / 2).toFloat())

0 commit comments

Comments
 (0)