如何在Android中执行字符串阴影跨度?

CFD*_*CFD 3 android xamarin.android

正在做一些搜索.我可以看到一个向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);
Run Code Online (Sandbox Code Playgroud)

任何帮助或建议表示赞赏.我想知道我是否必须弄清楚如何从android.text.style.CharacterStyle派生我自己的ShadowSpan实现(可能在textPaint对象上覆盖updateDrawState()到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);
    }
}
Run Code Online (Sandbox Code Playgroud)

像这样使用

    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);
    }
Run Code Online (Sandbox Code Playgroud)

CFD*_*CFD 7

考虑到它之后,通过派生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);
    }
}
Run Code Online (Sandbox Code Playgroud)