SurfaceView的生命周期,和 Activity生命周期,Service生命周期,BroadcastReceiver生命周期,等,不一样;

因为SurfaceView显示的是(视频画面,游戏画面,相机画面),SurfaceView显示的是动态的画面,而且对性能要求很高,所以SurfaceView的生命周期是这样:

SurfaceView的生命周期:

            当Activity完全显示之后,SurfaceView才会被创建

            只要Activity不是在前台,SurfaceView就会销毁

想要操作SurfaceView就要操作SurfaceHolder,所以并不是直接操作SurfaceView,而是要操作SurfaceView提供的对象:SurfaceHolder

Android-SurfaceView生命周期-LMLPHP


在activity_surface_view.xml布局文件中定义SurfaceView

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <!--
定义SurfaceView
-->
<SurfaceView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </RelativeLayout>

观察SurfaceView的生命周期Java代码:

package liudeli.my_media1;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView; public class MySurfaceViewActivity extends Activity { private final String TAG = MySurfaceViewActivity.class.getSimpleName(); /**
* 操作的是SurfaceHolder,所以定义全局变量
*/
private SurfaceHolder surfaceHolder; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_surface_view); // 获取在布局文件中定义的SurfaceView
SurfaceView surfaceView = findViewById(R.id.surface_view); // 不能直接操作SurfaceView,需要通过SurfaceView拿到SurfaceHolder
surfaceHolder = surfaceView.getHolder(); // 使用SurfaceHolder设置屏幕高亮,注意:所有的View都可以设置 设置屏幕高亮
surfaceHolder.setKeepScreenOn(true); // 使用SurfaceHolder设置把画面或缓存 直接显示出来
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); /**
* 由于要观察SurfaceView生命周期,所以需要设置监听
* 此监听不一样,是addCallback
*
*/
surfaceHolder.addCallback(callback);
} @Override
protected void onStart() {
super.onStart();
Log.d(TAG, ">>>> onStart()");
} @Override
protected void onResume() {
super.onResume();
Log.d(TAG, ">>>> onResume()");
} @Override
protected void onPause() {
super.onPause();
Log.d(TAG, ">>>> onPause()");
} @Override
protected void onStop() {
super.onStop();
Log.d(TAG, ">>>> onStop()");
} /**
* 定义SurfaceView监听回调
*/
private SurfaceHolder.Callback callback = new SurfaceHolder.Callback() { /**
* surfaceHolder被创建了
* @param holder
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.d(TAG, "----------- surfaceHolder被创建了------------");
} /**
* surfaceHolder发生改变了
* @param holder
* @param format
* @param width
* @param height
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.d(TAG, "----------- surfaceHolder发生改变了------------");
} /**
* surfaceHolder被销毁了
* @param holder
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d(TAG, "----------- surfaceHolder被销毁了------------");
}
}; /**
* 当Activity被销毁的时候,一定要移除监听
*/
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, ">>>> onDestroy()");
if (null != callback) surfaceHolder.removeCallback(callback);
}
}

观察SurfaceView的生命周期

Android-SurfaceView生命周期-LMLPHP

SurfaceView的生命周期Log日志:

  Activity必须完全被显示之后,也就是调用了onResume()方法之后才会被创建

      12-20 22:19:26.090 9868-9868/liudeli.my_media1 D/MySurfaceViewActivity: >>>> onStart()
      12-20 22:19:26.090 9868-9868/liudeli.my_media1 D/MySurfaceViewActivity: >>>> onResume()

      12-20 22:19:26.453 9868-9868/liudeli.my_media1 D/MySurfaceViewActivity: ----------- surfaceHolder被创建了------------

      12-20 22:19:26.453 9868-9868/liudeli.my_media1 D/MySurfaceViewActivity: ----------- surfaceHolder发生改变了------------

  

  只要Activity不在前台,也就是调用了onPause()方法之后才会被销毁

      12-20 22:19:42.593 9868-9868/liudeli.my_media1 D/MySurfaceViewActivity: >>>> onPause()

      12-20 22:19:42.701 9868-9868/liudeli.my_media1 D/MySurfaceViewActivity: ----------- surfaceHolder被销毁了------------

05-11 16:11