Avoid calling textPaint.getTextBounds repeatedly

This operation seems to be very expensive on some Samsung phones,
which causes ANRs.

Fixes #962
This commit is contained in:
2021-06-05 18:19:03 -05:00
parent 89b24911ba
commit be51538704
3 changed files with 10 additions and 3 deletions

View File

@@ -4,6 +4,7 @@
### Fixed ### Fixed
- Improve automatic checkmarks for monthly habits (@iSoron, 947) - Improve automatic checkmarks for monthly habits (@iSoron, 947)
- Fix small theme issues (@iSoron) - Fix small theme issues (@iSoron)
- Fix ANR on some Samsung phones (@iSoron, #962)
## [2.0.2] - 2021-05-23 ## [2.0.2] - 2021-05-23

View File

@@ -36,6 +36,7 @@ class AndroidCanvas : Canvas {
var innerDensity = 1.0 var innerDensity = 1.0
var innerWidth = 0 var innerWidth = 0
var innerHeight = 0 var innerHeight = 0
var mHeight = 15
var paint = Paint().apply { var paint = Paint().apply {
isAntiAlias = true isAntiAlias = true
@@ -64,11 +65,10 @@ class AndroidCanvas : Canvas {
} }
override fun drawText(text: String, x: Double, y: Double) { override fun drawText(text: String, x: Double, y: Double) {
textPaint.getTextBounds(text, 0, text.length, textBounds)
innerCanvas.drawText( innerCanvas.drawText(
text, text,
x.toDp(), x.toDp(),
y.toDp() - textBounds.exactCenterY(), y.toDp() + 0.6f * mHeight,
textPaint, textPaint,
) )
} }
@@ -126,10 +126,17 @@ class AndroidCanvas : Canvas {
Font.BOLD -> Typeface.DEFAULT_BOLD Font.BOLD -> Typeface.DEFAULT_BOLD
Font.FONT_AWESOME -> getFontAwesome(context) Font.FONT_AWESOME -> getFontAwesome(context)
} }
updateMHeight()
} }
override fun setFontSize(size: Double) { override fun setFontSize(size: Double) {
textPaint.textSize = size.toDp() textPaint.textSize = size.toDp()
updateMHeight()
}
private fun updateMHeight() {
textPaint.getTextBounds("m", 0, 1, textBounds)
mHeight = textBounds.height()
} }
override fun setStrokeWidth(size: Double) { override fun setStrokeWidth(size: Double) {

View File

@@ -65,7 +65,6 @@ open class HabitPickerDialog : Activity() {
widgetUpdater = component.widgetUpdater widgetUpdater = component.widgetUpdater
widgetId = intent.extras?.getInt(EXTRA_APPWIDGET_ID, INVALID_APPWIDGET_ID) ?: 0 widgetId = intent.extras?.getInt(EXTRA_APPWIDGET_ID, INVALID_APPWIDGET_ID) ?: 0
val habitIds = ArrayList<Long>() val habitIds = ArrayList<Long>()
val habitNames = ArrayList<String>() val habitNames = ArrayList<String>()
for (h in habitList) { for (h in habitList) {