可能是目前轻量级弹幕控件中功能最强大的一款
本項目是一個開源的彈幕控件庫,能夠支持多種樣式彈幕,彈幕點擊監聽,彈幕分區域顯示,自定義移動速度等功能,項目原理是通過自定義ViewGroup。可能是目前輕量級彈幕控件中功能最強大的一款了。
Github項目地址:github.com/hust2010107…,希望你能Star或者提交Issues.
效果
- 常規樣式
- 點擊事件
- 多種彈幕樣式
- 分區域顯示
- GIF效果圖
使用
0. 添加依賴
1. 導入xdanmuku源碼
你可以直接下載本項目xdanmuku模塊,并導入項目目錄,并添加依賴compile project(':xdanmuku')
2. Gradle
先把jitpack倉庫添加到項目根 build.gradle(Project)文件中,
allprojects {repositories {...maven { url 'https://jitpack.io' }} }復制代碼然后在你的項目中添加依賴
dependencies {compile 'com.github.hust201010701:XDanmuku:33a063b46e' }復制代碼其他添加依賴的方式,如maven等請自行到點我查看。
1. 添加控件
在布局xml中添加控件
<com.orzangleli.xdanmuku.DanmuContainerViewandroid:id="@+id/danmuContainerView"android:layout_width="match_parent"android:layout_height="240dp"/>復制代碼2. 自定義彈幕實體類DanmuEntity
根據需求定制彈幕實體類,包含所有彈幕的屬性和類型。
public class DanmuEntity {public String content;public int textColor;public int backgroundColor;public int type;public String time; }復制代碼3. 添加DanmuConverter(彈幕轉換器)
DanmuConverter中有兩個抽象方法需要實現,getSingleLineHeight是返回所有彈幕樣式中高度最大值作為彈幕航道的高度;convert負責將彈幕實體類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;} };復制代碼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(); }復制代碼5. 彈幕點擊事件監聽
//彈幕點擊事件 danmuContainerView.setOnItemClickListener(new DanmuContainerView.OnItemClickListener<DanmuEntity>() {@Overridepublic void onItemClick(DanmuEntity danmuEntity) {Toast.makeText(MainActivity.this,danmuEntity.content,Toast.LENGTH_SHORT).show();} });復制代碼6. 設置彈幕移動速度
DanmuContainerView中預設了三種彈幕移動速度:
public final static int LOW_SPEED = 1; public final static int NORMAL_SPEED = 3; public final static int HIGH_SPEED = 5;復制代碼設置速度通過setSpeed方法:
danmuContainerView.setSpeed(DanmuContainerView.HIGH_SPEED);復制代碼同時你可以傳遞具體的整數型速度:
danmuContainerView.setSpeed(4);復制代碼7. 彈幕顯示區域
本人將彈幕控件按照豎向均分為3份,分別為GRAVITY_TOP,GRAVITY_CENTER,GRAVITY_BOTTOM。用戶可以自由組合顯示區域,默認情況下全區域(GRAVITY_FULL)顯示。設置要顯示的區域通過setGravity方法實現,參數可以使用 | 進行連接。
//只在上方和中間區域顯示彈幕 danmuContainerView.setGravity(DanmuContainerView.GRAVITY_TOP | DanmuContainerView.GRAVITY_CENTER);復制代碼后記
本控件的原理你可能已經知道了使用自定義ViewGroup實現。但是之前我花了很多事件嘗試通過自定義LayoutManager讓RecyclerView實現彈幕控件,不過最終這種方案失敗了,更多細節討論歡迎發送郵件(orzangleli@163.com)給我。
歡迎Star,提交Issues。
總結
以上是生活随笔為你收集整理的可能是目前轻量级弹幕控件中功能最强大的一款的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java基础-可执行jar包
- 下一篇: 据阿里云EMR快速搭建数据平台(二)