首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Android SpannableString和MaskFilterSpan来像预期的那样模糊来自TextView的一些文本?

如何使用Android SpannableString和MaskFilterSpan来像预期的那样模糊来自TextView的一些文本?
EN

Stack Overflow用户
提问于 2017-09-07 06:22:21
回答 1查看 563关注 0票数 1

我所期望的是这样的。

这是一个电影评论,当一些文字可能会破坏故事,它将被这些标签扰流板包装。因此,我编写了方法,找出每个扰流词的开始索引和结束索引,然后使用MaskFilterSpan模糊它。

但结果并不好。

如果我只需将MaskFilterSpan替换为StrikethroughSpan或BackgroundColorSpan,它就会工作得很好。

我的代码:

代码语言:javascript
运行
复制
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的一个bug?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-07 06:26:27

如果在AndroidManifest.xml.中对您的活动设置android:hardwareAccelerated="false",则它将正确呈现。

硬件加速不支持BlurMaskFilter。

您可以在这里检查不支持的绘图操作:

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46089196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档