对SurfaceView的一点认识

1. 定义

在Android系统中,有一种特殊的视图,称为SurfaceView,它拥有独立的绘图表面,即它不与其宿主窗口共享同一个绘图表面。由于拥有独立的绘图表面,因此SurfaceView的UI可以在一个独立的线程中进行绘制,且不会占用主线程资源。SurfaceView一方面可以实现复杂而高效的UI,另一方面又不会导致用户输入无法得到响应。

2. view、surfaceview和GLsurfaceview的区别

  • View:显示视图,内置画布,提供图形绘制函数、触屏事件、按键事件函数等;必须在UI主线程内更新画面,速度较慢。
  • SurfaceView : 基于view视图进行拓展的视图类,更适合2D游戏的开发;是view的子类,类似使用双缓机制,在新的线程中更新画面所以刷新界面速度比view快。
  • GLSurfaceView:基于SurfaceView视图再次进行拓展的视图类,专用于3D游戏开发的视图;是SurfaceView的子类,openGL专用。

3. Surface的使用

  • Surface与MediaPlayer实现视频播放
public class SurfaceViewActivity extends AppCompatActivity implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener {
    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;
    private MediaPlayer mediaPlayer;
    private static final String VIDEO_PATH = "https://inducesmile.com/wp-content/uploads/2016/05/small.mp4";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_surface_view);
        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        //获取到SurfaceHolder对象
        surfaceHolder = surfaceView.getHolder();
        //设置其生命周期监听
        surfaceHolder.addCallback(this);
    }

    //创建SurfaceView时被调用
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setDisplay(surfaceHolder); //将SurfaceView与MediaPlayer进行绑定
        try {
            mediaPlayer.setDataSource(VIDEO_PATH);
            mediaPlayer.prepare();
            mediaPlayer.setOnPreparedListener(this);
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    //SurfaceView大小尺寸改变时调用,比如横竖屏切换
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    //SurfaceView被销毁时被调用
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        releaseMediaPlayer();
    }

    //音频播放准备好后,开始播放
    @Override
    public void onPrepared(MediaPlayer mp) {
        mediaPlayer.start();
    }

    @Override
    protected void onPause() {
        super.onPause();
        releaseMediaPlayer();
    }

    @Override
    protected void onStop() {
        super.onStop();
        releaseMediaPlayer();
    }

    //释放MediaPlayer对象
    private void releaseMediaPlayer(){
        if (mediaPlayer != null){
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
}

首先根据SurfaceView获取到其SurfaceHolder对象,然后添加SurfaceHolder的监听事件,在SurfaceView被创建时,调用mediaPlayer.setDisplay(surfaceHolder);将SurfaceView和MediaPlayer进行绑定。

  • SurfaceView实现动画效果
public class SinSurfaceView extends SurfaceView implements Runnable{
    private SurfaceHolder mSurfaceHolder;
    private Paint mPaint; //画笔对象
    private Path mPath; //路径

    public SinSurfaceView(Context context) {
        super(context);
        init();
    }

    public SinSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SinSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    //初始化
    private void init(){
        //设置画笔
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(5);
        mPaint.setColor(Color.GREEN);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        //设置路径
        mPath = new Path();
        //获取到SurfaceHolder对象
        mSurfaceHolder = getHolder();
        //设置其监听事件
        mSurfaceHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                //开启线程,进行绘制
                new Thread(SinSurfaceView.this).start();
            }

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

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                isDrawing = false;  //设置为false,表示停止绘制
                mSurfaceHolder.removeCallback(this); //将callback移除 
            }
        });
    }
    //是否进行绘制标识,默认为false
    private volatile boolean isDrawing = true;
    private int drawX, drawY;

    @Override
    public void run() {
        //当允许绘制时,则不断进行绘制
        while (isDrawing){
            drawX ++;
            drawY = (int)(100*Math.sin(drawX*2*Math.PI / 180) + 400);
            mPath.lineTo(drawX, drawY);
            draw();
        }
    }

    //将path对象绘制到canvas中
    private void draw(){
        Canvas canvas = null;
        try {
            //获取到canvas对象
            canvas = mSurfaceHolder.lockCanvas();
            if (canvas != null){
                canvas.drawColor(Color.WHITE);
                canvas.drawPath(mPath, mPaint);
            }
        } finally {
            if (canvas != null){
                //释放canvas对象
                mSurfaceHolder.unlockCanvasAndPost(canvas);
            }
        }

    }
}

3. SurfaceView原理

你可能感兴趣的:(对SurfaceView的一点认识)