Android實現自訂的彈幕效果_Android

來源:互聯網
上載者:User

一、效果圖

先來看看效果圖吧~~

二、實現原理方案

1、自訂ViewGroup-XCDanmuView,繼承RelativeLayout來實現,當然也可以繼承其他三大布局類哈

2、初始化若干個TextView(彈幕的item View,這裡以TextView 為例,當然也可以其他了~),然後通過addView添加到自訂View中

3、通過addView添加到XCDanmuView中,位置在座標,為了實現 從螢幕外移動進來的效果

我們還需要修改添加進來TextView的位置,以從右向左移動方向來說,addView後必須將該TextView的位置設定到右邊的螢幕外

這樣我們採用的方法,是在onLayout()方法中對childView進行layout重新布局設定位置

4、隨機沖左側或右側出來彈幕itemView,移動採用屬性動畫來實現平移,從螢幕的一端移動到另一端,當動畫結束後,就將

該child從XCDanmuView中remove掉。並重新new 一個彈幕itemView ,並addView到XCDanmuView中,並開始動畫移動

5、本自訂彈幕View支援從左至右和從右至左兩個方向,支援自訂設定螢幕彈幕最多顯示個數。

 

三、自訂彈幕效果XCDanmuView的具體實現

1、初始化需要用到的資料變數

private int mWidth;  private int mScreenWidth;  private List<View> mChildList;  private boolean mIsWorking = false;  private Context mContext;  private int mMaxShowNum = 15;  private int mRowNum = 4;  private int[] mSpeeds = {      3000,4000,5000,6000  };  private int mDelayDuration = 500;  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、初始化若干個彈幕item view

public void initDanmuItemViews(String[] strContents){    mStrContents = strContents;    for(int i = 0; i < mMaxShowNum; i ++){      int index = mRandom.nextInt(100) % strContents.length;      createDanmuView(i,strContents[index],false);    }  }

3、建立彈幕item view 並addView到XCDanmuView中

public void createDanmuView(int index,String content,boolean reset){    final TextView textView = new TextView(mContext);    textView.setTextColor(Color.WHITE);    int r = mRandom.nextInt(100) % mRowNum;    textView.setBackgroundResource(mBgResIds[r]);    textView.setText(content +"_"+ (index+1));    RelativeLayout.LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,        RelativeLayout.LayoutParams.WRAP_CONTENT);    int row = mRandom.nextInt(100) % mRowNum;    while(row == lastRow){      row = mRandom.nextInt(100)% mRowNum;    }    int pos = mRandom.nextInt(100)% 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、重新設定childView的初始位置到螢幕之外

@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、彈幕item view的移動效果

private Handler mHandler = new Handler() {    @Override    public void handleMessage(final Message msg) {      super.handleMessage(msg);      final int pos = msg.what;      ViewPropertyAnimator animator;      if(mDirection == XCDirection.FROM_RIGHT_TO_LEFT){        animator = mChildList.get(msg.what).animate()            .translationXBy(-(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(100) % mSpeeds.length;      animator.setDuration(mSpeeds[index]);      animator.setInterpolator(new LinearInterpolator());      animator.setListener(new Animator.AnimatorListener() {        @Override        public void onAnimationStart(Animator animator) {        }        @Override        public void onAnimationEnd(Animator animator) {          XCDanmuView.this.removeView(mChildList.get(pos));          int index = mRandom.nextInt(100) % mStrContents.length;          createDanmuView(pos, mStrContents[index], true);          mHandler.sendEmptyMessageDelayed(pos, mDelayDuration);          Log.v("czm", "size=" + mChildList.size());        }        @Override        public void onAnimationCancel(Animator animator) {        }        @Override        public void onAnimationRepeat(Animator animator) {        }      });      animator.start();    }  };

 6、開啟彈幕效果和關閉彈幕效果以及對於的動畫效果

boolean isFirst = true;  public void start(){    switchAnimation(XCAction.SHOW);    if(isFirst){      for(int i =0;i< mChildList.size();i++){        mHandler.sendEmptyMessageDelayed(i,i * mDelayDuration);      }      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 onAnimationEnd(Animation animation) {        if(action == XCAction.HIDE){          XCDanmuView.this.setVisibility(View.GONE);        }else{          XCDanmuView.this.setVisibility(View.VISIBLE);        }      }      @Override      public void onAnimationRepeat(Animation animation) {      }    });  }

四、如何使用該自訂側滑View控制項

使用該自訂View非常簡單,控制項預設效果從右向左,如果需要修改方向為從左至右,只需設定下方向即可

public class MainActivity extends Activity {  private XCDanmuView mDanmuView;  private List<View> mViewList;  private String[] mStrItems = {      "搜狗","百度",      "騰訊","360",      "阿里巴巴","搜狐",      "網易","新浪",      "搜狗-上網從搜狗開始","百度一下,你就知道",      "必應搜尋-有求必應","好搜-用好搜,特順手",      "Android-Google","IOS-蘋果",      "Windows-微軟","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() {      @Override      public void onClick(View view) {        if (mDanmuView.isWorking()) {          mDanmuView.hide();          ((Button) view).setText("開啟彈幕");        } else {          mDanmuView.start();          ((Button) view).setText("關閉彈幕");        }      }    });  }  private void initDanmuView() {    mDanmuView = (XCDanmuView)findViewById(R.id.danmu);    mDanmuView.initDanmuItemViews(mStrItems);  }}

五、總結

以上就是在Android中實現自訂彈幕效果的全部內容個,希望本文的內容對大家開發Android的時候能有所協助。如果有疑問可以留言交流。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.