pref: 表情显示性能

This commit is contained in:
HuanCheng65 2023-07-20 15:31:08 +08:00
parent 67d4af51aa
commit 91893bb3b7
No known key found for this signature in database
GPG Key ID: 5EC9DD60A32C7360
8 changed files with 67 additions and 23 deletions

View File

@ -346,9 +346,9 @@ class ReplyActivity : BaseActivity(), View.OnClickListener,
editText.mgr.disable()
editText.setText(
StringUtil.getEmoticonContent(
EmoticonUtil.EMOTICON_ALL_WEB_TYPE,
editText,
content
content,
EmoticonUtil.EMOTICON_ALL_WEB_TYPE
)
)
editText.mgr.enable()

View File

@ -10,7 +10,12 @@ import com.huanchengfly.tieba.post.api.models.MessageListBean
import com.huanchengfly.tieba.post.api.models.MessageListBean.MessageInfoBean
import com.huanchengfly.tieba.post.components.MyViewHolder
import com.huanchengfly.tieba.post.fragments.MessageFragment
import com.huanchengfly.tieba.post.utils.*
import com.huanchengfly.tieba.post.utils.DateTimeUtils
import com.huanchengfly.tieba.post.utils.EmoticonUtil
import com.huanchengfly.tieba.post.utils.ImageUtil
import com.huanchengfly.tieba.post.utils.NavigationHelper
import com.huanchengfly.tieba.post.utils.StringUtil
import com.huanchengfly.tieba.post.utils.getItemBackgroundDrawable
class MessageListAdapter(
context: Context, private val type: Int
@ -73,13 +78,12 @@ class MessageListAdapter(
)
val contentTextView = viewHolder.getView<TextView>(R.id.message_list_item_content)
contentTextView.text = StringUtil.getEmoticonContent(
EmoticonUtil.EMOTICON_ALL_TYPE,
contentTextView,
item.content
item.content,
EmoticonUtil.EMOTICON_ALL_TYPE
)
val textView = viewHolder.getView<TextView>(R.id.message_list_item_quote)
textView.text = StringUtil.getEmoticonContent(
EmoticonUtil.EMOTICON_ALL_TYPE,
textView,
if (type == MessageFragment.TYPE_REPLY_ME) {
if ("1" == item.isFloor) {
@ -89,7 +93,8 @@ class MessageListAdapter(
}
} else {
item.title
}
},
EmoticonUtil.EMOTICON_ALL_TYPE
)
textView.setOnClickListener {
if ("1" == item.isFloor && item.quotePid != null) {

View File

@ -257,7 +257,7 @@ public class RecyclerFloorAdapter extends BaseSingleTypeAdapter<SubFloorListBean
private void setText(TextView textView, CharSequence content) {
content = BilibiliUtil.replaceVideoNumberSpan(getContext(), content);
content = StringUtil.getEmoticonContent(EmoticonUtil.EMOTICON_ALL_TYPE, textView, content);
content = StringUtil.getEmoticonContent(textView, content, EmoticonUtil.EMOTICON_ALL_TYPE);
textView.setText(content);
}

View File

@ -213,7 +213,7 @@ class ThreadReplyAdapter(context: Context) :
}
textView.text = replaceVideoNumberSpan(
context,
StringUtil.getEmoticonContent(EmoticonUtil.EMOTICON_ALL_TYPE, textView, builder)
StringUtil.getEmoticonContent(textView, builder, EmoticonUtil.EMOTICON_ALL_TYPE)
)
textView.setPadding(
DisplayUtil.dp2px(context, 8f),

View File

@ -6,11 +6,12 @@ import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import androidx.appcompat.content.res.AppCompatResources
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
@ -21,6 +22,9 @@ import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.rememberTextMeasurer
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.github.panpf.sketch.compose.AsyncImage
import com.github.panpf.sketch.fetch.newFileUri
import com.github.panpf.sketch.fetch.newResourceUri
import com.github.panpf.sketch.request.LoadRequest
import com.github.panpf.sketch.request.LoadResult
import com.google.accompanist.drawablepainter.rememberDrawablePainter
@ -48,6 +52,11 @@ fun getEmoticonHeightPx(style: TextStyle): Int {
return textLayoutResult.size.height
}
data class Emoticon(
val id: String,
val name: String
)
object EmoticonManager {
private val DEFAULT_EMOTICON_MAPPING: Map<String, String> = mapOf(
"呵呵" to "image_emoticon1",
@ -128,13 +137,8 @@ object EmoticonManager {
PlaceholderVerticalAlign.TextCenter
),
children = {
Image(
painter = rememberDrawablePainter(
drawable = getEmoticonDrawable(
LocalContext.current,
id
)
),
AsyncImage(
imageUri = rememberEmoticonUri(id = id),
contentDescription = stringResource(
id = R.string.emoticon,
getEmoticonNameById(id) ?: ""
@ -212,6 +216,15 @@ object EmoticonManager {
return File(getEmoticonCacheDir(), "$id.png")
}
fun getAllEmoticon(): List<Emoticon> {
return emoticonIds.map { id ->
Emoticon(
id = id,
name = getEmoticonNameById(id) ?: ""
)
}
}
fun getEmoticonIdByName(name: String): String? {
return emoticonMapping[name]
}
@ -252,6 +265,32 @@ object EmoticonManager {
).also { drawableCache[id] = it }
}
fun getEmoticonUri(context: Context, id: String?): String {
id ?: return ""
val resId = getEmoticonResId(context, id)
if (resId != 0) {
return newResourceUri(resId)
}
val emoticonFile = getEmoticonFile(id)
if (!emoticonFile.exists()) {
return ""
}
return newFileUri(emoticonFile)
}
@Composable
fun rememberEmoticonPainter(id: String): Painter {
val context = LocalContext.current
val drawable = remember(id) { getEmoticonDrawable(context, id) }
return rememberDrawablePainter(drawable = drawable)
}
@Composable
fun rememberEmoticonUri(id: String): String {
val context = LocalContext.current
return remember(id) { getEmoticonUri(context, id) }
}
private suspend fun fetchEmoticons(context: Context) {
emoticonIds.forEach {
val resId = getEmoticonResId(context, it)

View File

@ -172,8 +172,8 @@ object EmoticonUtil {
// 特殊文字处理,将表情等转换一下
mEditText!!.setText(
getEmoticonContent(
emoticon_map_type,
mEditText!!, sb
mEditText!!,
sb, emoticon_map_type
)
)

View File

@ -133,7 +133,7 @@ class PostListAdapterHelper(
private fun setText(textView: TextView, content: CharSequence?) {
var text = content
text = replaceVideoNumberSpan(context, text)
text = StringUtil.getEmoticonContent(EmoticonUtil.EMOTICON_ALL_TYPE, textView, text)
text = StringUtil.getEmoticonContent(textView, text, EmoticonUtil.EMOTICON_ALL_TYPE)
textView.text = text
}

View File

@ -25,9 +25,9 @@ import kotlin.math.roundToInt
object StringUtil {
@JvmStatic
fun getEmoticonContent(
emoticon_map_type: Int,
tv: TextView,
source: CharSequence?
source: CharSequence?,
emoticonType: Int = EmoticonUtil.EMOTICON_ALL_TYPE
): SpannableString {
return try {
if (source == null) {
@ -38,7 +38,7 @@ object StringUtil {
} else {
SpannableString(source)
}
val regexEmoticon = EmoticonUtil.getRegex(emoticon_map_type)
val regexEmoticon = EmoticonUtil.getRegex(emoticonType)
val patternEmoticon = Pattern.compile(regexEmoticon)
val matcherEmoticon = patternEmoticon.matcher(spannableString)
while (matcherEmoticon.find()) {