可能是目前轻量级弹幕控件中功能最强大的一款
本項(xiàng)目是一個(gè)開源的彈幕控件庫,能夠支持多種樣式彈幕,彈幕點(diǎn)擊監(jiān)聽,彈幕分區(qū)域顯示,自定義移動速度等功能,項(xiàng)目原理是通過自定義ViewGroup。可能是目前輕量級彈幕控件中功能最強(qiáng)大的一款了。
Github項(xiàng)目地址:github.com/hust2010107…,希望你能Star或者提交Issues.
效果
- 常規(guī)樣式
- 點(diǎn)擊事件
- 多種彈幕樣式
- 分區(qū)域顯示
- GIF效果圖
使用
0. 添加依賴
1. 導(dǎo)入xdanmuku源碼
你可以直接下載本項(xiàng)目xdanmuku模塊,并導(dǎo)入項(xiàng)目目錄,并添加依賴compile project(':xdanmuku')
2. Gradle
先把jitpack倉庫添加到項(xiàng)目根 build.gradle(Project)文件中,
allprojects {repositories {...maven { url 'https://jitpack.io' }} }復(fù)制代碼然后在你的項(xiàng)目中添加依賴
dependencies {compile 'com.github.hust201010701:XDanmuku:33a063b46e' }復(fù)制代碼其他添加依賴的方式,如maven等請自行到點(diǎn)我查看。
1. 添加控件
在布局xml中添加控件
<com.orzangleli.xdanmuku.DanmuContainerViewandroid:id="@+id/danmuContainerView"android:layout_width="match_parent"android:layout_height="240dp"/>復(fù)制代碼2. 自定義彈幕實(shí)體類DanmuEntity
根據(jù)需求定制彈幕實(shí)體類,包含所有彈幕的屬性和類型。
public class DanmuEntity {public String content;public int textColor;public int backgroundColor;public int type;public String time; }復(fù)制代碼3. 添加DanmuConverter(彈幕轉(zhuǎn)換器)
DanmuConverter中有兩個(gè)抽象方法需要實(shí)現(xiàn),getSingleLineHeight是返回所有彈幕樣式中高度最大值作為彈幕航道的高度;convert負(fù)責(zé)將彈幕實(shí)體類DanmuEntity綁定到彈幕子視圖上(類似于BaseAdapter的getView方法的作用)。
DanmuConverter danmuConverter = new DanmuConverter<DanmuEntity>() {@Overridepublic int getSingleLineHeight() {//將所有類型彈幕的布局拿出來,找到高度最大值,作為彈道高度View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_danmu, null);//指定行高view.measure(0, 0);View view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_super_danmu, null);//指定行高view2.measure(0, 0);return Math.max(view.getMeasuredHeight(),view2.getMeasuredHeight());}@Overridepublic View convert(DanmuEntity model) {View view = null;if(model.getType() == 0) {view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_danmu, null);TextView content = (TextView) view.findViewById(R.id.content);ImageView image = (ImageView) view.findViewById(R.id.image);image.setImageResource(ICON_RESOURCES[random.nextInt(5)]);content.setText(model.content);content.setTextColor(Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256)));}else if(model.getType() == 1) {view = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_super_danmu, null);TextView content = (TextView) view.findViewById(R.id.content);content.setText(model.content);TextView time = (TextView) view.findViewById(R.id.time);time.setText(model.getTime());}return view;} };復(fù)制代碼4. 添加彈幕
DanmuEntity danmuEntity = new DanmuEntity(); danmuEntity.setContent(doubleSeed.substring(index, index + 2 + random.nextInt(20))); danmuEntity.setType(random.nextInt(2)); danmuEntity.setTime("23:20:11"); try {danmuContainerView.addDanmu(danmuEntity); } catch (Exception e) {e.printStackTrace(); }復(fù)制代碼5. 彈幕點(diǎn)擊事件監(jiān)聽
//彈幕點(diǎn)擊事件 danmuContainerView.setOnItemClickListener(new DanmuContainerView.OnItemClickListener<DanmuEntity>() {@Overridepublic void onItemClick(DanmuEntity danmuEntity) {Toast.makeText(MainActivity.this,danmuEntity.content,Toast.LENGTH_SHORT).show();} });復(fù)制代碼6. 設(shè)置彈幕移動速度
DanmuContainerView中預(yù)設(shè)了三種彈幕移動速度:
public final static int LOW_SPEED = 1; public final static int NORMAL_SPEED = 3; public final static int HIGH_SPEED = 5;復(fù)制代碼設(shè)置速度通過setSpeed方法:
danmuContainerView.setSpeed(DanmuContainerView.HIGH_SPEED);復(fù)制代碼同時(shí)你可以傳遞具體的整數(shù)型速度:
danmuContainerView.setSpeed(4);復(fù)制代碼7. 彈幕顯示區(qū)域
本人將彈幕控件按照豎向均分為3份,分別為GRAVITY_TOP,GRAVITY_CENTER,GRAVITY_BOTTOM。用戶可以自由組合顯示區(qū)域,默認(rèn)情況下全區(qū)域(GRAVITY_FULL)顯示。設(shè)置要顯示的區(qū)域通過setGravity方法實(shí)現(xiàn),參數(shù)可以使用 | 進(jìn)行連接。
//只在上方和中間區(qū)域顯示彈幕 danmuContainerView.setGravity(DanmuContainerView.GRAVITY_TOP | DanmuContainerView.GRAVITY_CENTER);復(fù)制代碼后記
本控件的原理你可能已經(jīng)知道了使用自定義ViewGroup實(shí)現(xiàn)。但是之前我花了很多事件嘗試通過自定義LayoutManager讓RecyclerView實(shí)現(xiàn)彈幕控件,不過最終這種方案失敗了,更多細(xì)節(jié)討論歡迎發(fā)送郵件(orzangleli@163.com)給我。
歡迎Star,提交Issues。
總結(jié)
以上是生活随笔為你收集整理的可能是目前轻量级弹幕控件中功能最强大的一款的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java基础-可执行jar包
- 下一篇: 据阿里云EMR快速搭建数据平台(二)