Example #1
0
        /// <summary>
        /// 发送一条弹幕
        /// </summary>
        public int Send(Danmaku danmaku)
        {
            if (DanmakuViewPool == null)
            {
                throw new ArgumentNullException("Danmaku view pool is null. Did you call init() first?");
            }
            DanmakuView view = DanmakuViewPool.Get();

            if (view == null)
            {
                MeVisual.Warn(TAG, "show: Too many danmaku, discard");
                return(RESULT_FULL_POOL);
            }
            if (DanmakuContainer == null || DanmakuContainer.Get() == null)
            {
                MeVisual.Warn(TAG, "show: Root view is null.");
                return(RESULT_NULL_ROOT_VIEW);
            }
            view.SetDanmaku(danmaku);
            // 字体大小
            int textSize = danmaku.Size;

            view.SetTextSize(ComplexUnitType.Px, textSize);
            // 字体颜色
            try
            {
                var color = Color.ParseColor(danmaku.Color);
                view.SetTextColor(color);
            }
            catch
            {
                view.SetTextColor(Color.White);
            }
            // 计算弹幕距离顶部的位置
            int marginTop = PositionCalculator.GetMarginTop(view);

            if (marginTop == -1)
            {
                // 屏幕放不下了
                MeVisual.Debug(TAG, "send: screen is full, too many danmaku [" + danmaku + "]");
                return(TOO_MANY_DANMAKU);
            }
            FrameLayout.LayoutParams p = (FrameLayout.LayoutParams)view.LayoutParameters;
            if (p == null)
            {
                p = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
            }
            p.TopMargin           = marginTop;
            view.LayoutParameters = p;
            view.SetMinHeight((int)(Config.LineHeight * 1.35));
            view.Show(DanmakuContainer.Get(), GetDisplayDuration(danmaku));
            return(RESULT_OK);
        }
 private void ScheduleCheckUnusedViews()
 {
     Checker.ScheduleWithFixedDelay(new Runnable(() =>
     {
         MeVisual.Verbose(TAG, "scheduleCheckUnusedViews: mInUseSize=" + InUseSize + ", mCacheSize=" + Cache.Count);
         long current = JavaSystem.CurrentTimeMillis();
         while (Cache != null)
         {
             DanmakuViewWithExpireTime first = Cache.FirstOrDefault();
             if (current > first.ExpireTime)
             {
                 Cache.Remove(first);
             }
             else
             {
                 break;
             }
         }
     }), 1000, 1000, TimeUnit.Milliseconds);
 }