Android to achieve a custom _android effect

Source: Internet
Author: User

One, the effect chart

Let's take a look at the effect chart first ~

Second, the realization principle plan

1, customization ViewGroup-XCDanmuView , inheritance RelativeLayout to achieve, of course, you can inherit the other three big layout class ha

2, initialize a number TextView of (the screen item View , here TextView for example, of course, can also be other ~), and then by addView adding to the custom View

3, by addView adding to the XCDanmuView location in the coordinates, in order to achieve the effect of moving in from the outside of the screen

We also need to modify the added TextView location to move direction from right to left, and addView then you must TextView set the position to the right side of the screen

The way we do this is to set the location of the reset in the onLayout() method childView layout

4, random rush to the left or right out itemView of the screen, moving using property animation to achieve translation, from one end of the screen to the other end, when the animation is over, will

It's time to child从XCDanmuView remove drop. and renewed new a barrage itemView , and addView into the XCDanmuView middle, and began to animate the moving

5, this custom barrage View support from left to right and from right to left two directions, support custom Settings screen pinball display up to a maximum number.

Third, the concrete realization of the xcdanmuview of the effect of the bullet screen

1, initialization needs to use the data variables

private int mwidth;
  private int mscreenwidth;
  Private list<view> mchildlist;
  Private Boolean misworking = false;
  Private context Mcontext;
  private int mmaxshownum =;
  private int mrownum = 4;
  Private int[] Mspeeds = {
      3000,4000,5000,6000
  };
  private int mdelayduration =;
  Private int[] Mbgresids = {
      r.drawable.bg_danmu0,
      r.drawable.bg_danmu1,
      R.DRAWABLE.BG_DANMU2,
      R.drawable.bg_danmu3
  };
  Private int[] Mrowpos = {
      150,140,160,150
  };
  Private Random mrandom;
  Private string[] mstrcontents;
  public static enum xcdirection{
    from_right_to_left,
    form_left_to_right
  } public
  enum xcaction{
    show,hide
  }
  Private Xcdirection mdirection = Xcdirection.from_right_to_left;
private void Init () {
  mscreenwidth = Getscreenwidth ();
  Mchildlist = new arraylist<> ();
  Mrandom = new Random ();
}

2. Initialize a number of bullet screen Item view

