Example analysis how to realize the animation effect of _android in Android application

Source: Internet
Author: User

In B station or other video site to see the video, often will open the effect of the screen, while watching the show side of the spit slot. The barrage looks interesting, and today we're going to achieve a simple bullet screen effect.

Intuitively, the effect is to add some view on a viewgroup, and then let the view move. So, the whole idea of realization is probably this:
1, the definition of a relativelayout, dynamic add TextView inside.
2, these textview font size, color, moving speed, the initial position are random.
3. Add TextView to the right edge of the relativelayout and add one at a time.
4, for each textview to do translation animation, so that TextView from right to left to move.
5. When TextView moves out of the screen from the left, remove TextView from the relativelayout.
With the idea below to see the specific code.
First, the Barrageitem is defined to store information about each bullet, including the font content, font size color, moving speed, vertical position, width of the font occupied, and so on.

public class Barrageitem {public 
  TextView TextView; 
  public int textcolor; 
  public String text; 
  public int textsize; 
  public int movespeed;//Move speed public 
  int verticalpos;//vertical orientation position public 
  int textmeasuredwidth;//font display width 
occupied } 

       then define Barrageview, because the size of the screen's font color and the speed of movement are random, you need to define the maximum minimum value to limit their range, They then set their values in this range by generating random numbers. Also need to define the text of the screen, here is a direct write dead some fixed values.
 
 

  Private context Mcontext; 
  Private Barragehandler Mhandler = new Barragehandler (); 
  Private Random Random = new Random (System.currenttimemillis ()); Private static final Long barrage_gap_min_duration = 1000;//minimum time interval of two shells private static final long Barrage_gap_max_durat ION = 2000;//Maximum time interval of two shells private int maxspeed = 10000;//speed, ms private int minspeed = 5000;//speed, MS private int Max 
  Size = 30;//text size, dp private int minsize = 15;//text size, dp private int totalheight = 0;  private int lineheight = 0;//the height of each row of the barrage private int totalline = 0;//The number of rows private string[] Itemtext = {"Need help", "what Are you get what to "," hahaha haha "," Occupy sofa ... 
  "," ************ "," Whether need help "," I will not easily dog belt "," Hey Hei "," This is I have seen the longest long long long long long comments "}; 
private int textcount; 
 
  Private list<barrageitem> itemList = new arraylist<barrageitem> (); 
  Public Barrageview {This (context, NULL); Public Barrageview (context, AttributeSet attrs) {The context, attrs, 0); Barrageview (context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, defstyleattr 
    ); 
    Mcontext = context; 
  Init (); 
 }

       If the vertical position displayed by the projectile is random, there will be an overlap in the vertical direction, so it is necessary to divide the vertical direction according to the height of the projectile at the maximum of the height of the screen. 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.
 

@Override public void Onwindowfocuschanged (Boolean haswindowfocus) {super.onwindowfocuschanged (haswindowfocus); 
  Totalheight = Getmeasuredheight (); 
  Lineheight = Getlineheight (); 
Totalline = Totalheight/lineheight; A bullet screen is generated at random intervals through the handler sendemptymessagedelayed.
 
The following code sets the properties of the bullet screen item. Class Barragehandler extends Handler {@Override public void Handlemessage (msg) {Super.handlemessage (M 
    SG); 
    Generateitem (); 
    Each barrage produces a random interval of int duration = (int) (barrage_gap_max_duration-barrage_gap_min_duration) * Math.random ()); 
  This.sendemptymessagedelayed (0, duration); 
  } 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); 
 }

Add each pinball item to the view and add a translateanimation animation to view, removing view from view when the animation ends.


private void Showbarrageitem (final Barrageitem item) {int leftMargin = This.getright ()-This.getleft ()-this.ge 
 
    Tpaddingleft (); 
    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 (Animati On animation) {} @Override public void Onanimationend (animation animation) {Item.textv 
        Iew.clearanimation (); 
      BarrageView.this.removeView (Item.textview); 
    @Override public void Onanimationrepeat (Animation Animation) {}}); 
  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; 
 }

This completes the function of the screen, the principle of implementation is not complicated. Can be based on specific needs to add more control, such as control each row of the barrage does not repeat, control the movement of the interpolator to produce different sliding effects and so on.

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.