Slightly increase library view performance by caching typefaces

This commit is contained in:
len 2016-04-22 19:41:59 +02:00
parent 9f0da3f1d6
commit b83efd90a8

View File

@ -6,6 +6,7 @@ import android.graphics.Typeface
import android.util.AttributeSet import android.util.AttributeSet
import android.widget.TextView import android.widget.TextView
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import java.util.*
class PTSansTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) : class PTSansTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
@ -14,6 +15,9 @@ class PTSansTextView @JvmOverloads constructor(context: Context, attrs: Attribut
companion object { companion object {
const val PTSANS_NARROW = 0 const val PTSANS_NARROW = 0
const val PTSANS_NARROW_BOLD = 1 const val PTSANS_NARROW_BOLD = 1
// Map where typefaces are cached
private val typefaces = HashMap<Int, Typeface>(2)
} }
init { init {
@ -22,11 +26,13 @@ class PTSansTextView @JvmOverloads constructor(context: Context, attrs: Attribut
val typeface = values.getInt(R.styleable.PTSansTextView_typeface, 0) val typeface = values.getInt(R.styleable.PTSansTextView_typeface, 0)
when (typeface) { setTypeface(typefaces.getOrPut(typeface) {
PTSANS_NARROW -> setTypeface(Typeface.createFromAsset(context.assets, "fonts/PTSans-Narrow.ttf")) Typeface.createFromAsset(context.assets, when (typeface) {
PTSANS_NARROW_BOLD -> setTypeface(Typeface.createFromAsset(context.assets, "fonts/PTSans-NarrowBold.ttf")) PTSANS_NARROW -> "fonts/PTSans-Narrow.ttf"
else -> throw IllegalArgumentException("Font not found " + typeface) PTSANS_NARROW_BOLD -> "fonts/PTSans-NarrowBold.ttf"
} else -> throw IllegalArgumentException("Font not found " + typeface)
})
})
values.recycle() values.recycle()
} }