mirror of
https://github.com/Tencent/QMUI_Android.git
synced 2026-10-17 04:31:16 +08:00
use int
This commit is contained in:
@@ -62,14 +62,14 @@ class Line private constructor() {
|
||||
|
||||
fun add(element: Element) {
|
||||
mElements.add(element)
|
||||
contentWidth = (contentWidth + element.measureWidth).toInt()
|
||||
contentHeight = contentHeight.toFloat().coerceAtLeast(element.measureHeight).toInt()
|
||||
contentWidth += element.measureWidth
|
||||
contentHeight = contentHeight.coerceAtLeast(element.measureHeight)
|
||||
}
|
||||
|
||||
fun addFirst(element: Element) {
|
||||
mElements.add(0, element)
|
||||
contentWidth = (contentWidth + element.measureWidth).toInt()
|
||||
contentHeight = contentHeight.toFloat().coerceAtLeast(element.measureHeight).toInt()
|
||||
contentWidth += element.measureWidth
|
||||
contentHeight = contentHeight.coerceAtLeast(element.measureHeight)
|
||||
}
|
||||
|
||||
fun first(): Element? {
|
||||
@@ -185,14 +185,14 @@ class Line private constructor() {
|
||||
hideLastIfSpaceIfNeeded(dropLastIfSpace)
|
||||
layoutWidth = contentWidth
|
||||
val alignment = env.alignment
|
||||
var start = x.toFloat()
|
||||
var addSpace = 0f
|
||||
var start = x
|
||||
var addSpace = 0
|
||||
if (alignment === TypeEnvironment.Alignment.RIGHT) {
|
||||
start = (x + widthLimit - contentWidth).toFloat()
|
||||
start = x + widthLimit - contentWidth
|
||||
} else if (alignment === TypeEnvironment.Alignment.CENTER) {
|
||||
start = x + (widthLimit - contentWidth) / 2f
|
||||
start = x + (widthLimit - contentWidth) / 2
|
||||
} else if (alignment === TypeEnvironment.Alignment.JUSTIFY) {
|
||||
val remain = (widthLimit - contentWidth).toFloat()
|
||||
val remain = widthLimit - contentWidth
|
||||
if (!(isEnd || isMiddleParagraphEndLine) || remain < env.lastLineJustifyMaxWidth) {
|
||||
val gapCount = calculateGapCount()
|
||||
if (gapCount > 0) {
|
||||
@@ -211,7 +211,7 @@ class Line private constructor() {
|
||||
}
|
||||
el.x = x
|
||||
x += el.measureWidth
|
||||
el.y = y + (contentHeight - el.measureHeight) / 2f
|
||||
el.y = y + (contentHeight - el.measureHeight) / 2
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,13 +29,13 @@ class DrawableElement(
|
||||
}
|
||||
|
||||
override fun onMeasure(env: TypeEnvironment) {
|
||||
setMeasureDimen(drawable.intrinsicWidth.toFloat(), drawable.intrinsicHeight.toFloat(), 0f)
|
||||
setMeasureDimen(drawable.intrinsicWidth, drawable.intrinsicHeight, 0)
|
||||
}
|
||||
|
||||
override fun onDraw(env: TypeEnvironment, canvas: Canvas) {
|
||||
drawBg(env, canvas)
|
||||
canvas.save()
|
||||
canvas.translate(x, y)
|
||||
canvas.translate(x.toFloat(), y.toFloat())
|
||||
drawable.draw(canvas)
|
||||
canvas.restore()
|
||||
drawBorder(env, canvas)
|
||||
|
||||
@@ -57,14 +57,14 @@ abstract class Element(val text: CharSequence, val index: Int, val start: Int) {
|
||||
var visible = VISIBLE
|
||||
|
||||
|
||||
var measureWidth = 0f
|
||||
var measureWidth = 0
|
||||
private set
|
||||
var measureHeight = 0f
|
||||
var measureHeight = 0
|
||||
private set
|
||||
var x = 0f
|
||||
var y = 0f
|
||||
var baseLine = 0f
|
||||
var nextGapWidth = 0f
|
||||
var x = 0
|
||||
var y = 0
|
||||
var baseLine = 0
|
||||
var nextGapWidth = 0
|
||||
|
||||
private var saveTypeList: MutableList<Int>? = null
|
||||
private var restoreTypeList: MutableList<Int>? = null
|
||||
@@ -92,8 +92,8 @@ abstract class Element(val text: CharSequence, val index: Int, val start: Int) {
|
||||
}
|
||||
}
|
||||
|
||||
private val rightWithGap: Float
|
||||
get() = x + measureWidth + nextGapWidth + 0.5f
|
||||
private val rightWithGap: Int
|
||||
get() = x + measureWidth + nextGapWidth
|
||||
|
||||
init {
|
||||
if(text.length == 1){
|
||||
@@ -235,7 +235,7 @@ abstract class Element(val text: CharSequence, val index: Int, val start: Int) {
|
||||
}
|
||||
|
||||
|
||||
protected fun setMeasureDimen(measureWidth: Float, measureHeight: Float, baseline: Float) {
|
||||
protected fun setMeasureDimen(measureWidth: Int, measureHeight: Int, baseline: Int) {
|
||||
this.measureWidth = measureWidth
|
||||
this.measureHeight = measureHeight
|
||||
baseLine = baseline
|
||||
@@ -275,7 +275,7 @@ abstract class Element(val text: CharSequence, val index: Int, val start: Int) {
|
||||
protected abstract fun onDraw(env: TypeEnvironment, canvas: Canvas)
|
||||
protected fun drawBg(env: TypeEnvironment, canvas: Canvas) {
|
||||
if (env.backgroundColor != Color.TRANSPARENT) {
|
||||
canvas.drawRect(x, y, rightWithGap, y + measureHeight, env.bgPaint)
|
||||
canvas.drawRect(x.toFloat(), y.toFloat(), rightWithGap.toFloat(), (y + measureHeight).toFloat(), env.bgPaint)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,21 +283,21 @@ abstract class Element(val text: CharSequence, val index: Int, val start: Int) {
|
||||
val paint = env.borderPaint
|
||||
if (env.borderLeftWidth > 0) {
|
||||
paint.color = env.borderLeftColor
|
||||
canvas.drawRect(x, y, x + env.borderLeftWidth, y + measureHeight, paint)
|
||||
canvas.drawRect(x.toFloat(), y.toFloat(), (x + env.borderLeftWidth).toFloat(), (y + measureHeight).toFloat(), paint)
|
||||
}
|
||||
if (env.borderTopWidth > 0) {
|
||||
paint.color = env.borderTopColor
|
||||
canvas.drawRect(x, y, rightWithGap, y + env.borderTopWidth, paint)
|
||||
canvas.drawRect(x.toFloat(), y.toFloat(), rightWithGap.toFloat(), (y + env.borderTopWidth).toFloat(), paint)
|
||||
}
|
||||
if (env.borderRightWidth > 0) {
|
||||
paint.color = env.borderRightColor
|
||||
canvas.drawRect(x + measureWidth - env.borderRightWidth, y,
|
||||
x + measureWidth, y + measureHeight, paint)
|
||||
canvas.drawRect((x + measureWidth - env.borderRightWidth).toFloat(), y.toFloat(),
|
||||
(x + measureWidth).toFloat(), (y + measureHeight).toFloat(), paint)
|
||||
}
|
||||
if (env.borderBottomWidth > 0) {
|
||||
paint.color = env.borderBottomColor
|
||||
canvas.drawRect(x, y + measureHeight - env.borderBottomWidth,
|
||||
rightWithGap, y + measureHeight, paint)
|
||||
canvas.drawRect(x.toFloat(), (y + measureHeight - env.borderBottomWidth).toFloat(),
|
||||
rightWithGap.toFloat(), (y + measureHeight).toFloat(), paint)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,13 +25,13 @@ class EmojiElement(val drawable: Drawable, text: CharSequence, index: Int, start
|
||||
val paint = env.paint
|
||||
val size = (paint.fontMetrics.descent - paint.fontMetrics.ascent).toInt()
|
||||
drawable.setBounds(0, 0, size, size)
|
||||
setMeasureDimen(size.toFloat(), size.toFloat(), 0f)
|
||||
setMeasureDimen(size, size, 0)
|
||||
}
|
||||
|
||||
override fun onDraw(env: TypeEnvironment, canvas: Canvas) {
|
||||
drawBg(env, canvas)
|
||||
canvas.save()
|
||||
canvas.translate(x, y)
|
||||
canvas.translate(x.toFloat(), y.toFloat())
|
||||
drawable.draw(canvas)
|
||||
canvas.restore()
|
||||
drawBorder(env, canvas)
|
||||
|
||||
@@ -33,7 +33,7 @@ class IgnoreEffectElement(list: List<Element>) : Element("", -1, -1) {
|
||||
}
|
||||
|
||||
override fun onMeasure(env: TypeEnvironment) {
|
||||
setMeasureDimen(0f, 0f, 0f)
|
||||
setMeasureDimen(0, 0, 0)
|
||||
}
|
||||
|
||||
override fun onDraw(env: TypeEnvironment, canvas: Canvas) {}
|
||||
|
||||
@@ -21,7 +21,7 @@ import com.qmuiteam.qmui.type.TypeEnvironment
|
||||
class NextParagraphElement(text: CharSequence, index: Int, start: Int) : Element(text, index, start) {
|
||||
|
||||
override fun onMeasure(env: TypeEnvironment) {
|
||||
setMeasureDimen(0f, 0f, 0f)
|
||||
setMeasureDimen(0, 0, 0)
|
||||
}
|
||||
|
||||
override fun onDraw(env: TypeEnvironment, canvas: Canvas) {}
|
||||
|
||||
@@ -22,14 +22,14 @@ open class TextElement(text: CharSequence, index: Int, start: Int) : Element(tex
|
||||
|
||||
override fun onMeasure(env: TypeEnvironment) {
|
||||
val paint = env.paint
|
||||
setMeasureDimen(paint.measureText(text, 0, text.length),
|
||||
paint.fontMetrics.descent - paint.fontMetrics.ascent,
|
||||
-paint.fontMetrics.ascent)
|
||||
setMeasureDimen((paint.measureText(text, 0, text.length) + 0.5f).toInt(),
|
||||
paint.fontMetricsInt.descent - paint.fontMetricsInt.ascent,
|
||||
-paint.fontMetricsInt.ascent)
|
||||
}
|
||||
|
||||
override fun onDraw(env: TypeEnvironment, canvas: Canvas) {
|
||||
drawBg(env, canvas)
|
||||
canvas.drawText(text, 0, text.length, x, y + baseLine, env.paint)
|
||||
canvas.drawText(text, 0, text.length, x.toFloat(), (y + baseLine).toFloat(), env.paint)
|
||||
drawBorder(env, canvas)
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,8 @@ package com.qmuiteam.qmui.type.view
|
||||
|
||||
import android.content.Context
|
||||
import android.graphics.*
|
||||
import android.os.Build
|
||||
import android.os.SystemClock
|
||||
import android.util.AttributeSet
|
||||
import androidx.annotation.RequiresApi
|
||||
import com.qmuiteam.qmui.type.TypeModel
|
||||
import com.qmuiteam.qmui.type.parser.PlainTextParser
|
||||
import com.qmuiteam.qmui.type.parser.TextParser
|
||||
@@ -22,7 +20,7 @@ class MarqueeTypeView : BaseTypeView {
|
||||
if (field != value) {
|
||||
field = value
|
||||
reset()
|
||||
contentWidth = -1f
|
||||
contentWidth = -1
|
||||
typeModel = value.parse(text)
|
||||
requestLayout()
|
||||
if (isAttachedToWindow) {
|
||||
@@ -36,7 +34,7 @@ class MarqueeTypeView : BaseTypeView {
|
||||
if (field != value) {
|
||||
field = value
|
||||
reset()
|
||||
contentWidth = -1f
|
||||
contentWidth = -1
|
||||
typeModel = textParser.parse(value)
|
||||
requestLayout()
|
||||
if (isAttachedToWindow) {
|
||||
@@ -50,8 +48,8 @@ class MarqueeTypeView : BaseTypeView {
|
||||
var moveSpeedPerMs: Float = resources.displayMetrics.density / 36
|
||||
var lastDrawTime = -2L
|
||||
|
||||
private var elementMaxHeight: Float = 0f
|
||||
private var contentWidth: Float = -1f
|
||||
private var elementMaxHeight = 0
|
||||
private var contentWidth = -1
|
||||
private var fadeHelper: FadeHelper? = null
|
||||
private var startX: Float = 0f
|
||||
|
||||
@@ -87,9 +85,9 @@ class MarqueeTypeView : BaseTypeView {
|
||||
|
||||
|
||||
private fun measureAndLayoutModel() {
|
||||
elementMaxHeight = 0f
|
||||
elementMaxHeight = 0
|
||||
var current = typeModel?.firstElement()
|
||||
var x = 0f
|
||||
var x = 0
|
||||
while (current != null) {
|
||||
current.measure(environment)
|
||||
current.x = x
|
||||
|
||||
Reference in New Issue
Block a user