[英]Custom View is not drawing in BottomSheetDialogFragment, when sometimes onSizeChanged is called after invalidate
我正在開發一個自定義圖形視圖,顯示在底部工作表對話框片段中並且它沒有繪制,當我添加數據並在調用onSizeChanged()
invalidate()
時。 這種情況有時會發生,我不知道如何解決。
我將數據添加到我的 BottomSheetDialogFragment 的onViewCreated(...)
中,即 LiveData,因此觀察者調用 ChartView 的setDataPoints()
肯定需要一些時間。
我還在兩個正常片段中使用圖表,它在那里工作正常。
我的自定義視圖:
class MonthlyTrendChart @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) {
//region Variables
private val data = mutableListOf<DateDataPoint>()
private val points = mutableListOf<PointF>()
private val path = Path()
private val pathPaint: Paint
//endregion
//region Constructor
init {
pathPaint = Paint().apply {
isAntiAlias = true
color = Color.WHITE
strokeWidth = 5.0f
style = Paint.Style.STROKE
}
setWillNotDraw(false)
}
//endregion
//region View
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
println("MB: onSizeChanged: ${data.size}")
}
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
computePath()
canvas?.drawPath(path, pathPaint)
}
//endregion
//region Methods
fun setDataPoints(dataPoints: List<DateDataPoint>) = GlobalScope.launch(Dispatchers.Main) {
val min = dataPoints.minByOrNull { it.value }?.value ?: 0.0f
val positiveDataPoints: List<DateDataPoint> = List(dataPoints.size) { i -> DateDataPoint(dataPoints[i].date, dataPoints[i].value - min) }
data.clear()
data.addAll(positiveDataPoints)
computePoints()
postInvalidate()
println("MB: invalidate: ${data.size}")
}
private fun computePoints() {
points.clear()
val graphHeight = height.toFloat()
val maxValue: Float = data.maxByOrNull { it.value }!!.value
val xStep: Float = width.toFloat() / (data.size - 1)
for (i in 0 until data.size) {
val xVal = xStep * i
val yVal = graphHeight - data[i].value / maxValue * graphHeight
points.add(PointF(xVal, yVal))
}
}
private fun computePath() {
if (points.isEmpty()) return
with(path) {
reset()
moveTo(points.first().x, points.first().y)
for (i in 0 until points.size) {
lineTo(points[i].x, points[i].y)
}
}
}
//endregion
companion object {
}
}
這是 output:
2021-02-19 18:13:25.210 29400-29400/at.guger.moneybook.dev I/System.out: MB: invalidate: 28
2021-02-19 18:13:30.146 29400-29400/at.guger.moneybook.dev I/System.out: MB: onSizeChanged: 0
2021-02-19 18:13:30.153 29400-29400/at.guger.moneybook.dev I/System.out: MB: invalidate: 28
2021-02-19 18:13:32.424 29400-29400/at.guger.moneybook.dev I/System.out: MB: onSizeChanged: 0
2021-02-19 18:13:32.429 29400-29400/at.guger.moneybook.dev I/System.out: MB: invalidate: 28
2021-02-19 18:13:34.897 29400-29400/at.guger.moneybook.dev I/System.out: MB: onSizeChanged: 0
2021-02-19 18:13:34.900 29400-29400/at.guger.moneybook.dev I/System.out: MB: invalidate: 28
2021-02-19 18:13:37.077 29400-29400/at.guger.moneybook.dev I/System.out: MB: onSizeChanged: 0
2021-02-19 18:13:37.083 29400-29400/at.guger.moneybook.dev I/System.out: MB: invalidate: 28
2021-02-19 18:13:38.689 29400-29400/at.guger.moneybook.dev I/System.out: MB: invalidate: 28 <---- this is the case when it doesn't draw
2021-02-19 18:13:38.697 29400-29400/at.guger.moneybook.dev I/System.out: MB: onSizeChanged: 28 <---- doesn't draw
我已經嘗試在onSizeChanged()
中調用invalidate()
) ,設置setWillNotDraw(false)
或使用postInvalidateDelayed(100)
延遲調用invalidate()
) 。
我找到了解決該問題的方法:
要檢查是否已調用onSizeChanged
,我在方法setDataPoints
中檢查height * width > 0
。
如果這個條件不成立,我設置一個標志來重新計算尺寸變化的繪圖數據。
在onSizeChanged
中,我檢查該標志並再次調用我的setDataPoints
方法,這現在可以工作了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.