Android簡單實現(xiàn)彈幕效果
本文實例為大家分享了Android實現(xiàn)彈幕效果的具體代碼,供大家參考,具體內(nèi)容如下
首先分析一下,他是由三層布局來共同完成的,第一層視頻布局,第二層字幕布局,第三層輸入框布局,要想讓這三個布局在同一頁面上,必須用相對布局或幀布局。
<?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)建一個彈幕的解析器
public class MainActivity extends AppCompatActivity {
private boolean showDanmaku;
private DanmakuView danmakuView;
private DanmakuContext danmakuContext;
//創(chuàng)建一個彈幕的解析器
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()方法來提升繪制效率,也就是繪制速度
// 又調(diào)用了setCallback()方法來設(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();
//第一個參數(shù)是彈幕的解析器
//調(diào)用DanmakuView的prepare()方法來進行準備,準備完成后會自動調(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);
}
}
最后使頁面橫屏展示:
<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>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android網(wǎng)絡(luò)監(jiān)聽和網(wǎng)絡(luò)判斷示例介紹
大家好,本篇文章主要講的是Android網(wǎng)絡(luò)監(jiān)聽和網(wǎng)絡(luò)判斷示例介紹,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
Android解決viewpager嵌套滑動沖突并保留側(cè)滑菜單功能
這篇文章主要介紹了 解決viewpager嵌套滑動沖突,并保留側(cè)滑菜單功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-06-06
Android之PreferenceActivity應(yīng)用詳解(2)
看到很多書中都沒有對PreferenceActivity做介紹,而我正好又在項目中用到,所以就把自己的使用的在這總結(jié)一下,也方便日后查找2012-11-11
Android高版本API方法如何在低版本系統(tǒng)上做兼容性處理淺析
這篇文章主要給大家介紹了關(guān)于Android高版本API方法如何在低版本系統(tǒng)上做兼容性處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧2018-08-08
Android開發(fā)實現(xiàn)廣告無限循環(huán)功能示例
這篇文章主要介紹了Android開發(fā)實現(xiàn)廣告無限循環(huán)功能,結(jié)合完整實例形式分析了Android廣告圖片輪播功能的具體實現(xiàn)步驟與相關(guān)功能、布局等操作技巧,需要的朋友可以參考下2017-11-11
Android Compose實現(xiàn)聯(lián)系人列表流程
聲明式UI,更簡單的自定義,實時帶交互的預覽功能Compose并不是類似于Recyclerview的高級控件,而是直接拋棄了View,ViewGroup那套東西,從上到下魯了一套全新的框架,直白點說就是它的渲染機制,布局機制,觸摸算法,以及UI具體寫法全都是新的2023-03-03

