标签:
@author : qingdujun
SurfaceView的使用挺简单的,只要实现继承SurfaceView并实现Callback接口即可;
package com.qdj.surfaceviewone; import com.qdj.ui.SurfaceViewOne; import android.support.v7.app.ActionBarActivity; import android.view.Window; import android.view.WindowManager; import android.os.Bundle; public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /*set window*/ getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); /*define view by yourself*/ setContentView(new SurfaceViewOne(this)); } }
自定义SurfaceView
package com.qdj.ui; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; public class SurfaceViewOne extends SurfaceView implements Callback, Runnable { private SurfaceHolder sfHolder; private Thread thread; private Canvas canvas; private Paint paint; int screenW, screenH; int clock; public SurfaceViewOne(Context context) { super(context); clock = 1; thread = new Thread(this); sfHolder = getHolder(); sfHolder.addCallback(this); paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.BLUE); setKeepScreenOn(true); } private void draw() { try { /* get a canvas object. */ canvas = sfHolder.lockCanvas(); /* set bg color. */ canvas.drawColor(Color.WHITE); canvas.drawText("this is a game." + (clock++), 100, 100, paint); } catch (Exception e) { } finally { /* Finish editing pixels in the surface. */ if (canvas != null) { sfHolder.unlockCanvasAndPost(canvas); } } } @Override public void run() { while (true) { draw(); try { /* delay 100ms to let us can see. */ Thread.sleep(100); } catch (Exception e) { // TODO: handle exception } } } @Override public void surfaceCreated(SurfaceHolder holder) { /* get screen size. */ screenW = getWidth(); screenH = getHeight(); /* start the thread. */ thread.start(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } }
标签:
原文地址:http://www.cnblogs.com/qingdujun/p/4436315.html