Many projects need to use the effect of the screen, especially in the video when the need to show the other people's play, including their own hair.
Try writing this effect today.
The idea is to move the animation effect from right to left, font content, font size, bounce speed and other attributes together with TextView encapsulated into Barrageitem, and the control effect and Barrageitem binding in Barrageview to display. The idea is still relatively simple. I'm not taking into account the expressive barrage, and I'm going to keep it up to date.
First look at the effect:
Project directory Structure:
Next define Barrageitem.class: This class will be textview with right-to-left animation effects, font content, font size, bounce speed and other properties of the screen binding.
public class Barrageitem {public
TextView TextView;
public int textcolor;
public String text;
public int textsize;
Move speed public
int movespeed;
Position in vertical direction public
int verticalpos;
Font display occupied width public
int textmeasuredwidth;
}
Then define Barrageview, because the screen font color size and moving speed are random, you need to define the maximum minimum value to limit their range, and then by generating random numbers to set their values in this range. Also need to define the text of the screen, here is a direct write dead some fixed values.
Barrageview.class:
public class Barrageview extends Relativelayout {private context mcontext;
Private Barragehandler Mhandler = new Barragehandler ();
Private Random Random = new Random (System.currenttimemillis ());
Minimum interval of two shells private static final long barrage_gap_min_duration = 1000;
Maximum interval of two shells private static final long barrage_gap_max_duration = 2000;
Speed, ms private int maxspeed = 12000;
Speed, ms private int minspeed = 8000;
Literal maximum value private int maxSize = 50;
Text minimum value private int minsize = 10;
private int totalheight = 0; private int lineheight = 0;//the height of each row of the screen private int totalline = 0;//The number of rows of the barrage private string[] Itemtext = {"They all say Cai Wise Very handsome, but he always thinks he is very ugly "," they all say Cai is a male god, but he only thinks he is a boy, "Cai Wisdom is not a male God, Cai is a boy", "cai Wisdom seems to be gay", "Cai is Curved", "Cai is curved, fortunately now break back", "he has withstood his this The age should not have wit and handsome, he is very tired "," I hate their beauty value, I feel that their talent is the place to attract others, "why is he not interested in sister?" Why? "," why he does not want to fall in love, "he does not want to be loved by others, but also do not expect others to love him, he has been accustomed to a person", "whether his heart is living an old child", "his world has been he and his shadow,Until she met her "," she led him out of his own world, changing his many views "," he gradually found himself inseparable from him, he chose not to suppress himself "," because he is not the age of powerlessness, "she often said he was high cold, now more and more feel he hates stuffy", "began he has been with her to keep
Friend distance, but he found himself not at all "};
private int textcount;
Public Barrageview (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr);
Mcontext = context;
_init ();
Public Barrageview (context, AttributeSet attrs) {This (context, NULL, 0);
The public Barrageview {This (context, NULL);
private void _init () {textcount = Itemtext.length;
int duration = (int) ((barrage_gap_max_duration-barrage_gap_min_duration) * Math. Random ());
Mhandler.sendemptymessagedelayed (0, duration); @Override public void Onwindowfocuschanged (Boolean haswindowfocus) {super.onwindowfocuschanged (HASWINDOWFO
CUS);
Totalheight = Getmeasuredheight ();
Lineheight = Getlineheight ();
Totalline = Totalheight/lineheight; } Private VOID Generateitem () {Barrageitem item = new Barrageitem ();
String tx = itemtext[(int) (Math.random () * textcount)];
int sz = (int) (MinSize + (maxsize-minsize) * Math.random ());
Item.textview = new TextView (mcontext);
Item.textView.setText (TX);
Item.textView.setTextSize (SZ);
Item.textView.setTextColor (Color.rgb) (Random.nextint (256), Random.nextint (256), Random.nextint (256));
Item.textmeasuredwidth = (int) gettextwidth (item, TX, SZ);
Item.movespeed = (int) (Minspeed + (maxspeed-minspeed) * Math.random ());
if (Totalline = = 0) {totalheight = Getmeasuredheight ();
Lineheight = Getlineheight ();
Totalline = Totalheight/lineheight;
} Item.verticalpos = Random.nextint (totalline) * lineheight;
Showbarrageitem (item);
} private void Showbarrageitem (final Barrageitem item) {int leftMargin = This.getright ()-This.getleft ()
-This.getpaddingleft (); Layoutparams PArams = new Layoutparams (layoutparams.wrap_content, layoutparams.wrap_content);
Params.addrule (Relativelayout.align_parent_top);
Params.topmargin = Item.verticalpos;
This.addview (Item.textview, params);
Animation anim = Generatetranslateanim (item, LeftMargin); Anim.setanimationlistener (New Animation.animationlistener () {@Override public void Onanimationstart (Anima
tion arg0) {} @Override public void Onanimationrepeat (Animation arg0) {}
@Override public void Onanimationend (Animation arg0) {item.textView.clearAnimation ();
BarrageView.this.removeView (Item.textview);
}
});
Item.textView.startAnimation (ANIM); Private Translateanimation Generatetranslateanim (barrageitem item, int leftMargin) {translateanimation
Anim = new Translateanimation (leftMargin,-item.textmeasuredwidth, 0, 0); Anim.setduration (Item.movespEED);
Anim.setinterpolator (New Acceleratedecelerateinterpolator ());
Anim.setfillafter (TRUE);
return anim; /** * Calculates the length of strings in TextView * * @param item * @param text * @param Size * For the string to be computed
Font Size * @return The length of the string in TextView * * Public float Gettextwidth (barrageitem item, string text, float size) {
Rect bounds = new Rect ();
Textpaint paint;
Paint = Item.textView.getPaint ();
Paint.gettextbounds (text, 0, text.length (), bounds);
return Bounds.width ();
/** * Get the maximum height of each row of shells * * Private int getlineheight () {Barrageitem item = new Barrageitem ();
String tx = Itemtext[0];
Item.textview = new TextView (mcontext);
Item.textView.setText (TX);
Item.textView.setTextSize (maxSize);
Rect bounds = new Rect ();
Textpaint paint;
Paint = Item.textView.getPaint ();
Paint.gettextbounds (TX, 0, Tx.length (), bounds);
return Bounds.height (); } classBarragehandler extends Handler {@Override public void Handlemessage (msg) {Super.handlemessage (M
SG);
Generateitem (); The time generated by each barrage is random int duration = (int) (barrage_gap_max_duration-barrage_gap_min_duration) * Math. Rando
m ());
This.sendemptymessagedelayed (0, duration);
}
}
}
If the vertical position of the projectile appears to be random, there will be the overlap of the projectile in the vertical direction, so it is necessary to divide the vertical direction according to the height of the projectile, and then let the shells randomly distribute at these specified vertical positions. This value is computed in onwindowfocuschanged because the height of the getmeasuredheight () in the view is not NULL in this method.
Mainactivity.class:
<span style= "FONT-SIZE:18PX;" >public class Mainactivity extends activity {
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
}
</span>
The above is the source of the curtain, in fact, I think there is less of their own to send the function of the screen, I will be updated to go up in the future, mutual encouragement.