本文实例为大家分享了SurfaceVIEw视频d幕展示的具体代码,供大家参考,具体内容如下
全部代码如下:
package com.example.app2;import androID.graphics.Canvas;import androID.graphics.color;import androID.graphics.Paint;import androID.graphics.PixelFormat;import androID.graphics.PorterDuff;import androID.media.MediaPlayer;import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;import androID.text.TextUtils;import androID.vIEw.SurfaceHolder;import androID.vIEw.SurfaceVIEw;import androID.vIEw.VIEw;import androID.Widget.EditText;import androID.Widget.Toast;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback { private SurfaceVIEw mSvVIDeo; private SurfaceVIEw mSvDanMu; private EditText mEd; private MediaPlayer mediaPlayer; private SurfaceHolder svVIDeoHolder,svDanMuHolder; private boolean isPlay = true; List<DanMuBean> List = new ArrayList<>(); @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); // 创建MediaPlayer initPlayer(); // 初始化视图 initVIEw(); } private voID initPlayer() { if (mediaPlayer == null) { mediaPlayer = new MediaPlayer(); } // 重置 mediaPlayer.reset(); try { mediaPlayer.setDataSource("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"); mediaPlayer.prepareAsync(); //异步准备 mediaPlayer.setonPreparedListener(new MediaPlayer.OnPreparedListener() { @OverrIDe public voID onPrepared(MediaPlayer mp) { mediaPlayer.setLooPing(true); //是否开启循环播放 mediaPlayer.start(); //开始播放 } }); } catch (IOException e) { e.printstacktrace(); } } /** * 发送的信息 * * @param vIEw */ public voID send(VIEw vIEw) { submit(); } private voID submit() { // valIDate 非空判断 String edString = mEd.getText().toString().trim(); if (TextUtils.isEmpty(edString)) { Toast.makeText(this,"edString不能为空",Toast.LENGTH_SHORT).show(); return; } List.add(new DanMuBean(edString)); //添加数据 mEd.setText(""); //清空 } private voID initVIEw() { mSvVIDeo = (SurfaceVIEw) findVIEwByID(R.ID.sv_vIDeo); mSvDanMu = (SurfaceVIEw) findVIEwByID(R.ID.sv_danMu); mEd = (EditText) findVIEwByID(R.ID.ed); // 初始化 svDanMuHolder svVIDeoHolder svVIDeoHolder = mSvVIDeo.getHolder(); svDanMuHolder = mSvDanMu.getHolder(); // 添加监听 svVIDeoHolder.addCallback(this); svDanMuHolder.addCallback(this); // 将d幕显示在最上层,并设置为透明 mSvDanMu.setZOrderOntop(true); svDanMuHolder.setFormat(PixelFormat.transparent); //PixelFormat: 像素格式,transparent(2):透明的; TRANSLUCENT(-3):半透明 } /** * surfaceCreated:创建 * * @param holder */ @OverrIDe public voID surfaceCreated(SurfaceHolder holder) { if (holder == svVIDeoHolder) { mediaPlayer.setdisplay(svVIDeoHolder); //将内容显示在 svVIDeoHolder上 } else if (holder == svDanMuHolder) { // d幕设置 开启线程 new Thread(new Runnable() { @OverrIDe public voID run() { while (isPlay) { //死循环 // 得到画笔,设置属性 Paint paint = new Paint(); paint.setstrokeWIDth(5); //设置笔画宽度 paint.setTextSize(30); //设置字体大小 paint.setcolor(color.GREEN); // 设置颜色 // 得到画布 通过lockCanvas Canvas canvas = svDanMuHolder.lockCanvas(); if (canvas == null) { break; } // 填充画布的颜色 canvas.drawcolor(PixelFormat.transparent,PorterDuff.Mode.CLEAR); //参数1: 设为透明,参2: PorterDuff.Mode.CLEAR: 所绘制不会提交到画布上 // 设置d幕内容 for (int i = 0; i < List.size(); i++) { String text = List.get(i).text; canvas.drawText(text,List.get(i).x += 1,List.get(i).y,paint); if (List.get(i).x > mSvVIDeo.getWIDth()) { List.get(i).x = 0; } } // 提交 svDanMuHolder.unlockCanvasAndPost(canvas); } } }).start(); } } @OverrIDe public voID surfaceChanged(SurfaceHolder holder,int format,int wIDth,int height) { } @OverrIDe public voID surfaceDestroyed(SurfaceHolder holder) { isPlay = false; }}
布局xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" tools:context=".MainActivity"> <FrameLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <SurfaceVIEw androID:ID="@+ID/sv_vIDeo" androID:layout_wIDth="match_parent" androID:layout_height="280dp" /> <SurfaceVIEw androID:ID="@+ID/sv_danMu" androID:layout_wIDth="match_parent" androID:layout_height="280dp" /> </FrameLayout> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <EditText androID:ID="@+ID/ed" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="1" /> <button androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:onClick="send" androID:text="d幕" /> </linearLayout></linearLayout>
效果界面:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
总结以上是内存溢出为你收集整理的使用SurfaceView实现视频d幕全部内容,希望文章能够帮你解决使用SurfaceView实现视频d幕所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)