Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ImageSpan删除卡顿 #20

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions app/src/main/java/com/drake/spannable/sample/RichInputActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.graphics.Typeface
import android.os.Bundle
import android.text.Editable
import com.drake.engine.utils.dp
import com.drake.spannable.factory.ImageEditableFactory
import com.drake.spannable.listener.ModifyTextWatcher
import com.drake.spannable.replaceSpan
import com.drake.spannable.sample.base.BaseMenuActivity
Expand All @@ -34,23 +35,41 @@ class RichInputActivity : BaseMenuActivity() {
// 匹配规则, 因为同一个Span对象重复设置仅最后一个有效故每次都得创建新的对象
private val matchRules = mapOf<Regex, (MatchResult) -> Any?>(
"@[^@]+?(?=\\s|\$)".toRegex() to { HighlightSpan("#ed6a2c") },
"#[^@]+?(?=\\s|\$)".toRegex() to { HighlightSpan("#4a70d2", Typeface.defaultFromStyle(Typeface.BOLD)) },
"#[^@]+?(?=\\s|\$)".toRegex() to {
HighlightSpan(
"#4a70d2",
Typeface.defaultFromStyle(Typeface.BOLD)
)
},
"蚂蚁".toRegex() to { CenterImageSpan(this, R.drawable.ic_ant).setDrawableSize(50.dp) },
"生气|angry".toRegex() to { CenterImageSpan(this, R.drawable.ic_angry).setDrawableSize(50.dp) },
"开心|happy".toRegex() to { CenterImageSpan(this, R.drawable.ic_happy).setDrawableSize(50.dp) }
"生气|angry".toRegex() to {
CenterImageSpan(
this,
R.drawable.ic_angry
).setDrawableSize(50.dp)
},
"开心|happy".toRegex() to {
CenterImageSpan(
this,
R.drawable.ic_happy
).setDrawableSize(50.dp)
}
)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)

// 包含 @用户 #标签 表情包 等自动替换规则
binding.etInput.addTextChangedListener(object : ModifyTextWatcher() {
override fun onModify(s: Editable) {
matchRules.forEach { rule ->
s.replaceSpan(rule.key, replacement = rule.value)
with(binding) {
etInput.setEditableFactory(ImageEditableFactory.instance)
etInput.addTextChangedListener(object : ModifyTextWatcher() {
override fun onModify(s: Editable) {
matchRules.forEach { rule ->
s.replaceSpan(rule.key, replacement = rule.value)
}
}
}
})
})
}
}
}
2 changes: 1 addition & 1 deletion spannable/src/main/java/com/drake/spannable/SpanUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ fun CharSequence.replaceSpan(
(spanBuilder as SpannableStringBuilder).replace(range.first + offset, range.first + offset + matchLength, adjustReplacement)
offset += adjustReplacement.length - matchLength
}
else -> spanBuilder[range.first, range.last + 1] = spanned
else -> spanBuilder.setSpan(spanned,range.first, range.last + 1)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.drake.spannable.factory

import android.annotation.SuppressLint
import android.text.Editable
import androidx.annotation.GuardedBy
import com.drake.spannable.factory.SpannableBuilder.Companion.create

/*
* Copyright 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ /**
* EditableFactory used to improve editing operations on an EditText.
*
*
* EditText uses DynamicLayout, which attaches to the Spannable instance that is being edited using
* ChangeWatcher. ChangeWatcher implements SpanWatcher and Textwatcher. Currently every delete/add
* operation is reported to DynamicLayout, for every span that has changed. For each change,
* DynamicLayout performs some expensive computations. i.e. if there is 100 EmojiSpans and the first
* span is deleted, DynamicLayout gets 99 calls about the change of position occurred in the
* remaining spans. This causes a huge delay in response time.
*
*
* Since "android.text.DynamicLayout$ChangeWatcher" class is not a public class,
* ImageEditableFactory checks if the watcher is in the classpath, and if so uses the modified
* Spannable which reduces the total number of calls to DynamicLayout for operations that affect
* EmojiSpans.
*
* @see SpannableBuilder
*/
class ImageEditableFactory @SuppressLint("PrivateApi") private constructor() :
Editable.Factory() {
init {
try {
val className = "android.text.DynamicLayout\$ChangeWatcher"
sWatcherClass = Class.forName(className, false, javaClass.classLoader)
} catch (t: Throwable) {
// ignore
}
}

override fun newEditable(source: CharSequence): Editable {
return if (sWatcherClass != null) {
create(
sWatcherClass!!,
source
)
} else super.newEditable(
source
)
}

companion object {
private val INSTANCE_LOCK = Any()

@GuardedBy("INSTANCE_LOCK")
@Volatile
private var sInstance: Editable.Factory? = null
private var sWatcherClass: Class<*>? = null
val instance: Editable.Factory?
get() {
if (sInstance == null) {
synchronized(INSTANCE_LOCK) {
if (sInstance == null) {
sInstance = ImageEditableFactory()
}
}
}
return sInstance
}
}
}
Loading