Skip to content

Canvas homework#138

Open
alexanderbatrakov wants to merge 2 commits into
Otus-Android:masterfrom
alexanderbatrakov:canvas_hw
Open

Canvas homework#138
alexanderbatrakov wants to merge 2 commits into
Otus-Android:masterfrom
alexanderbatrakov:canvas_hw

Conversation

@alexanderbatrakov
Copy link
Copy Markdown

No description provided.

import kotlin.math.min
import kotlin.math.sqrt

class PieChart @JvmOverloads constructor(
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не реализован механизм сохранения состояния, нужно сделать onSaveInstanceState и onRestoreInstanceState

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

поправил, спасибо

setMeasuredDimension(wSize, pieHeight.toInt())
}
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

это не надо вызывать

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

поправил

val wSize = MeasureSpec.getSize(widthMeasureSpec)
val hSize = MeasureSpec.getSize(heightMeasureSpec)

when (wMode) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не нужно вызывать setMeasuredDimension отдельно для высоты и ширины, плюс можно все упростить через resolveSize, примерно так:

    val measuredWidth = resolveSize(desiredWidth, widthMeasureSpec)
    val measuredHeight = resolveSize(desiredHeight, heightMeasureSpec)
    val size = min(measuredWidth, measuredHeight)
    setMeasuredDimension(size, size)

desiredWidth и desiredHeight желаемые размеры

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

поправил

private val jsonMapper: JsonMapper
) : ViewModel() {

val data = jsonMapper.mapJson()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут каждый payload превращается в отдельный сектор, а данные должны агрегироваться по категориям

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

поправил


canvas.drawArc(rect, startAngle, swipeAngle, true, paint)

sectors.add(
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

в onDraw нельзя вычислять сектора, это нужно сделать заранее, он вызывается очень часто

@SuppressLint("DrawAllocation") не нужно подавлять, это реальная проблема производительности

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

поправил

style = Paint.Style.FILL
}

val rect = RectF(0f, 0f, pieWith, pieHeight)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

график всегда 200 на 200 и не подстраивается под размеры вью, нужно реализовать onSizeChanged и в нем определить размеры rect

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

поправил


val distance = sqrt(dx * dx + dy * dy)

if (distance > rect.width() / 2f) return false
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут лучше возвращать true иначе событие может уйти родителю

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

поправил


data.forEach {item ->
paint.color = item.color
val swipeAngle = (item.amount/total) * 360f
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут при нулевом total можем получить деление на 0

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

поправил

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants