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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

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

本項(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。