SurfaceView的简单使用

主页面布局


SurfaceView的简单使用_第1张图片

主页面代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

protected Button mBufferBtn;

protected Button mVideoBtn;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

super.setContentView(R.layout.activity_main);

initView();

}

@Override

public void onClick(View view) {

Intent intent = new Intent();

if (view.getId() == R.id.button) {

intent.setClass(this, BufferActivity.class);

} else if (view.getId() == R.id.button) {

intent.setClass(this, VideoActivity.class);

}

startActivity(intent);

}

private void initView() {

mBufferBtn = (Button) findViewById(R.id.button2);

mBufferBtn.setOnClickListener(MainActivity.this);

mVideoBtn = (Button) findViewById(R.id.button2);

mVideoBtn.setOnClickListener(MainActivity.this);

}

}

SurfacrView渲染动画页面

public class BufferActivity extends AppCompatActivity implements SurfaceHolder.Callback {

protected SurfaceView mSurfaceView;

private SurfaceHolder mHolder;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

super.setContentView(R.layout.activity_buffer);

initView();

initSurfaceHolder();

}

// 初始化Surface的管理者

private void initSurfaceHolder() {

mHolder = mSurfaceView.getHolder();

// 添加管理生命周期的接口回调

mHolder.addCallback(this);

}

private void initView() {

mSurfaceView = (SurfaceView) findViewById(R.id.surface_view);

}

// 缓冲区创建

@Override

public void surfaceCreated(SurfaceHolder holder) {

Log.d("1507", "surfaceCreated");

new DrawThread().start();

}

// 缓冲区内容改变(子线程渲染UI的过程)

@Override

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

Log.d("1507", "surfaceChanged");

}

// 缓冲区销毁

@Override

public void surfaceDestroyed(SurfaceHolder holder) {

Log.d("1507", "surfaceDestroyed");

}

// 绘制UI的子线程

private class DrawThread extends Thread {

@Override

public void run() {

super.run();

// 创建画笔

Paint paint = new Paint();

paint.setColor(Color.GREEN);// 画笔颜色

paint.setStrokeWidth(10);// 画笔粗细。注意:Java中设置的尺寸单位都是px

paint.setStyle(Paint.Style.FILL_AND_STROKE);// 设置实心

paint.setAntiAlias(true);// 设置是否抗锯齿

// 获取SurfaceView的盖度

int height = mSurfaceView.getHeight();

Canvas canvas = null;

for (int i = 0; i < height; i+= 5) {

// 获取Surface中的画布

canvas = mHolder.lockCanvas();// 锁定画布

// 使用画笔在画布上绘制指定形状

canvas.drawCircle(100, i + 50, 50, paint);// 圆心x坐标,圆心y坐标,半径,画笔

// 缓冲区的画布绘制完毕,需要解锁并提交给窗口展示

mHolder.unlockCanvasAndPost(canvas);

//                try {

//                    Thread.sleep(100);

//                } catch (InterruptedException e) {

//                    e.printStackTrace();

//                }

}

}

}

}

渲染动画布局


SurfaceView的简单使用_第2张图片

SurfaceView播放视频布局


SurfaceView的简单使用_第3张图片

SurfaceView视频播放实现

public class VideoActivity extends AppCompatActivity implements View.OnClickListener {

protected MyVideoSurfaceView mSurfaceView;

protected Button mPlayBtn;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

super.setContentView(R.layout.activity_video);

initView();

}

// 运行、可见

@Override

protected void onStart() {

super.onStart();

}

// 可交互

@Override

protected void onResume() {

super.onResume();

}

private void play() {

String videoPath = Environment.getExternalStorageDirectory().getPath() +

"/VID_20171117_144736.3gp";// 外部存储根路径

mSurfaceView.playVideo(videoPath);

}

private void initView() {

mSurfaceView = (MyVideoSurfaceView) findViewById(R.id.surface_view);

mPlayBtn = (Button) findViewById(R.id.play_btn);

mPlayBtn.setOnClickListener(VideoActivity.this);

}

@Override

public void onClick(View view) {

if (view.getId() == R.id.play_btn) {

play();

}

}

}

SurefaceView视频播放工具类

public class MyVideoSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

private SurfaceHolder mHolder;

private MediaPlayer mMediaPlayer;

public MyVideoSurfaceView(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}

private void init() {

// 获取Surface换朝哪个区的持有者

mHolder = getHolder();

mHolder.addCallback(this);

}

// 设置播放源

public void playVideo(String path) {

if (mMediaPlayer == null) {

mMediaPlayer = new MediaPlayer();

}

try {

// 设置播放源

mMediaPlayer.setDataSource(path);

// 设置多媒体的显示部分:使用SurfaceHolder渲染画面

mMediaPlayer.setDisplay(mHolder);

mMediaPlayer.prepare();

mMediaPlayer.start();

} catch (IOException e) {

e.printStackTrace();

}

}

@Override

public void surfaceCreated(SurfaceHolder holder) {

}

@Override

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override

public void surfaceDestroyed(SurfaceHolder holder) {

mMediaPlayer.release();

mMediaPlayer = null;

}

}

你可能感兴趣的:(SurfaceView的简单使用)