说明:
先上GIf图,帧数有点低,但是实际效果比这个好很多。
效果图:
源码展示:
[C#] 纯文本查看 复制代码 class DecodeLabel:Label
{
private readonly Timer _timerAnimate = new Timer();
private TextDecodeEffect _decodeEffect;
private bool _showing;
private int _initGenCount;
public int Interval
{
get { return _timerAnimate.Interval; }
set { _timerAnimate.Interval = value; }
}
public DecodeLabel()
{
_timerAnimate.Interval = 100;
_timerAnimate.Tick += _timerAnimate_Tick;
}
public void Animate(bool show, string text, int initGenCount)
{
_initGenCount = initGenCount;
_decodeEffect = new TextDecodeEffect(text) { TextVisible = !show };
Text = _decodeEffect.Peek(DecodeMode.None);
_showing = show;
_timerAnimate.Start();
}
private void _timerAnimate_Tick(object sender, EventArgs e)
{
if (_initGenCount != 0)
{
Text = _decodeEffect.GenerateNumberRange(Text.Length);
_initGenCount--;
return;
}
var decodeMode = _showing ? DecodeMode.Show : DecodeMode.Hide;
var text = _decodeEffect.Peek(decodeMode);
if (text == null)
{
_timerAnimate.Stop();
}
else
{
Text = text;
}
}
}
public enum DecodeMode
{
None,
Show,
Numbers,
Hide
}
class TextDecodeEffect
{
private int _visibleCount;
private readonly Random _random = new Random();
public bool TextVisible
{
get { return _visibleCount == OriginalText.Length;}
set { _visibleCount = value ? OriginalText.Length : 0; }
}
public string OriginalText { get; private set; }
public TextDecodeEffect(string text)
{
OriginalText = text;
}
public string Peek(DecodeMode mode)
{
switch (mode)
{
case DecodeMode.Numbers:
return GenerateNumberRange(OriginalText.Length);
case DecodeMode.Hide:
if (_visibleCount == 0) return null;
_visibleCount--;
break;
case DecodeMode.Show:
if (_visibleCount == OriginalText.Length) return null;
_visibleCount++;
break;
}
var text = GenerateNumberRange(OriginalText.Length - _visibleCount);
text += OriginalText.Substring(OriginalText.Length - _visibleCount, _visibleCount);
return text;
}
public string GenerateNumberRange(int count)
{
var SB = new StringBuilder();
for (int i = 0; i < count; i++)
SB.Append(_random.Next(0, 10));
return SB.ToString();
}
}
案例源码下载:
|