Precompute TextView in I/O thread for performance

This commit is contained in:
topjohnwu 2020-06-29 05:25:54 -07:00
parent 89e9e7c176
commit a0b47f3ca3
2 changed files with 15 additions and 1 deletions

View File

@ -1,9 +1,14 @@
package com.topjohnwu.magisk.databinding package com.topjohnwu.magisk.databinding
import android.view.View import android.view.View
import android.widget.TextView
import androidx.core.text.PrecomputedTextCompat
import androidx.core.view.isGone import androidx.core.view.isGone
import androidx.core.view.isInvisible import androidx.core.view.isInvisible
import androidx.core.widget.TextViewCompat
import androidx.databinding.BindingAdapter import androidx.databinding.BindingAdapter
import com.topjohnwu.magisk.extensions.subscribeK
import io.reactivex.Single
@BindingAdapter("gone") @BindingAdapter("gone")
fun setGone(view: View, gone: Boolean) { fun setGone(view: View, gone: Boolean) {
@ -24,3 +29,12 @@ fun setGoneUnless(view: View, goneUnless: Boolean) {
fun setInvisibleUnless(view: View, invisibleUnless: Boolean) { fun setInvisibleUnless(view: View, invisibleUnless: Boolean) {
setInvisible(view, invisibleUnless.not()) setInvisible(view, invisibleUnless.not())
} }
@BindingAdapter("precomputedText")
fun setPrecomputedText(tv: TextView, text: CharSequence) {
Single.fromCallable {
PrecomputedTextCompat.create(text, TextViewCompat.getTextMetricsParams(tv))
}.subscribeK {
TextViewCompat.setPrecomputedText(tv, it);
}
}

View File

@ -27,7 +27,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:fontFamily="monospace" android:fontFamily="monospace"
android:text="@{viewModel.consoleText}" precomputedText="@{viewModel.consoleText}"
android:textAppearance="@style/AppearanceFoundation.Caption" android:textAppearance="@style/AppearanceFoundation.Caption"
android:textSize="10sp" android:textSize="10sp"
android:paddingTop="@{viewModel.insets.top + (int) @dimen/internal_action_bar_size}" android:paddingTop="@{viewModel.insets.top + (int) @dimen/internal_action_bar_size}"