public void Initdanmuitemviews (string[] strContents) {
    mstrcontents = strContents;
    for (int i = 0; i < mmaxshownum i + +) {
      int index = mrandom.nextint (MB)% Strcontents.length;
      Createdanmuview (I,strcontents[index],false);
    }
  

3, create the screen item view and AddView into the Xcdanmuview

public void Createdanmuview (int index,string Content,boolean reset) {final TextView TextView = new TextView (mcontext)
    ;
    Textview.settextcolor (Color.White);
    int r = mrandom.nextint (MB)% Mrownum;
    Textview.setbackgroundresource (Mbgresids[r]);
    Textview.settext (Content + "_" + (index+1)); Relativelayout.layoutparams LP = new Layoutparams (layoutparams.wrap_content, RelativeLayout.LayoutParams.WRAP_CONT
    ENT);
    int row = mrandom.nextint (MB)% Mrownum;
    while (row = = lastrow) {row = Mrandom.nextint (m)% Mrownum;
    int pos = mrandom.nextint% Mrownum;
    Lp.topmargin = row * Mrowpos[pos];
    LastRow = row;
    TEXTVIEW.SETLAYOUTPARAMS (LP);
    Textview.setpadding (40, 2, 40, 2);
    This.addview (TextView);
    if (reset) {mchildlist.set (Index,textview);
    }else{Mchildlist.add (Index,textview);
    } textview.setclickable (True); Textview.setonclicklistener (New Onclicklistener () {@Override public void onclicK (view view) {Toast Toast = Toast.maketext (Mcontext, Textview.gettext (), toast.length_short);
        Toast.setgravity (gravity.top,0,50);
      Toast.show ();
  }
    }); }

4, reset the initial position of Childview to the screen

@Override
  protected void OnLayout (Boolean changed, int l, int t, int r, int b) {
    super.onlayout (changed, L, T, R, b);
    int childcount = This.getchildcount ();
    for (int i=0;i<childcount;i++) {
      View view = Getchildat (i);
      Relativelayout.layoutparams LP = (layoutparams) view.getlayoutparams ();
      if (lp.leftmargin <= 0) {
        if (mdirection = = xcdirection.form_left_to_right) {
          view.layout (- View.getmeasuredwidth (), Lp.topmargin,
              0,lp.topmargin + view.getmeasuredheight ());
        } else{
          view.layout (Mscreenwidth,lp.topmargin,mscreenwidth+view.getmeasuredwidth (),
              lp.topMargin+ View.getmeasuredheight ());
        }

      else{
        continue
      }}
    }
  

5. The moving effect of the item view of the play screen

Private Handler Mhandler = new Handler () {@Override public void Handlemessage (Final message msg) {Super.h
      Andlemessage (msg);
      Final int pos = msg.what;
      Viewpropertyanimator animator; if (mdirection = = xcdirection.from_right_to_left) {animator = Mchildlist.get (msg.what). Animate (). trans
      Lationxby (-(Mscreenwidth + mchildlist.get (msg.what). GetWidth ())); }else{animator = Mchildlist.get (msg.what). Animate (). Translationxby (Mscreenwidth + mchildlist.get (msg
      . what). GetWidth ());
      } Random Random = new Random (System.currenttimemillis ());
      int index = random.nextint (MB)% Mspeeds.length;
      Animator.setduration (Mspeeds[index]);
      Animator.setinterpolator (New Linearinterpolator ()); Animator.setlistener (New Animator.animatorlistener () {@Override public void Onanimationstart (animator Ani Mator) {} @Override public void Onanimationend (animator animator) {XCDanmuView.this.removeView (Mchildlist.get (POS));
          int index = mrandom.nextint (MB)% Mstrcontents.length;
          Createdanmuview (POS, Mstrcontents[index], true);
          Mhandler.sendemptymessagedelayed (POS, mdelayduration);
        LOG.V ("Czm", "size=" + mchildlist.size ()); @Override public void Onanimationcancel (animator animator) {} @Override Publ
      IC void Onanimationrepeat (animator animator) {}});
    Animator.start (); }
  };

 6, opening the screen effect and closing the effect of the flare screen and the animation effect for

Boolean isfirst = true;
    public void Start () {switchanimation (xcaction.show); if (Isfirst) {for (int i =0;i< mchildlist.size (); i++) {mhandler.sendemptymessagedelayed (I,i * mdelayduratio
      n);
    } Isfirst = false;
  } misworking = true;
    public void Hide () {switchanimation (xcaction.hide);
  Misworking =false;
    public void Stop () {this.setvisibility (view.gone);
      for (int i =0;i< mchildlist.size (); i++) {mchildlist.get (i) clearanimation ();
    Mhandler.removemessages (i);
  } misworking =false;
    } private void Switchanimation (final xcaction action) {alphaanimation animation;
      if (action = = xcaction.hide) {animation = new alphaanimation (1.0f,0.0f);
    Animation.setduration (400);
      }else{animation = new Alphaanimation (0.0f,1.0f);
    Animation.setduration (1000);
    } XCDanmuView.this.startAnimation (animation);
    Animation.setanimationlistener (New Animation.animationlistener () {  @Override public void Onanimationstart (Animation Animation) {} @Override public void Onanimati
        OnEnd (Animation Animation) {if (action = = xcaction.hide) {XCDanmuView.this.setVisibility (view.gone);
        }else{XCDanmuView.this.setVisibility (view.visible);
  @Override public void Onanimationrepeat (Animation Animation) {}}); }

Iv. How to use this custom Sideslip view control

Using this custom view is very simple, the control defaults from right to left, and if you need to change direction from left to right, just set the next direction

public class Mainactivity extends activity {private Xcdanmuview Mdanmuview;
  Private list<view> mviewlist; Private string[] Mstritems = {"Sogou", "Baidu", "Tencent", "360", "Alibaba", "Sohu", "NetEase", "Sina", "Sogou-the internet starts from the Sogou", "
  Baidu, you know, "Bing search-responsive", "good search-with good search, special handy", "android-Google", "ios-Apple", "windows-Microsoft", "Linux"};
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main);
    Initdanmuview ();
  Initlistener (); private void Initlistener () {Findviewbyid (r.id.btn). Setonclicklistener (New View.onclicklistener () {@Overr
          IDE public void OnClick (view view) {if (mdanmuview.isworking ()) {mdanmuview.hide ();
        ((button) view). SetText ("open bomb screen");
          else {mdanmuview.start ();
        ((button) view). SetText ("Close bomb screen");
  }
      }
    }); private void Initdanmuview () {Mdanmuview = (xcdanmuview) Findviewbyid (R.ID.DANMU);
  Mdanmuview.initdanmuitemviews (Mstritems); }

}

V. Summary

These are all the things you can do to achieve a custom bullet screen in Android, and hopefully the content of this article will help you when you develop Android. If you have questions, you can exchange messages.

Related Article

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.