//2.[初始化part-3]-通过向canvas中添加一个FontSize大小的弹幕来获取在canvas中显示时的真是尺寸 private void libInit(initCompleteHandler initCompleted) { BarrageUIBlock _testBarrage = new BarrageUIBlock(); _testBarrage.Text = "Hello,World!"; _testBarrage.FontFamily = (FontFamily) new FontFamilyConverter().ConvertFromString("Microsoft YaHei"); _testBarrage.Name = "uni_testforheight"; _testBarrage.FontSize = barrage_FontSize; _testBarrage.FontWeight = FontWeights.Bold; _testBarrage.Loaded += delegate(object sender, RoutedEventArgs e) { calcRow(system_RenderCanvas.Height, _testBarrage.Name, _testBarrage.ActualHeight); if (system_enableAPCS) { preventBarrageCover(initCompleted); } else { initCompleted(); } }; system_RenderCanvas.Children.Add(_testBarrage); system_RenderCanvas.RegisterName(_testBarrage.Name, _testBarrage); }
//3.[初始化part-3]-辅助函数 private void calcRow(double _renderHeight, string _testTargetName, double _fontHeight) { //canvas中移除该元素 BarrageUIBlock _testTargetBarrage = system_RenderCanvas.FindName(_testTargetName) as BarrageUIBlock; system_RenderCanvas.Children.Remove(_testTargetBarrage); system_RenderCanvas.UnregisterName(_testTargetName); _maxRow = (int)(_renderHeight / _fontHeight); _barrage_rowHeight = (int)_fontHeight; Console.WriteLine(_renderHeight); Console.WriteLine(_fontHeight); Console.WriteLine(_maxRow); }
//1.生成弹幕 public void createBarrage(string _content, int _targetRow, int _rowHeight, int _fontSize, int _duration, byte _R, byte _G, byte _B, bool _enableShadow) { BarrageUIBlock _singleBarrage = new BarrageUIBlock(); _singleBarrage.Text = _content; _singleBarrage.FontFamily = (FontFamily) new FontFamilyConverter().ConvertFromString("Microsoft YaHei"); _singleBarrage.Name = "uni_" + getRandomString(ra.Next(5, 8)); _singleBarrage.FontSize = _fontSize; _singleBarrage.SetValue(Canvas.TopProperty, (double)_targetRow * _rowHeight); _singleBarrage.Fill = new SolidColorBrush(Color.FromRgb(_R, _G, _B)); _singleBarrage.CacheMode = new BitmapCache(); _singleBarrage.FontWeight = FontWeights.Bold; if (_enableShadow == true) { DropShadowEffect _ef = new DropShadowEffect(); _ef.RenderingBias = RenderingBias.Performance; _ef.Opacity = (double)100; _ef.ShadowDepth = (double)0; _ef.BlurRadius = (double)11; if (_R == 0 && _G == 0 && _B == 0) { _ef.Color = Color.FromRgb(255, 255, 255); } else { _ef.Color = Color.FromRgb(0, 0, 0); } _singleBarrage.Effect = _ef; } _singleBarrage.Loaded += delegate(object sender, RoutedEventArgs e) { doAnimation(_singleBarrage.Name, _duration, _targetRow);//添加动画(弹幕移动) }; system_RenderCanvas.Children.Add(_singleBarrage); system_RenderCanvas.RegisterName(_singleBarrage.Name, _singleBarrage); if (system_enableAPCS) { lockRow(_targetRow);//置为忙状态 } }
//2.添加动画(移动)效果 private void doAnimation(string _targetUniqueName, int _duration, int _row) { BarrageUIBlock _targetBarrage = system_RenderCanvas.FindName(_targetUniqueName) as BarrageUIBlock; double _barrageWidth = _targetBarrage.ActualWidth; DoubleAnimation _doubleAnimation = new DoubleAnimation(system_RenderCanvas.Width, -_barrageWidth, new Duration(TimeSpan.FromMilliseconds(_duration)), FillBehavior.Stop); Storyboard _sb = new Storyboard(); Storyboard.SetTarget(_doubleAnimation, _targetBarrage); Storyboard.SetTargetProperty(_doubleAnimation, new PropertyPath("(Canvas.Left)")); _sb.Completed += delegate(object sender, EventArgs e) { removeOutdateBarrage(_targetUniqueName, _row); }; _sb.Children.Add(_doubleAnimation); _sb.Begin(); }
//3.移除canvas中已经显示完成的弹幕 private void removeOutdateBarrage(string _targetUniqueName, int _row) { BarrageUIBlock ready2remove = system_RenderCanvas.FindName(_targetUniqueName) as BarrageUIBlock; if (ready2remove != null) { system_RenderCanvas.Children.Remove(ready2remove); system_RenderCanvas.UnregisterName(_targetUniqueName); ready2remove = null; if (system_enableAPCS) { unlockRow(_row); } } else { Console.WriteLine("移除弹幕失败!"); } }