<span id="Label3"></p><p><p>first, The Harassment Phone page is detected similar to 360:</p></p><p><p><br></p></p><p><p>The layout is simple, above is a relativelayout, the following Button.<br></p></p><p><p>Function:</p></p><p><p>(1) after the barrage is generated, it automatically scrolls from the right to the left (translateanimation) and is removed immediately after the barrage Disappears.</p></p><p><p>(2) the position of the projectile appears randomly and does not repeat (prevents the text from overlapping).</p></p><p><p>(3) the font size is randomly changed within a certain range, and the font color can be set.</p></p><p><p>(4) Custom First deceleration, after the acceleration of the interpolator, the barrage to accelerate the entry, deceleration stay, and then accelerate Out.</p></p><p><p>1.Activity code:</p></p><p><p></p></p><pre name="code" class="java">/** * Simple bullet screen effect implementation * Created by admin on 15-6-4. */public class Mainactivity extends actionbaractivity {private MyHandler handler; The content of the barrage is private Tanmubean tanmubean; The parent component that placed the content of the bullet screen private relativelayout containervg; The height of the parent component, private int validheightspace; @Override protected void onCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); CONTAINERVG = (relativelayout) Findviewbyid (r.id.tanmu_container); Tanmubean = new Tanmubean (); Tanmubean.setitems (new string[]{"test", "bomb screen This thing really bad do ah", "always come up with various problems ~ ~", "do not know why?" Trouble! "and who is the great God who can help me?" "I need your help.", "test it", "it's A bad thing to do", "always come up with all kinds of problems ~ ~", "do not know why?" Trouble! "and who is the great God who can help me?" "I need your help.", "test it", "it's A bad thing to do", "always come up with all kinds of problems ~ ~", "do not know why?" Trouble! "and who is the great God who can help me?" "," I Need your help. "}); Handler = new MyHandler (this); Start the barrage View Starttanmuview = Findviewbyid (r.id.starttanmu); STarttanmuview.setonclicklistener (new view.onclicklistener () {@Override public void OnClick (View V) {if (containervg.getchildcount () > 0) {return; } existmarginvalues.clear (); New Thread (new createtanmuthread ()). start (); } }); }//2s automatically add a pop-up screen private class Createtanmuthread implements Runnable {@Override public void run () { int N = Tanmubean.getitems (). length; for (int i = 0; i < N; i++) {handler.obtainmessage (1, i, 0). sendtotarget (); Systemclock.sleep (2000); }}}//need to add components in mainline City private static class MyHandler extends Handler {private Weakreference<maina Ctivity> ref; MyHandler (mainactivity Ac) {ref = new Weakreference<> (ac); } @Override public void Handlemessage (Message Msg) {super.handlemEssage (msg); if (msg.what = = 1) {mainactivity ac = ref.get (); If (ac! = null && ac.tanmubean! = null) {int index = msg.arg1; String content = Ac.tanmuBean.getItems () [index]; float textSize = (float) (ac.tanmuBean.getMinTextSize () * (1 + math.random () * Ac.tanmuBean.getRange ())); int textcolor = Ac.tanmuBean.getColor (); Ac.showtanmu (content, textSize, textcolor); }}}} private void Showtanmu (String content, float textSize, int textcolor) {final TEXTV Iew TextView = new TextView (this); Textview.settextsize (textSize); Textview.settext (content);//textview.setsingleline (); Textview.settextcolor (textcolor); int leftMargin = containervg.getright ()-containervg.getleft ()-containervg.getpaddingleft (); Calculates the TopMargin (random value, but not the one already in the screen) of this bullet, int VerticalmaRgin = Getrandomtopmargin (); Textview.settag (verticalmargin); Layoutparams params = new Layoutparams (layoutparams.wrap_content, layoutparams.wrap_content); Params.addrule (relativelayout.align_parent_top); Params.topmargin = verticalmargin; Textview.setlayoutparams (params); Animation anim = Animationhelper.createtranslateanim (this, leftMargin,-screenutils.getscreenw (this)); Anim.setanimationlistener (new animation.animationlistener () {@Override public void Onanimationstart (Animation Animation) {} @Override public void Onanimationend (Animation Animation) {//remove the component Containervg.removeview (textView); Remove the placeholder int verticalmargin = (int) Textview.gettag (); Existmarginvalues.remove (verticalmargin); } @Override public void Onanimationrepeat (Animation Animation) {}}); Textview.startanimation (anim); Containervg.addview (textView); }//record the current position of the barrage that is still showing (avoid duplication) private set<integer> existmarginvalues = new Hashset<> (); private int linescount; private int Getrandomtopmargin () {//calculates The space height to be used for the barrage display if (validheightspace = = 0) {validheightspace = Containervg.getbottom ()-containervg.gettop ()-containervg.getpaddingtop ()-containervg.getpadding Bottom (); }//calculates the number of available rows if (linescount = = 0) {linescount = validheightspace/screenutils.dp2px (this, tanmub Ean.getmintextsize () * (1 + tanmubean.getrange ())); if (linescount = = 0) {throw new runtimeexception ("not enough space to show text."); }}//check overlap While (true) {int Randomindex = (int) (math.random () * linescount); int marginvalue = Randomindex * (validheightspace/linescount); If (!existmarginvalues.contains(marginvalue)) {existmarginvalues.add (marginvalue); Return marginvalue; } } }}</pre>2. Pan the animation generation Tool:<p><p></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">public class Animationhelper { /** * Creates a translation animation * /public static Animation Createtranslateanim (Context context, int fromX, int toX) { translateanimation tlanim = new translateanimation (fromX, toX, 0, 0); Automatic calculation time long duration = (long) (math.abs (tox-fromx) * 1.0f/screenutils.getscreenw (context) * 4000); Tlanim.setduration (duration); Tlanim.setinterpolator (new decelerateaccelerateinterpolator ()); Tlanim.setfillafter (true); return tlanim;} }</pre></pre>The Screenutils is the tool class used to get the screen width high, the DP and PX.<p><p></p></p><p><p>3. Custom interpolator, in fact, there is only one line of code</p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">The public class Decelerateaccelerateinterpolator implements Interpolator { //input from 0~1, and the return value is also from 0~1. curve characterization of return values speed plus minus trend @Override public float getinterpolation (float input) { return (float) (math.tan (input * 2-1)/4 * Math.PI )/2.0f + 0.5f;} }</pre></pre>4.TanmuBean is an entity class<p><p></p></p><p><p></p></p><pre name="code" class="java"><pre name="code" class="java">public class Tanmubean { private string[] items; private int color; private int mintextsize; Private float range; Public Tanmubean () { //init default value color = Color.parsecolor ("#eeeeee"); Mintextsize = +; Range = 0.5f; } Public string[] getItems () { return items; } public void Setitems (string[] items) { this.items = items; } public int GetColor () { return color; } public void SetColor (int color) { this.color = color; } /** * min textSize, in dp. * /public int getmintextsize () { return mintextsize; } public void Setmintextsize (int mintextsize) { this.mintextsize = mintextsize; } Public float GetRange () { return range; } public void SetRange (float range) { this.range = range; }}</pre></pre><br><br><br><br><br><p><p></p></p><p><p><br></p></p><p><p><br></p></p><p><p>Android: Simple bullet screen effect implementation</p></p></span>
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