Simple Video bullet screen function realization

Source: Internet
Author: User
Tags prepare visibility

I also read the blog before, relatively simple, more clear thinking, I used a lazy frame, do not do findbyid and monitoring events

Public classMainactivityextendsActivity {@BindView (r.id.Edit_text) EditTextEditText; @BindView (r.id.Send) ButtonSend; @BindView (r.id.Operation_layout) LinearLayoutOperationlayout; /*danmakucontext can be used to set the various global configurations of the barrage, such as setting the font, setting the maximum number of lines displayed, etc. */PrivateDanmakucontextDanmakucontext;Private BooleanShowdanmaku; @BindView (r.id.Video) VideoviewVideo;/* Video player */@BindView (r.id.Activity_main) relativelayoutActivitymain; @BindView (r.id.Danmaku_view) DanmakuviewDanmakuview; /* Parser for bullet screen */PrivateBasedanmakuparserParser=NewBasedanmakuparser () {@OverrideprotectedIdanmakus Parse () {return newDanmakus ();

    }
    }; @Overrideprotected voidOnCreate (@Nullable Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.Activity_main); Butterknife.bind ( This); /* Set the address of a video file */Video. Setvideopath (Environment.getexternalstoragedirectory () +"/pixels.mp4"); /* Start playing */Video. Start (); /* Just set some elevation drawing efficiency and call the Setcallback () method to set the callback function. */Danmakuview. Enabledanmakudrawingcache (true);Danmakuview. Setcallback (NewDrawhandler.callback () {@OverridePublic voidPrepared () {Showdanmaku=true;Danmakuview. Start ();
            Generatesomedanmaku (); } @OverridePublic voidUpdatetimer (Danmakutimer timer) {} @OverridePublic voidDanmakushown (Basedanmaku danmaku) {} @OverridePublic voidDrawingfinished () {}}); /* There is nothing good to set, directly by default */Danmakucontext= Danmakucontext.create (); /*prepare () method to prepare */Danmakuview. Prepare (Parser,Danmakucontext); GetWindow (). Getdecorview (). Setonsystemuivisibilitychangelistener (NewView.onsystemuivisibilitychangelistener () {@OverridePublic voidOnsystemuivisibilitychange (intVisibility) {if(visibility = = View.system_ui_flag_visible) {onwindowfocuschanged (true);

    }
            }
        }); }/* Immersion mode onwindowfocuschanged, just so that the picture does not have a navigation bar, if the video is generally horizontal * for the interface better look */@OverridePublic voidOnwindowfocuschanged (BooleanHasfocus) {Super. onwindowfocuschanged (Hasfocus); /* The following code can be directly glued, this mode 5.0 system will have, so first make a judgment, and then in the GetWindow (). Getdecorview () method gets the Decorview of the current interface Setsystemuivisibili Ty () set his visibility, the following are his methods, details Baidu http://blog.csdn.net/guolin_blog/article/details/51763825 * */if(Hasfocus && build.version.Sdk_int>=) {View Decorview = GetWindow (). Getdecorview (); Decorview.setsystemuivisibility (View.system_ui_flag_layout_stable | View.system_ui_flag_layout_hide_navigation | View.System_ui_flag_layout_fullscreen | View.system_ui_flag_hide_navigation | View.System_ui_flag_fullscreen | View.System_ui_flag_immersive_sticky); }}///3rd step in the configuration list, set his horizontal screen, configure the SD card permissions/////////////////The 4th step is the implementation of the screen, so you can use a danmakuflamemaster first to add a dependent App/build.gradle file, and in D The following dependencies are added to the ependencies closure: Compile ' com.github.ctiao:danmakuflamemaster:0.5.3 ' 5th step can be referenced in the layout, one thing to remember Danmakuview written in

    Videoview, because the controls added after Relativelayout are overwritten.
     * *//**danmakuview is already working, but there is no information on the screen, we can not see the effect, * so we also add a pop-up message to the function. * Observe the Adddanmaku () method, this method is used to add a pop-up message to the Danmakuview. * Add a pop-up screen to the pop-up view * *@paramcontentThe specific contents of the barrage *@paramWithborderWhether the barrage has a border */private voidAdddanmaku (String content,BooleanWithborder) {/*basedanmaku Creates an instance, TYPE_SCROLL_RL indicates that this is a right-to-left scrolling barrage */Basedanmaku Danmaku =Danmakucontext.mdanmakufactory. Createdanmaku (Basedanmaku.TYPE_SCROLL_RL); /* Set the color font display time of the barrage, etc. */Danmaku.text= content; Danmaku.padding= 5; Danmaku.textSize= sp2px (20); /* Text is set to white, */Danmaku.TextColor= Color. White; Danmaku.settime (Danmakuview. GetCurrentTime ()); /* Whether with a box */if(Withborder) {Danmaku.bordercolor= Color.GREEN; }Danmakuview. Adddanmaku (Danmaku); }/** * Randomly generates some barrage content for testing */private voidGeneratesomedanmaku () {NewThread (NewRunnable () {@OverridePublic voidRun () { while(Showdanmaku) {intTime =NewRandom (). Nextint (300); String content ="" + Time + time; Adddanmaku (Content,false);Try{Thread.Sleep (time); }Catch(Interruptedexception e)
                    {E.printstacktrace ();
    }}}). Start ();
     }/** * Method of SP turn px. */public intSP2PX (floatSpvalue) {Final FloatFontscale = Getresources (). Getdisplaymetrics ().scaleddensity;return(int) (Spvalue * fontscale + 0.5f); } @OnClick (R.id.Send)Public voidonviewclicked (view view) {Switch(View.getid ()) { CaseR.id.Send: String content =EditText. GetText (). toString ();if(! Textutils.isempty (content)) {Adddanmaku (content),true);EditText. SetText (""); } Break;
    }}/*danmakuview set a Click event, which triggers the Click event when the screen is clicked. Then the judgment, if the interface is hidden to show it, if the interface is displayed to hide it, so you can simply click on the screen to achieve the operation of the interface hidden and display. */@OnClick (r.id.Danmaku_view)Public voidOnviewclicked () {if(Operationlayout. getvisibility () = = View.GONE) {Operationlayout. setvisibility (View.VISIBLE); }Else{Operationlayout. setvisibility (View.GONE);
        }/* Next we register a click event for the Send button, get the input in EditText when you click Send, and then call the Adddanmaku () method to add the message to Danmakuview.
In addition, this barrage is sent by ourselves, so the second parameter of the Adddanmaku () method is passed to true. Finally, the system's global UI changes are monitored to ensure that the program can always be immersed in an immersive mode, since it causes the focus to be lost when the Input method pops up, thus exiting the immersive mode. */GetWindow (). Getdecorview (). Setonsystemuivisibilitychangelistener (NewView.onsystemuivisibilitychangelistener () {@OverridePublic voidOnsystemuivisibilitychange (intVisibility) {if(visibility = = View.system_ui_flag_visible) {onwindowfocuschanged (true);
    }
                    }
                }); } @Overrideprotected voidOnresume () {Super. Onresume ();if(Danmakuview!=NULL&&Danmakuview. isprepared () &&Danmakuview. ispaused ()) {Danmakuview. resume (); }} @Overrideprotected voidOnPause () {Super. OnPause ();if(Danmakuview!=NULL&&Danmakuview. isprepared ()) {Danmakuview. Pause (); }} @Overrideprotected voidOnDestroy () {Super. OnDestroy ();Showdanmaku=false;if(Danmakuview!=NULL) {Danmakuview. Release ();Danmakuview=NULL; }
    }


}
------------------------------------------------------------------------------
<?XML version="1.0"encoding="Utf-8"?> <Relativelayoutxmlns:Android="Http://schemas.android.com/apk/res/android" Android: Layout_width="Match_parent" Android: layout_height="Match_parent" Android: id="@+id/activity_main" Android: background="#000"> <!--videoview Local video player--<Videoview Android: id="@+id/video" Android: Layout_width="Match_parent" Android: layout_height="Wrap_content" Android: layout_centerinparent="true"/> <!--bomb screen layout-<Master.flame.danmaku.ui.widget.DanmakuView Android: Layout_width="Match_parent" Android: layout_height="Match_parent" Android: id="@+id/danmaku_view"/> <LinearLayout Android: id="@+id/operation_layout" Android: Layout_width="Match_parent" Android: layout_height="50DP" Android: layout_alignparentbottom="true" Android: background="#fff" Android: visibility="Gone"> <EditText Android: id="@+id/edit_text" Android: Layout_width="0DP" Android: layout_height="Match_parent" Android: layout_weight="1" /> <Button Android: id="@+id/send" Android: Layout_width="Wrap_content" Android: layout_height="Match_parent" Android: text="Send"/> </LinearLayout> </Relativelayout>
----------------------------------------------------------------------------------
<android: name=/>
<android: name=android: screenorientation=" Landscape "
    android: configchanges=" Orientation|keyboardhidden |screenlayout|screensize ">




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.