surfaceview和surfaceholde源码分析及其运用

本文深入探讨了SurfaceView和SurfaceHolder在Android开发中的作用和源码分析。SurfaceHolder作为Surface的控制器,提供锁定画布和更新显示内容的功能。SurfaceView是View的子类,内嵌Surface并提供独立的渲染线程,常用于游戏开发以提高程序响应速度。文章还介绍了SurfaceHolder.Callback接口,用于监听Surface的变化,并强调了在UI线程和渲染线程中操作SurfaceView的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、SurfaceHolder
public interface SurfaceHolder

显示一个surface的抽象接口,使你可以控制surface的大小和格式, 以及在surface上编辑像素,和监视surace的改变。

这个接口通常通过SurfaceView类实现。

源码

	public interface SurfaceHolder {
		//.........
	    public interface Callback {
	        /**
	         * This is called immediately after the surface is first created.
	         * Implementations of this should start up whatever rendering code
	         * they desire.  Note that only one thread can ever draw into
	         * a {@link Surface}, so you should not draw into the Surface here
	         * if your normal rendering will be in another thread.
	         * 
	         * @param holder The SurfaceHolder whose surface is being created.
	         */
	        public void surfaceCreated(SurfaceHolder holder);

	        /**
	         * This is called immediately after any structural changes (format or
	         * size) have been made to the surface.  You should at this point update
	         * the imagery in the surface.  This method is always called at least
	         * once, after {@link #surfaceCreated}.
	         * 
	         * @param holder The SurfaceHolder whose surface has changed.
	         * @param format The new PixelFormat of the surface.
	         * @param width The new width of the surface.
	         * @param height The new height of the surface.
	         */
	        public void surfaceChanged(SurfaceHolder holder, int format, int width,
	                int height);

	        /**
	         * This is called immediately before a surface is being destroyed. After
	         * returning from this call, you should no longer try to access this
	         * surface.  If you have a rendering thread that directly accesses
	         * the surface, you must ensure that thread is no longer touching the 
	         * Surface before returning from this function.
	         * 
	         * @param holder The SurfaceHolder whose surface is being destroyed.
	         */
	        public void surfaceDestroyed(SurfaceHolder holder);
	    }
	    //.....................
	}

surface的控制器,用来操纵surface,处理它的Canvas上画的效果和动画,控制表面,大小,像素等。
几个需要注意的方法:
(1)abstract void addCallback(SurfaceHolder.Callback callback);
// SurfaceView当前的持有者一个回调对象。
(2)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值