如何在maxLine TextView的最后一行应用水平衰落边缘

时间:2019-04-23 19:37:29

标签: android textview fade fadeout truncate

我想像Google Play电影那样对TextView实施淡化边缘行为:

enter image description here

如您所见,第三行的最后一个字母具有渐弱的边缘效果。 是否有办法针对通过android:maxLines定义的特定行实现这一目标? (例如android:maxLines="3"

我尝试了以下操作,但它仅与属性android:singleLine一起使用,这不是我的目标:

<TextView
    ...
    android:requiresFadingEdge="horizontal"
    android:fadingEdgeLength="30dp"
    android:ellipsize="none"
    android:singleLine="true" />

在此处设置android:maxLines不会导致完全消失。

编辑/附加:

以前,我还像here那样扩展了Shader的同时尝试了LinearGradientTextView的{​​{1}},但是所描述的解决方案应用了背景/前景(而且还有其他一些问题……)。

我想将Gradient应用于maxLine行的最后3-4个字符。可以吗?

1 个答案:

答案 0 :(得分:0)

尝试一下。.希望它能解决

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