dependencies {
compile 'com.github.ctiao:DanmakuFlameMaster:0.5.3'
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <VideoView android:id="@+id/vv_video" android:layout_width="match_parent" android:layout_height="match_parent" /> <master.flame.danmaku.ui.widget.DanmakuView android:id="@+id/sv_danmaku" android:layout_width="match_parent" android:layout_height="match_parent" /> <include android:id="@+id/media_controller" android:layout_width="match_parent" android:layout_height="fill_parent" layout="@layout/media_controller" /> </FrameLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:gravity="center_vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="#8acc22dd" > <Button android:layout_weight="1" android:id="@+id/rotate" android:layout_width="0dp" android:layout_height="wrap_content" android:text="@string/rotate" /> <Button android:layout_width="0dp" android:layout_weight="1" android:id="@+id/btn_hide" android:layout_height="wrap_content" android:text="@string/hide_danmaku" /> <Button android:id="@+id/btn_show" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="@string/show_danmaku" /> <Button android:id="@+id/btn_pause" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="@string/pause_danmaku" /> <Button android:id="@+id/btn_resume" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="@string/resume_danmaku" /> <Button android:id="@+id/btn_send" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="@string/send_danmaku" /> <Button android:id="@+id/btn_send_image_text" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="@string/send_danmaku_image_text" /> <Button android:id="@+id/btn_send_danmakus" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:text="@string/send_danmakus" /> </LinearLayout> </FrameLayout>
//设置最大行数 HashMap<Integer,Integer> maxLinesPair = new HashMap<>(); maxLinesPair.put(BaseDanmaku.TYPE_SCROLL_RL,5);//滚动弹幕最大显示5行 //设置是否禁止重叠 HashMap<Integer, Boolean> overlappingEnablePair = new HashMap<>(); overlappingEnablePair.put(BaseDanmaku.TYPE_SCROLL_RL, true); overlappingEnablePair.put(BaseDanmaku.TYPE_FIX_TOP, true); mDanmakuContext = DanmakuContext.create();//初始化上下文 mDanmakuContext.setDanmakuStyle(IDisplayer.DANMAKU_STYLE_STROKEN,3);//设置弹幕类型 mDanmakuContext.setDuplicateMergingEnabled(false);//设置是否合并重复弹幕 mDanmakuContext.setScrollSpeedFactor(1.2f);//设置弹幕滚动速度 mDanmakuContext.setScaleTextSize(1.2f);//设置弹幕字体大小 mDanmakuContext.setCacheStuffer(new SpannedCacheStuffer(),mCacheStufferAdapter);//设置缓存绘制填充器 图文混排使用SpannedCacheStuffer mDanmakuContext.setMaximumLines(maxLinesPair);//设置最大行数 mDanmakuContext.preventOverlapping(overlappingEnablePair); //设置是否禁止重叠 mParser = createParser(this.getResources().openRawResource(R.raw.comments));//加载弹幕资源文件 mDvDanmaku.prepare(mParser, mDanmakuContext); mDvDanmaku.showFPS(true); mDvDanmaku.enableDanmakuDrawingCache(true);
private SpannableStringBuilder createSpannable(Drawable drawable) {
String text = "bitmap";
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text);
ImageSpan span = new ImageSpan(drawable);
spannableStringBuilder.setSpan(span, 0, text.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
spannableStringBuilder.append("图文混排");
spannableStringBuilder.setSpan(new BackgroundColorSpan(Color.parseColor("#8A2233B1")), 0, spannableStringBuilder.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
return spannableStringBuilder;
}
<i> <chatserver>chat.bilibili.com</chatserver> <chatid>2962351</chatid> <mission>0</mission> <maxlimit>1500</maxlimit> <source>k-v</source> <d p="145.91299438477,1,25,16777215,1422201001,0,D6673695,757075520">我从未见过如此厚颜无耻之人</d> </i>
/**
* 从弹幕文件中提起弹幕
* @param stream
* @return
*/
private BaseDanmakuParser createParser(InputStream stream) {
if (stream == null) {
return new BaseDanmakuParser() {
@Override
protected Danmakus parse() {
return new Danmakus();
}
};
}
ILoader loader = DanmakuLoaderFactory.create(DanmakuLoaderFactory.TAG_BILI);//创建一个BiliDanmakuLoader实例来加载弹幕流文件
try {
loader.load(stream);
} catch (IllegalDataException e) {
e.printStackTrace();
}
BaseDanmakuParser parser = new BiliDanmukuParser();//弹幕解析者
IDataSource<?> dataSource = loader.getDataSource();
parser.load(dataSource);
return parser;
}
String tagName = localName.length() != 0 ? localName : qName;
tagName = tagName.toLowerCase(Locale.getDefault()).trim();
if (tagName.equals("d")) {
String pValue = attributes.getValue("p");
// parse p value to danmaku
String[] values = pValue.split(",");
if (values.length > 0) {
long time = (long) (Float.parseFloat(values[0]) * 1000); // 出现时间
int type = Integer.parseInt(values[1]); // 弹幕类型
float textSize = Float.parseFloat(values[2]); // 字体大小
int color = Integer.parseInt(values[3]) | 0xFF000000; // 颜色
item = mContext.mDanmakuFactory.createDanmaku(type, mContext);
if (item != null) {
item.setTime(time);
item.textSize = textSize * (mDispDensity - 0.6f);
item.textColor = color;
item.textShadowColor = color <= Color.BLACK ? Color.WHITE : Color.BLACK;
}
}
}
mDvDanmaku.prepare(mParser, mDanmakuContext);//传入解析完成的弹幕和上下文
private void prepare() {
if (handler == null)
handler = new DrawHandler(getLooper(mDrawingThreadType), this, mDanmakuVisible);//创建一个Handler
}
public interface Callback {
public void prepared();
public void updateTimer(DanmakuTimer timer);
public void danmakuShown(BaseDanmaku danmaku);
public void drawingFinished();
}
mTimeBase = SystemClock.uptimeMillis();
if (mParser == null || !mDanmakuView.isViewReady()) {//没有准备好,延时0.1秒后再执行
sendEmptyMessageDelayed(PREPARE, 100);
} else {
prepare(new Runnable() {
@Override
public void run() {
pausedPosition = 0;
mReady = true;
if (mCallback != null) {
mCallback.prepared();
}
}
});
}
private void addDanmaku(boolean islive) {
BaseDanmaku danmaku = mDanmakuContext.mDanmakuFactory.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);//添加一条从右到左的滚动弹幕
if (danmaku == null || mDvDanmaku == null) {
return;
}
danmaku.text = "这是一条弹幕" + System.nanoTime();
danmaku.padding = 5;
danmaku.priority = 0; // 可能会被各种过滤器过滤并隐藏显示 设置为1的话,就一定会显示 适用于本机发送的弹幕
danmaku.isLive = islive;
danmaku.setTime(mDvDanmaku.getCurrentTime() + 1200);
danmaku.textSize = 25f * (mParser.getDisplayer().getDensity() - 0.6f);
danmaku.textColor = Color.RED;//默认设置为红色字体
danmaku.textShadowColor = Color.WHITE;
danmaku.borderColor = Color.GREEN;//为了区别其他弹幕与自己发的弹幕,再给自己发的弹幕加上边框
mDvDanmaku.addDanmaku(danmaku);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有