Android簡(jiǎn)單實(shí)現(xiàn)彈幕效果
本文實(shí)例為大家分享了Android實(shí)現(xiàn)彈幕效果的具體代碼,供大家參考,具體內(nèi)容如下
首先分析一下,他是由三層布局來(lái)共同完成的,第一層視頻布局,第二層字幕布局,第三層輸入框布局,要想讓這三個(gè)布局在同一頁(yè)面上,必須用相對(duì)布局或幀布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_main"
tools:context="com.bwie.danmustudy.MainActivity">
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/>
<master.flame.danmaku.ui.widget.DanmakuView
android:id="@+id/danmaku_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<LinearLayout
android:id="@+id/operation_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:visibility="gone"
android:background="#fff"
android:orientation="horizontal"
>
<EditText
android:id="@+id/edit_text"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<Button
android:id="@+id/send"
android:text="send"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
</RelativeLayout>
創(chuàng)建一個(gè)彈幕的解析器
public class MainActivity extends AppCompatActivity {
private boolean showDanmaku;
private DanmakuView danmakuView;
private DanmakuContext danmakuContext;
//創(chuàng)建一個(gè)彈幕的解析器
private BaseDanmakuParser parser=new BaseDanmakuParser() {
@Override
protected IDanmakus parse() {
return new Danmakus();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//播放視頻
VideoView video_view= (VideoView) findViewById(R.id.video_view);
Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.minion_08);
video_view.setVideoURI(uri);
video_view.start();
danmakuView= (DanmakuView) findViewById(R.id.danmaku_view);
//調(diào)用了enableDanmakuDrawingCache()方法來(lái)提升繪制效率,也就是繪制速度
// 又調(diào)用了setCallback()方法來(lái)設(shè)置回調(diào)函數(shù)。
danmakuView.enableDanmakuDrawingCache(true);
danmakuView.setCallback(new DrawHandler.Callback() {
@Override
public void prepared() {
showDanmaku=true;
danmakuView.start();
}
@Override
public void updateTimer(DanmakuTimer timer) {
}
@Override
public void danmakuShown(BaseDanmaku danmaku) {
}
@Override
public void drawingFinished() {
}
});
danmakuContext=danmakuContext.create();
//第一個(gè)參數(shù)是彈幕的解析器
//調(diào)用DanmakuView的prepare()方法來(lái)進(jìn)行準(zhǔn)備,準(zhǔn)備完成后會(huì)自動(dòng)調(diào)用剛才設(shè)置的回調(diào)函數(shù)中的prepared()方法
danmakuView.prepare(parser,danmakuContext);
final LinearLayout operationLayout= (LinearLayout) findViewById(R.id.operation_text);
final Button send= (Button) findViewById(R.id.send);
final EditText edit_text= (EditText) findViewById(R.id.edit_text);
danmakuView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (operationLayout.getVisibility()==View.GONE){
operationLayout.setVisibility(View.VISIBLE);
}else{
operationLayout.setVisibility(View.GONE);
}
}
});
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String content=edit_text.getText().toString();
if (!TextUtils.isEmpty(content)){
addDanmaku(content,true);
edit_text.setText("");
}
}
});
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus&& Build.VERSION.SDK_INT>=19){
View decorView=getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|View.SYSTEM_UI_FLAG_FULLSCREEN
|View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
);
}
}
private void addDanmaku(String content,boolean withBorder){
BaseDanmaku danmaku=danmakuContext.mDanmakuFactory
.createDanmaku(BaseDanmaku.TYPE_SCROLL_RL);
danmaku.text=content;
danmaku.padding=5;
danmaku.textSize=50;
danmaku.setTime(danmakuView.getCurrentTime());
if (withBorder){
danmakuView.addDanmaku(danmaku);
}
}
最后使頁(yè)面橫屏展示:
<activity android:name=".MainActivity"
只需要加這一行代碼就可以
android:screenOrientation="landscape"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Jetpack組件DataBinding詳解
這篇文章主要介紹了Android Jetpack組件DataBinding,DataBinding有很多優(yōu)勢(shì),其中最明顯是代碼更加簡(jiǎn)潔,可讀性會(huì)更高。部分和UI控件有關(guān)的代碼可以在布局文件當(dāng)中完成,本文給大家詳細(xì)講解,需要的朋友可以參考下2022-10-10
android自定義控件ImageView實(shí)現(xiàn)圓形圖片
這篇文章主要為大家詳細(xì)介紹了android自定義控件ImageView實(shí)現(xiàn)圓形圖片,適用于用戶頭像,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android自定義PopupWindow簡(jiǎn)單小例子
這篇文章主要為大家詳細(xì)介紹了Android自定義PopupWindow簡(jiǎn)單小例子,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android入門(mén)計(jì)算器編寫(xiě)代碼
這篇文章主要為大家詳細(xì)介紹了Android入門(mén)計(jì)算器編寫(xiě)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
Android開(kāi)發(fā)Input系統(tǒng)觸摸事件分發(fā)
這篇文章主要為大家介紹了Android開(kāi)發(fā)Input系統(tǒng)觸摸事件分發(fā)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Android使用開(kāi)源框架ANDROID-IMAGE-INDICATOR實(shí)現(xiàn)圖片輪播部署
這篇文章主要為大家詳細(xì)介紹了Android使用開(kāi)源框架ANDROID-IMAGE-INDICATOR實(shí)現(xiàn)圖片輪播部署,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android本地?cái)?shù)據(jù)存儲(chǔ)Room實(shí)踐和優(yōu)化技巧
本文詳細(xì)介紹了Android本地?cái)?shù)據(jù)存儲(chǔ)框架Room的使用,包括基本概念、核心組件、最佳實(shí)踐、優(yōu)化技巧等,幫助開(kāi)發(fā)者學(xué)習(xí)和掌握Room的使用方法,提升數(shù)據(jù)存儲(chǔ)效率和應(yīng)用性能2023-04-04
Android實(shí)現(xiàn)Ant Design 自定義表單組件
Ant Design 組件提供了Input,InputNumber,Radio,Select,uplod等表單組件,下面通過(guò)本文給大家詳細(xì)介紹Android實(shí)現(xiàn)Ant Design 自定義表單組件,需要的的朋友參考下吧2017-06-06

