问题描述:

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

}

网友答案:

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);
    }
}
网友答案:

I've done essentially what you've done, except in Java, and it works for me.

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);
    }
}
网友答案:

Perhaps you'd consider using android:shadowColor, android:shadowDx, android:shadowDy, android:shadowRadius; alternatively setShadowLayer().

check the link it contains examples to customize text (scroll down to Text Shadow section)

also see this one

相关阅读:
Top