Android手把手敲出弹幕

我是echo回声的忠实粉丝,里面的各种3D音效真是美滴很美滴很。这个App的开发组是完美主义,被深深吸引无法自拔。尤其是弹幕的神评论更加搞笑。今天,我就仿着echo的弹幕纯手打了一个效果出来。

Talk is cheap,show you the code

是时候来一发炸裂的开场了:


giphy.gif

写这个功能之前,我低下头深深沉思,我要做什么功能呢,要怎么做到呢?写程序要一步一步来,步子大了会扯着蛋,于是我在纸上列出:

1.创建一个Relativelayout,在里面添加一个moveView(每一个弹幕view)。
2.写下这个moveView和该布局。
3.给moveView添加translation动画从右往左跑出,动画开始添加,动画结束移除该view。
4.用定时器不停创建多个moveView从右往左动画。
5.在根据Relativelayout的高度范围,创建随机高度,设置给moveView的上边界距离。
6.创建数据源,包括头像id,名字,弹幕文字内容
7.设置随机出现间隔时间;设置背景图(这里我是截的背景图)

我用双手成就我的设想:

1.创建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.创建moveview,传入Relativelayout高度随机设置params.topMargin属性值,即随机垂直高度出现。

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());
    }

    /**
     * 设置随机出现垂直位置
     */
    public void randomVerticalPos(int heightPixels){
        int randonTop = (int) (Math.random()*heightPixels);

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) getLayoutParams();
        params.topMargin = randonTop;
        setLayoutParams(params);
    }

}

3.随机设置Translationg动画的duration,即实现随机移动速度;

    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随机间隔创建moveview。

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            addTextView();

            //弹幕出现间隔时间400-700ms
            int randomDelay = (int) (Math.random()*300+400);
            handler.sendEmptyMessageDelayed(0,randomDelay);
        }
    };

    private void addTextView(){
        if(curPos == datas.size()){  //循环播放
            curPos = 0;
        }
        MoveView moveView = new MoveView(getContext(),datas.get(curPos++));
        addView(moveView);
        moveView.randomVerticalPos(heightPixels);
        startTranslateAnim(moveView);
    }

5.模拟网络请求弹幕数据列表,这里包括,头像、用户名、弹幕内容。

    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;
    }

盼望着,盼望着,一直都盼望着拿时间写一个弹幕自己来用,今天终于写了。好了,咱们下期节目再见!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,902评论 1 32
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,893评论 0 17
  • 如今直播行业确实是非常火爆啊,大大小小的公司都要涉足一下直播的领域,用斗鱼的话来讲,现在就是千播之战。而弹幕则无疑...
    GB_speak阅读 8,274评论 1 30
  • 花花的世界,欢迎你^O^ 今天的好朋友:鲸鱼和海星 2号作品画:Whale&Starfish 今日色彩:深紫,橙,...
    jjlyn阅读 2,187评论 0 0
  • 亲爱的孩子们: 见字如面,今天我们在课上讲了苏轼的故事,你对他的了解有多少? 当我们登高远眺,面对滔滔江水,会禁不...
    桥溪老师阅读 3,012评论 0 0

友情链接更多精彩内容