日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

可能是目前轻量级弹幕控件中功能最强大的一款

發布時間:2025/4/5 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 可能是目前轻量级弹幕控件中功能最强大的一款 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本項目是一個開源的彈幕控件庫,能夠支持多種樣式彈幕,彈幕點擊監聽,彈幕分區域顯示,自定義移動速度等功能,項目原理是通過自定義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。

總結

以上是生活随笔為你收集整理的可能是目前轻量级弹幕控件中功能最强大的一款的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。