SurfaceView介绍
1.SurfaceView和View的区别:
View 主要适用于主动更新的情况,而 surfaceView 主要适用于被动更新,例如频繁的刷新。
View 在主线程中对画面进行刷新,而 surfaceView 通常会通过一个子线程来进行页面的刷新
View 在绘图时没有使用双缓冲机制,而 surfaceView 在底层实现机制上就已经实现了双缓冲机制。
总结就是,如果你的自定义 View 需要频繁刷新,或者刷新时数据处理量很大,考虑用 SurfaceView 来替代 View。
二.双缓存机制

三.MainActivty
<LinearLayout 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"
tools:context=".MainActivity">
<VideoView
android:id="@+id/vv"
android:layout_width="match_parent"
android:layout_height="400dp" />
</LinearLayout>
//java文件
package com.example.administrator.myapplication4;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class Main2Activity extends AppCompatActivity {
VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
videoView=findViewById(R.id.vv);
//设置资源
videoView.setVideoPath("http://wvideo.spriteapp.cn/video/2019/0812/5d50ae80664f1_wpd.mp4");
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
videoView.start();
}
});
//控制器:自带进度条和播放暂停按钮
videoView.setMediaController(new MediaController(this));
}
}
//效果图

//MainActivity2代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".SurfaceViewActivity"
android:orientation="vertical">
<SurfaceView
android:id="@+id/surface"
android:layout_width="match_parent"
android:layout_height="300dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_current"
android:text="00:00"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<SeekBar
android:id="@+id/bar"
android:layout_weight="7"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_duration"
android:text="00:00"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/bt_start"
android:text="播放/暂停"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
//java文件
package com.example.administrator.myapplication4;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
public class SurfaceViewActivity extends AppCompatActivity implements SurfaceHolder.Callback, View.OnClickListener {
SurfaceView surfaceView;//控件 一个Surfaceview 对应一个surfaceHolder
SurfaceHolder surfaceHolder;//持有者,不能直接操作SurfaceView,通过他的SurfaceHolder来操作,
MediaPlayer mediaPlayer = new MediaPlayer();//媒体资源对象
private TextView tv_current;
private SeekBar bar;
private TextView tv_duration;
private Button bt_start;
Timer timer;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==101){
int currentPosition = mediaPlayer.getCurrentPosition();//获得当前进度
bar.setProgress(currentPosition);//给金福条设置
int duration = mediaPlayer.getDuration();//总时长
bar.setMax(duration);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_surface_view);
initView();
initPalyer();
initTimer();
}
//定时器:每隔一段时间获取视频当前的进度
private void initTimer() {
timer=new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(101);
}
},0,200);//0毫秒之后执行 每隔200毫秒再执行一次
}
private void initView() {
surfaceView = findViewById(R.id.surface);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);//添加接口回调,能够获取surfaceview的生命周期
tv_current = (TextView) findViewById(R.id.tv_current);
tv_current.setOnClickListener(this);
bar = (SeekBar) findViewById(R.id.bar);
bar.setOnClickListener(this);
tv_duration = (TextView) findViewById(R.id.tv_duration);
tv_duration.setOnClickListener(this);
bt_start = (Button) findViewById(R.id.bt_start);
bt_start.setOnClickListener(this);
bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mediaPlayer.seekTo(seekBar.getProgress());//更新视频的进度
}
});
}
//初始化mediaplayer
private void initPalyer() {
try {
mediaPlayer.setDataSource("http://wvideo.spriteapp.cn/video/2019/0812/5d50ae80664f1_wpd.mp4");
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// mediaPlayer.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
//生命周期:创建 将mediaplayer和surfaceholder绑定一起,
@Override
public void surfaceCreated(SurfaceHolder holder) {
mediaPlayer.setDisplay(surfaceHolder);//将mediapler显示在surfaceview
}
//改变
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
//销毁
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_start:
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
}else{
mediaPlayer.start();
}
break;
}
}
}
//效果图

//MainActivity3
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".GraphisActivity">
<SurfaceView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
//java文件
package com.example.administrator.myapplication4;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class GraphisActivity extends AppCompatActivity implements SurfaceHolder.Callback {
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_graphis);
surfaceView=findViewById(R.id.surface_view);
surfaceHolder=surfaceView.getHolder();
surfaceHolder.addCallback(this);
}
//画画:画笔paint+画布canves
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void surfaceCreated(SurfaceHolder holder) {
//TODO 1:画笔
Paint paint = new Paint();
paint.setColor(Color.GREEN);//颜色
paint.setStrokeWidth(30);//画笔宽度
paint.setTextSize(60);//文字的大小
paint.setAntiAlias(true);//抗锯齿,默认false
//TODO 2:画布
Canvas canvas = holder.lockCanvas();//锁定 我在画的时候,不让其他人的话
//画点
canvas.drawPoint(200,300,paint);
//画线
canvas.drawLine(200,300,400,500,paint);
//画圆
canvas.drawOval(100,200,300,400,paint);
//画矩形
canvas.drawRect(300,400,500,600,paint);
//画扇形
canvas.drawArc(500,500,700,700,20,180,true,paint);
//画文字:歌词滚动
canvas.drawText("我是帅哥承谦益",800,800,paint);
//画图片
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher),800,900,paint);
holder.unlockCanvasAndPost(canvas);//解除锁定
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}

992

被折叠的 条评论
为什么被折叠?



