4

一直在做一些搜索。我可以看到一种将阴影层添加到 TextView 的方法,但我只想对一段文本进行阴影处理。我基本上是在做一个 EditText ,用户可以在其中更改文本选择的样式。其中一种风格是带有颜色选择的阴影。颜色、大小、字体等都有跨度,但我找不到阴影的东西。

基本上我想做类似的事情:(注意代码来自 Mono Droid,但 Java 答案也会有帮助)

        var N = new ShadowSpan(color,dx,dy,radius); // no such thing?
        int S = txEdit.SelectionStart;
        int E = txEdit.SelectionEnd;
        Str = new SpannableString(txEdit.TextFormatted);
        Str.SetSpan(N,S,E, SpanTypes.InclusiveInclusive);
        txEdit.SetText(Str, TextView.BufferType.Spannable);
        txEdit.SetSelection(S,E);

任何帮助或建议表示赞赏。我想知道我是否必须弄清楚如何从android.text.style.CharacterStyle派生出我自己的 ShadowSpan 实现(可能覆盖 updateDrawState() 到TextPaint 对象上的setShadowLayer?)或者我只是错过了简单的答案?我不能是唯一一个想要这样做的人,所以我想在尝试一些定制的东西之前我会先问一下。

- 编辑 -

我尝试创建自己的 ShadowSpan,它似乎确实有效。如果有人有更好的解决方案,我仍然保持开放状态。似乎某些东西应该已经存在,但我想我不必做太多。

这是我在 Mono for Android 中的内容

public class ShadowSpan : Android.Text.Style.CharacterStyle
{
    public float Dx;
    public float Dy;
    public float Radius;
    public Android.Graphics.Color Color;
    public ShadowSpan(float radius, float dx, float dy, Android.Graphics.Color color)
    {
        Radius = radius; Dx = dx; Dy = dy; Color = color;
    }

    public override void UpdateDrawState (TextPaint tp)
    {
        tp.SetShadowLayer(Radius, Dx, Dy, Color);
    }
}

像这样使用

    void HandleClick (object sender, EventArgs e)
    {
        var N = new ShadowSpan(1,1,1,Android.Graphics.Color.Red);
        int S = txEdit.SelectionStart;
        int E = txEdit.SelectionEnd;
        Str = new SpannableString(txEdit.TextFormatted);
        Str.SetSpan(N,S,E, SpanTypes.InclusiveInclusive);
        txEdit.SetText(Str, TextView.BufferType.Spannable);
        txEdit.SetSelection(S,E);
    }
4

3 回答 3

8

仔细考虑之后,通过从 CharacterStyle 派生来实现自定义跨度似乎非常简单。我猜谷歌不想用一堆一次性的 Span 类来膨胀 API。我想在构建我的问题的过程中,我最终回答了它。好吧,希望有一天这可以帮助其他人。感谢所有发布建议的人。

public class ShadowSpan : Android.Text.Style.CharacterStyle
{
    public float Dx;
    public float Dy;
    public float Radius;
    public Android.Graphics.Color Color;
    public ShadowSpan(float radius, float dx, float dy, Android.Graphics.Color color)
    {
        Radius = radius; Dx = dx; Dy = dy; Color = color;
    }

    public override void UpdateDrawState (TextPaint tp)
    {
        tp.SetShadowLayer(Radius, Dx, Dy, Color);
    }
}
于 2012-05-18T12:13:17.110 回答
4

我基本上完成了你所做的,除了 Java,它对我有用。

private static class ShadowStyleSpan extends ClickableSpan {
    private int radius;
    private int dx;
    private int dy;
    private int color;

    public ShadowStyleSpan(int radius, int dx, int dy, int color) {
        super();
        this.radius = radius;
        this.dx = dx;
        this.dy = dy;
        this.color = color;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        ds.setShadowLayer(radius, dx, dy, color);
    }
}
于 2012-06-05T23:57:29.983 回答
0

也许你会考虑使用android:shadowColor , android:shadowDx, android:shadowDy, android:shadowRadius; 或者setShadowLayer()

检查它包含自定义文本示例的链接(向下滚动到文本阴影部分)

也看到这个

于 2012-05-17T20:41:07.230 回答