可能是目前轻量级弹幕控件中功能最强大的一款
XDanmuku
Ò»ÖÖÖ§³Ö¶àÖÖµ¯Ä»ÑùʽµÄµ¯Ä»ÊÓͼ¿Ø¼þ
±¾ÏîÄ¿ÊÇÒ»¸ö¿ªÔ´µÄµ¯Ä»¿Ø¼þ¿â£¬Äܹ»Ö§³Ö¶àÖÖÑùʽµ¯Ä»£¬µ¯Ä»µã»÷¼àÌý£¬µ¯Ä»·ÖÇøÓòÏÔʾ£¬×Ô¶¨ÒåÒƶ¯Ëٶȵȹ¦ÄÜ£¬ÏîÄ¿ÔÀíÊÇͨ¹ý×Ô¶¨ÒåViewGroup¡£¿ÉÄÜÊÇÄ¿Ç°ÇáÁ¿¼¶µ¯Ä»¿âÖй¦ÄÜ×îÇ¿´óµÄÒ»¿îÁË¡£
Ч¹û
- ³£¹æÑùʽ
- µã»÷ʼþ
- ¶àÖÖµ¯Ä»Ñùʽ
- ·ÖÇøÓòÏÔʾ
- 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.DanmuContainerView
android: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>() {
@Override
public 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());
}
@Override
public 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>() {
@Override
public 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¡£