??
Android Bullet Screen realization: Based on the B-Station bullet screen Open source system (2)
On the basis of appendix 1, the simulation realizes a practical development of the application scenario: from the network uninterrupted period of time to take the bomb screen data, these barrage data is often batch, and then take these from the network to the batch data from the display. Note the use of handler and thread-safe queue concurrentlinkedqueue in this example.
Java code:
Package Zhangphil.danmaku;import Android.app.activity;import Android.graphics.color;import android.os.Bundle; Import Android.os.handler;import Android.os.message;import Android.support.annotation.nonnull;import Android.text.textutils;import Android.util.log;import Android.view.view;import Android.widget.Button;import Java.util.arraylist;import Java.util.hashmap;import Java.util.concurrent.concurrentlinkedqueue;import Java.util.concurrent.scheduledthreadpoolexecutor;import Java.util.concurrent.timeunit;import Master.flame.danmaku.danmaku.model.basedanmaku;import Master.flame.danmaku.danmaku.model.danmakutimer;import Master.flame.danmaku.danmaku.model.idisplayer;import Master.flame.danmaku.danmaku.model.android.DanmakuContext; Import Master.flame.danmaku.ui.widget.danmakuview;public class Mainactivity extends Activity {private Danmakuview Mdan Makuview; Private Danmakucontext Mcontext; Private Acfundanmakuparser Mparser; Private Scheduledthreadpoolexecutor MscheduledthreadpooleXecutor = null; Private concurrentlinkedqueue<string> mqueue = null; Private final int what_get_list_data = 0XFFA01; Private final int what_display_single_danmaku = 0XFFA02; Private final int[] colors = {color.red, color.yellow, Color.Blue, Color.green, Color.cyan, Color.dkgray}; Private Handler Handler = new Handler () {@Override public void Handlemessage (Message msg) {Supe R.handlemessage (msg); Switch (msg.what) {case WHAT_GET_LIST_DATA:handler.removeMessages (what_get_list_data); ArrayList lists = (ArrayList) msg.obj; if (lists! = null &&!lists.isempty ()) {Mqueue.addall (lists); if (!mqueue.isempty ()) handler.sendemptymessage (What_display_single_danmaku); } break; Case What_display_single_danmaku:handleR.removemessages (What_display_single_danmaku); Displaydanmaku (); Break } } }; private void Displaydanmaku () {String s = mqueue.poll (); if (! Textutils.isempty (s)) {Adddanmaku (S, true); } if (!mqueue.isempty ()) handler.sendemptymessagedelayed (What_display_single_danmaku, (long) (Math.rando M () * 400) + 100); } @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mqueue = new concurrentlinkedqueue<> (); Mdanmakuview = (Danmakuview) Findviewbyid (R.id.danmakuview); Init (); Mscheduledthreadpoolexecutor = new Scheduledthreadpoolexecutor (1); Getdanmakumessagetask mtask = new Getdanmakumessagetask (); Delay of 0 seconds execution, every few seconds cycle to perform a task mscheduledthreadpoolexecutor.scheduleatfixedrate (mtask, 0, 5, timeunit.seconds); Button show = (Button) Findviewbyid (r.id.show); Button hide = (Button) Findviewbyid (r.id.hide); Button SendText = (button) Findviewbyid (R.id.sendtext); Button pause = (Button) Findviewbyid (R.id.pause); Button resume = (Button) Findviewbyid (R.id.resume); Button clear = (Button) Findviewbyid (r.id.clear); Show.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { Mdanmakuview.show (); } }); Hide.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { Mdanmakuview.hide (); } }); Sendtext.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { Send a pop-up screen sendtextmessage () Every time you click on the button; } }); Pause.setonclicklistener (New View.onclicklistener () {@Override PUBlic void OnClick (View v) {mdanmakuview.pause (); } }); Resume.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { Mdanmakuview.resume (); } }); Clear.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) { Cleardanmaku (); } }); /** * assumes that the thread task simulates the time-consuming operation of fetching barrage data from the network * */Private class Getdanmakumessagetask implements Runnable { @Override public void Run () {try {Thread.Sleep ((long) (Math.random () * 1000)); } catch (Interruptedexception e) {e.printstacktrace (); } arraylist<string> danmakulists = new arraylist<> (); int count = (int) (Math.random () * 100); for (int i = 0; i < count; i++) {Danmakulists.add ("batch numberAccording to "+ i +"-"+ math.random ()); } if (!danmakulists.isempty ()) {Message msg = Handler.obtainmessage (); Msg.what = What_get_list_data; Msg.obj = danmakulists; Handler.sendmessage (msg); }}} private void Cleardanmaku () {mqueue.clear (); Mdanmakuview.cleardanmakusonscreen (); } private void Init () {mcontext = Danmakucontext.create (); Sets the maximum number of lines to display Hashmap<integer, integer> maxlinespair = new hashmap<> (); Maxlinespair.put (BASEDANMAKU.TYPE_SCROLL_RL, 8); Maximum display of the rolling barrage 5 lines//Set whether to prohibit overlapping hashmap<integer, boolean> Overlappingenablepair = new hashmap<> (); Overlappingenablepair.put (BASEDANMAKU.TYPE_SCROLL_RL, true); Overlappingenablepair.put (Basedanmaku.type_fix_top, true); Mcontext.setdanmakustyle (Idisplayer.danmaku_style_stroken, 10)//stroke thickness. setduplicatemergingenabled (False). Setscrollspeedfactor (1.2f)//The speed of the projectile screen. Attention! The smaller the value, the faster it gets! The higher the value, the slower the speed. by Phil Setscaletextsize (1.2f)//Scaled value//.setcachestuffer (new Spannedcachestuffer (), MCa Chestufferadapter)//graphics mix use spannedcachestuffer//. Setcachestuffer (New Backgroundcachestuffer ())//Draw background using Backgrou Ndcachestuffer. Setmaximumlines (Maxlinespair). preventoverlapping (Overlappingenablepair); Mparser = new Acfundanmakuparser (); Mdanmakuview.prepare (Mparser, Mcontext); Mdanmakuview.showfps (TRUE); Mdanmakuview.enabledanmakudrawingcache (TRUE); if (Mdanmakuview! = null) {Mdanmakuview.setcallback (New Master.flame.danmaku.controller.DrawHandler.Callback () {@Override public void Updatetimer (Danmakutimer timer) {} @ Override public void drawingfinished () {} @Override public Vo ID DANMAkushown (Basedanmaku Danmaku) {log.d ("Bullet screen text", "Danmakushown text=" + danmaku.text); } @Override public void prepared () {Mdanmakuview.start (); } }); }} private void Sendtextmessage () {Adddanmaku ("Zhangphil @ csdn:" + System.currenttimemillis (), true); private void Adddanmaku (Charsequence txt, boolean islive) {Basedanmaku Danmaku = mcontext.mdanmakufactory.c Reatedanmaku (BASEDANMAKU.TYPE_SCROLL_RL); if (Danmaku = = NULL | | mdanmakuview = = NULL) {return; } danmaku.text = txt; danmaku.padding = 5; danmaku.priority = 0; May be filtered by various filters and hidden display danmaku.islive = islive; Danmaku.settime (Mdanmakuview.getcurrenttime () + 1200); Danmaku.textsize = 20f * (Mparser.getdisplayer (). Getdensity ()-0.6f); Text bullet screen font size Danmaku.textcolor = Getrandomcolor (); The color of the text Danmaku.textshadowcoLor = Getrandomcolor (); Text Bullet screen stroke color//danmaku.underlinecolor = Color.dkgray; Text bullet screen underline color danmaku.bordercolor = Getrandomcolor (); The color of the Border Mdanmakuview.adddanmaku (Danmaku); } @Override protected void OnPause () {super.onpause (); if (Mdanmakuview! = null && mdanmakuview.isprepared ()) {mdanmakuview.pause (); }} @Override protected void Onresume () {super.onresume (); if (Mdanmakuview! = null && mdanmakuview.isprepared () && mdanmakuview.ispaused ()) {Mdanmakuvie W.resume (); }} @Override protected void OnDestroy () {Super.ondestroy (); if (Mdanmakuview! = null) {//dont forget release! Mdanmakuview.release (); Mdanmakuview = null; } if (Mscheduledthreadpoolexecutor! = null) Mscheduledthreadpoolexecutor.shutdown (); }/** * Randomly select a color from a range of colors * * @return */Private inT Getrandomcolor () {int i = ((int) (Math.random () *))% Colors.length; return colors[i]; }}
Code Run Result:
Appendix:
1, "Android Bullet screen realization: Based on the B-Station bullet screen Open source system (1)" Link address: http://blog.csdn.net/zhangphil/article/details/68067100
2, "Java concurrentlinkedqueue queue thread safe operation" link address: http://blog.csdn.net/zhangphil/article/details/65936066
Android Bullet Screen realization: Based on the B-Station bullet screen Open source system (2)