<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Android仿echo評論彈幕效果

          共 7279字,需瀏覽 15分鐘

           ·

          2021-05-09 10:08

          我是echo回聲的忠實粉絲,里面的各種3D音效真是美滴很美滴很。這個App的開發(fā)組是完美主義,被深深吸引無法自拔。尤其是彈幕的神評論更加搞笑。今天,我就仿著echo的彈幕純手打了一個效果出來。

          是時候來一發(fā)炸裂的開場了:


          寫這個功能之前,我低下頭深深沉思,我要做什么功能呢,要怎么做到呢?寫程序要一步一步來,步子大了會扯著蛋,于是我在紙上列出:


              1、創(chuàng)建一個Relativelayout,在里面添加一個moveView(每一個彈幕view)。

              2、寫下這個moveView和該布局。

              3、給moveView添加translation動畫從右往左跑出, 動畫開始添加,動畫結(jié)束移除該view。

              4、用定時器不停創(chuàng)建多個moveView從右往左動畫。

              5、在根據(jù)Relativelayout的高度范圍,創(chuàng)建隨機高度,設(shè)置給moveView的上邊界距離。

              6、創(chuàng)建數(shù)據(jù)源,包括頭像id,名字,彈幕文字內(nèi)容

              7、設(shè)置隨機出現(xiàn)間隔時間;設(shè)置背景圖(這里我是截的背景圖)



          我用雙手成就我的設(shè)想:

          1、創(chuàng)建MoveView布局

          <LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:gravity="center_vertical">
          <ImageView android:id="@+id/avator" android:layout_width="30dp" android:layout_height="30dp" android:background="@mipmap/internet_star"/>
          <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:background="@drawable/shape_text">
          <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Jack ma" android:textColor="#2ecc71"/>
          <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:text="哈哈哈" android:singleLine="true" android:background="@drawable/shape_text" android:textColor="#000" /> </LinearLayout>
          </LinearLayout>

          2、創(chuàng)建moveview,傳入Relativelayout高度隨機設(shè)置params.topMargin屬性值,即隨機垂直高度出現(xiàn)。
          public class MoveView extends LinearLayout{    private View rootView;    private ImageView ivAvator;    private TextView tvName;    private TextView tvContent;
          public MoveView(Context context, DanmuBean danmuBean) { super(context);
          rootView = inflate(context,R.layout.item,null); addView(rootView); init(danmuBean); }
          private void init(DanmuBean danmuBean) { ivAvator = rootView.findViewById(R.id.avator); tvName = rootView.findViewById(R.id.tv_name); tvContent = rootView.findViewById(R.id.tv_content);
          ivAvator.setImageResource(danmuBean.getAvatorId()); tvName.setText(danmuBean.getUserName()); tvContent.setText(danmuBean.getContent()); }
          /** * 設(shè)置隨機出現(xiàn)垂直位置 */ public void randomVerticalPos(int heightPixels){ int randonTop = (int) (Math.random()*heightPixels);
          RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) getLayoutParams(); params.topMargin = randonTop; setLayoutParams(params); }
          }

          3、隨機設(shè)置Translationg動畫的duration,即實現(xiàn)隨機移動速度;
              private void startTranslateAnim(final View view){        int randomDuration = (int) (Math.random()*2000 + 7000);        TranslateAnimation anim = new TranslateAnimation(widthPixels,-widthPixels,0,0);        anim.setDuration(randomDuration);        anim.setAnimationListener(new Animation.AnimationListener() {            @Override            public void onAnimationStart(Animation animation) {
          }
          @Override public void onAnimationEnd(Animation animation) { removeView(view); }
          @Override public void onAnimationRepeat(Animation animation) {
          } }); view.startAnimation(anim); }

          4、handler隨機間隔創(chuàng)建moveview
              private Handler handler = new Handler(){        @Override        public void handleMessage(Message msg) {            addTextView();
          //彈幕出現(xiàn)間隔時間400-700ms int randomDelay = (int) (Math.random()*300+400); handler.sendEmptyMessageDelayed(0,randomDelay); } };
          private void addTextView(){ if(curPos == datas.size()){ //循環(huán)播放 curPos = 0; } MoveView moveView = new MoveView(getContext(),datas.get(curPos++)); addView(moveView); moveView.randomVerticalPos(heightPixels); startTranslateAnim(moveView); }

          5、模擬網(wǎng)絡(luò)請求彈幕數(shù)據(jù)列表,這里包括,頭像、用戶名、彈幕內(nèi)容
              private List loadData(){        ArrayList<DanmuBean> datas = new ArrayList<>();        datas.add(new DanmuBean(0,R.mipmap.internet_star,"小強","女神我愛你!"));        datas.add(new DanmuBean(0,R.mipmap.make_music_voice_changer_funny,"Jack ma","媽媽問我為什么跪著聽歌"));        datas.add(new DanmuBean(0,R.mipmap.make_music_voice_changer_lori,"習大大","歌詞特別美"));        datas.add(new DanmuBean(0,R.mipmap.make_music_voice_changer_robot,"寶強","全世界都安靜了"));        datas.add(new DanmuBean(0,R.mipmap.make_music_voice_changer_uncle,"寶寶","聽到放不下耳機,聽到耳朵痛都不放下,哈哈哈"));        datas.add(new DanmuBean(0,R.mipmap.internet_star,"我是大神","中國好聲音,I want you"));        datas.add(new DanmuBean(0,R.mipmap.internet_star,"茜茜","一天腦子里都在唱這首歌"));        datas.add(new DanmuBean(0,R.mipmap.make_music_voice_changer_funny,"Bangbangbang","有故事,好聽"));        datas.add(new DanmuBean(0,R.mipmap.make_music_voice_changer_uncle,"stalse","我就是為了你開的會員"));        datas.add(new DanmuBean(0,R.mipmap.internet_star,"hehe","太好聽了.."));        datas.add(new DanmuBean(0,R.mipmap.make_music_voice_changer_robot,"小強","女神我愛你!"));        datas.add(new DanmuBean(0,R.mipmap.make_music_voice_changer_uncle,"Jack ma","媽媽問我為什么跪著聽歌"));        datas.add(new DanmuBean(0,R.mipmap.internet_star,"習大大","歌詞特別美"));        datas.add(new DanmuBean(0,R.mipmap.make_music_voice_changer_funny,"寶強","全世界都安靜了"));        datas.add(new DanmuBean(0,R.mipmap.internet_star,"寶寶","聽到放不下耳機,聽到耳朵痛都不放下,哈哈哈"));        datas.add(new DanmuBean(0,R.mipmap.internet_star,"我是大神","中國好聲音,I want you"));        datas.add(new DanmuBean(0,R.mipmap.make_music_voice_changer_robot,"茜茜","一天腦子里都在唱這首歌"));        datas.add(new DanmuBean(0,R.mipmap.make_music_voice_changer_uncle,"Bangbangbang","有故事,好聽"));        datas.add(new DanmuBean(0,R.mipmap.internet_star,"stalse","我就是為了你開的會員"));        datas.add(new DanmuBean(0,R.mipmap.make_music_voice_changer_funny,"hehe","太好聽了.."));        datas.add(new DanmuBean(0,R.mipmap.internet_star,"小強","女神我愛你!"));        datas.add(new DanmuBean(0,R.mipmap.make_music_voice_changer_robot,"Jack ma","媽媽問我為什么跪著聽歌"));        datas.add(new DanmuBean(0,R.mipmap.make_music_voice_changer_uncle,"習大大","歌詞特別美"));        datas.add(new DanmuBean(0,R.mipmap.internet_star,"寶強","全世界都安靜了"));        datas.add(new DanmuBean(0,R.mipmap.make_music_voice_changer_funny,"寶寶","聽到放不下耳機,聽到耳朵痛都不放下,哈哈哈"));        datas.add(new DanmuBean(0,R.mipmap.internet_star,"我是大神","中國好聲音,I want you"));        datas.add(new DanmuBean(0,R.mipmap.make_music_voice_changer_uncle,"茜茜","一天腦子里都在唱這首歌"));        datas.add(new DanmuBean(0,R.mipmap.internet_star,"Bangbangbang","有故事,好聽"));        datas.add(new DanmuBean(0,R.mipmap.make_music_voice_changer_funny,"stalse","我就是為了你開的會員"));        datas.add(new DanmuBean(0,R.mipmap.internet_star,"hehe","太好聽了.."));
          return datas; }

          源碼地址:
          https://github.com/18380438200/Danmu

          到這里就結(jié)束啦。
          瀏覽 171
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  大香蕉免费福利视频 | 亚洲最大免费黄色 | 操逼毛片视频 | 久久久青| 人妻在线大香蕉 |