gpt4 book ai didi

android - 如何使用 Android SpannableString 和 MaskFilterSpan 按预期模糊 TextView 中的某些文本?

转载 作者:行者123 更新时间:2023-11-29 19:08:57 25 4
gpt4 key购买 nike

我所期望的是这样的。 enter image description here这是一篇影评,当某些文字可能剧透时,会被这些标签包裹[剧透][/剧透]。所以我写了一个方法找出每个剧透词的开始索引和结束索引,然后使用 MaskFilterSpan 对其进行模糊处理。

但结果并不好。

enter image description here

如果我只是将 MaskFilterSpan 替换为 StrikethroughSpan 或 BackgroundColorSpan,它就完美无缺。 enter image description here enter image description here

我的代码:

private void testBlurText() {
String text = "Alec Baldwin nailed his job; \n" +
"The animators nailed their job (seriously, the art style and changes were amazing);\n" +
"The ideas guy nailed his job ([spoiler]It's all the imagination of the kid[/spoiler]);\n" +
"The writers just let the story down :( ([spoiler]Although at the beginning it's clear that the whole shebang is the older kid's imagination, the writers probably realised that the script was too short and so the end got changed to something that makes no sense. Why would Tim receive the kid for a second time? - unless it was all a dream...[/spoiler] ).\n" +
"\n" +
"All in all, a good movie, amazing acting and animating, partly let down by a badly written ending to the story. 8/10.";
SpannableString spanText = new SpannableString(text);
List<Pair<Integer, Integer>> spoilersContainer = new ArrayList<>();
getSpoilerIndex(text, 0, spoilersContainer);
for (Pair<Integer, Integer> pair : spoilersContainer) {
spanText.setSpan(new MaskFilterSpan(new BlurMaskFilter(20, BlurMaskFilter.Blur.NORMAL)), pair.first, pair.second, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
Logger.d("SpoilerIndex:" + pair.toString());
}
tvTestBlurText.setText(spanText);
}

private void getSpoilerIndex(String rawComment, int beginIndex, List<Pair<Integer, Integer>> spoilersContainer) {
int startIndex = rawComment.indexOf("[spoiler]", beginIndex);
int endIndex = rawComment.indexOf("[/spoiler]", beginIndex);
if (startIndex != -1 && endIndex != -1) {
spoilersContainer.add(new Pair<>(startIndex, endIndex + "[/spoiler]".length()));
getSpoilerIndex(rawComment, endIndex + "[/spoiler]".length(), spoilersContainer);
}
}

MaskFilterSpan 有什么问题吗,或者只是一个错误?

最佳答案

如果您在 AndroidManifest.xml 中的 Activity 上设置 android:hardwareAccelerated="false",它会正确呈现。

BlurMaskFilter 不支持硬件加速。

您可以在此处检查不支持的绘图操作:

http://developer.android.com/guide/topics/graphics/hardware-accel.html

关于android - 如何使用 Android SpannableString 和 MaskFilterSpan 按预期模糊 TextView 中的某些文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46089196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com