Android雙重SurfaceView實(shí)現(xiàn)彈幕效果
本文實(shí)例為大家分享了Android雙重SurfaceView實(shí)現(xiàn)彈幕效果的具體代碼,供大家參考,具體內(nèi)容如下
頁面布局
首先是XML的layout布局,這里的總的父布局是一個FrameLayout用于貼上兩個SurfaceView,一個用來播放視頻,一個用來顯示彈幕
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:orientation="vertical"
tools:context=".DanmuActivity">
<SurfaceView
android:id="@+id/sv_text"
android:layout_width="match_parent"
android:layout_height="400dp"/>
<SurfaceView
android:id="@+id/sv_media"
android:layout_width="match_parent"
android:layout_height="400dp"/>
/>
<EditText
android:id="@+id/et_text"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="450dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="發(fā)送"
android:layout_marginTop="500dp"
android:onClick="Gogo"/>
</FrameLayout>
對象類
創(chuàng)建一個對象類來存放你所發(fā)送的彈幕
public class Danmu {
String text;//彈幕內(nèi)容
int x;//x軸
int y;//y軸
public Danmu(String text){
this.text = text;
//將y設(shè)置為隨機(jī),彈幕出現(xiàn)的位置也為隨機(jī)
this.y = (int) (Math.random()*400);
this.x = 0;
}
}
Activity實(shí)現(xiàn)SurfaceHolder.Callback并重寫其方法
先定義需要的東西,播放視頻我們用Mediaplayer
//視頻播放 private MediaPlayer mediaPlayer; //彈幕Surface與視頻Surface private SurfaceView sv_text, sv_media; //兩個Surface對應(yīng)的兩個holder private SurfaceHolder text_holder, media_holder; EditText editText;//字幕輸入框 List<Danmu> list = new ArrayList<>();//存放
初始化MediaPlayer,要在第一步執(zhí)行否則運(yùn)行會報空,這里封裝成了一個方法,直接在onCreate內(nèi)調(diào)用
private void initPlayer() throws IOException {
//先判斷是否創(chuàng)建過,沒創(chuàng)建就創(chuàng)建出來
if (mediaPlayer == null) {
mediaPlayer = new MediaPlayer();
}
mediaPlayer.reset();//使其恢復(fù)空閑狀態(tài)
//播放的資源
mediaPlayer.setDataSource("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
mediaPlayer.prepare();//準(zhǔn)備
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {//準(zhǔn)備完畢了
mediaPlayer.start();//播放
}
});
}
初始化控件,同樣封裝為方法,holder用對應(yīng)的Surface獲取到
private void initView() {
sv_text = findViewById(R.id.sv_text);
text_holder = sv_text.getHolder();
text_holder.addCallback(this);
sv_media = findViewById(R.id.sv_media);
media_holder = sv_media.getHolder();
media_holder.addCallback(this);
editText = findViewById(R.id.et_text);
//設(shè)置透明,將播放彈幕的Surface放到第一位并設(shè)置為背景透明
sv_text.setZOrderOnTop(true);
text_holder.setFormat(PixelFormat.TRANSPARENT);
}
接下來是Surface.Callback重寫的方法
@Override
public void surfaceCreated(SurfaceHolder holder) {
//判斷當(dāng)前holder是否是media的那個
if (holder == media_holder) {
//設(shè)置要顯示的Surfaceholder
mediaPlayer.setDisplay(media_holder);
//判斷當(dāng)前holder是否是字幕的那個
} else if (holder == text_holder) {
//創(chuàng)建線程執(zhí)行耗時操作
new Thread() {
@Override
public void run() {
super.run();
//死循環(huán)用來一直更新彈幕的位置
while (true) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Paint paint = new Paint();//創(chuàng)建畫筆
paint.setStrokeWidth(5);//畫筆粗細(xì)
paint.setColor(Color.GREEN);//畫筆顏色
paint.setTextSize(30);//設(shè)置文字大小
//創(chuàng)建畫板
Canvas canvas = text_holder.lockCanvas();
//判斷若畫板為空則跳出循環(huán)
if (canvas == null) {
break;
}
//設(shè)置畫布顏色,透明
canvas.drawColor(PixelFormat.TRANSPARENT, PorterDuff.Mode.CLEAR);
//用循環(huán)來你的彈幕集合并且在畫板上展示出來
//x+=20為你的彈幕在不斷的從左到右移動
for (Danmu danmu : list) {
canvas.drawText(danmu.text, danmu.x += 20, danmu.y, paint);
//若移動的位置大于視頻Surface的寬度了就歸0
if (danmu.x > sv_media.getWidth()) {
danmu.x = 0;
}
}
//將畫布解鎖并顯示到屏幕上
text_holder.unlockCanvasAndPost(canvas);
}
}
}.start();//不要忘記開啟線程
}
}
發(fā)送的按鈕的點(diǎn)擊事件
public void Gogo(View view) {
//先判斷輸入框里有沒有東西
if (!editText.getText().toString().isEmpty() && !editText.getText().toString().equals("")) {
Danmu danmu = new Danmu(editText.getText().toString());
list.add(danmu);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:Android實(shí)現(xiàn)帶進(jìn)度條的WebView
欄 目:Android
本文標(biāo)題:Android雙重SurfaceView實(shí)現(xiàn)彈幕效果
本文地址:http://www.jygsgssxh.com/a1/Android/9033.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動登錄
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10android實(shí)現(xiàn)指紋識別功能
- 01-10Emoji表情在Android JNI中的兼容性問題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10android開發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解
- 01-10android異步消息機(jī)制 源碼層面徹底解析(1)


閱讀排行
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問題詳
隨機(jī)閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)


