Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a string shadow span in Android?

Been doing a bit of searching. I can see a method to add a shadow layer to a TextView, but I only want to shadow a span of text. I'm basically doing an EditText where user will be able to change style of text selections. One of those styles being a shadow with color of choice. There are spans for color, size, typeface, etc, but I cannot find something for shadows.

Basically I want to do something like: (Note code is from Mono Droid, but a Java answer would be helpful as well)

        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);

Any assistance or suggestion is appreciated. I'm wondering if I have to figure out how to derive my own ShadowSpan implementation from android.text.style.CharacterStyle (maybe override updateDrawState() to setShadowLayer on the TextPaint object?) or perhaps I'm just missing the simple answer? I can't be the only one who has wanted to do this, so I'd thought I'd ask before going too far with trying something custom.

-- EDIT --

I tried creating my own ShadowSpan and it does seem to work. I'm still leaving the floor open if anyone has a better solution. It just seems like something should already exist, but I guess I didn't have to do too much.

Here is what I have in 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);
    }
}

Used like so

    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);
    }
like image 808
CFD Avatar asked Dec 17 '22 00:12

CFD


1 Answers

Having thought about it more, it seems pretty simple to implement custom spans by deriving from CharacterStyle. I would guess Google didn't want to bloat the API with a bunch of one-off Span classes. I guess in the process of constructing my question I've ended up answering it. Well, hopefully this help someone else out some day. Thanks to all who posted suggestions.

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);
    }
}
like image 140
CFD Avatar answered Jan 06 '23 09:01

CFD