SurfaceView连续渲染SurfaceHolder Canvas
Canvas canvas = holder.lockCanvas(); canvas.drawRGB(255, 0, 0); holder.unlockCanvasAndPost(canvas);
第一行,锁定Surface用于渲染并返回一个可用的Canvas
创新互联建站长期为成百上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为韶山企业提供专业的网站设计、成都网站建设,韶山网站改版等技术服务。拥有10年丰富建站经验和众多成功案例,为您定制开发。
第二行,解锁Surface并确保通过Canvas进行绘制的内容可显示到屏幕上
例子:
import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.graphics.Canvas; import android.view.Menu; import android.view.SurfaceHolder; import android.view.SurfaceView; public class MainActivity extends Activity { FastRenderView renderView; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); renderView = new FastRenderView(this); setContentView(renderView); } protected void onResume() {//重载 super.onResume(); renderView.resume(); } class FastRenderView extends SurfaceView implements Runnable { Thread renderThread = null; SurfaceHolder holder = null; volatile boolean running = false; public FastRenderView(Context context) { super(context); holder = getHolder(); } public void resume()//自定义 { running = true; renderThread.start(); } public void run() { while(running) { if(!holder.getSurface().isValid()) continue; Canvas canvas = holder.lockCanvas(); canvas.drawRGB(255, 0, 0); holder.unlockCanvasAndPost(canvas); } } public void pause()//自定义 { running = false; while(true) { try { renderThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
本文标题:SurfaceView连续渲染SurfaceHolder Canvas
本文路径:http://ybzwz.com/article/iesdso.html