SurfaceView和View的明顯不同在於Surface不需要通過線程來更新視圖,但在繪制之前必須使用lockCanvas方法鎖定畫布,並得 到畫布,然後繪制,完成後用unlockCanvasAndPost方法解鎖畫布。SurfaceView類的事件處理和View一樣。
首先來看一個簡單的框架。
繪制界面類:
- package com.example.bonusball;
-
- import Android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.RectF;
- import android.util.AttributeSet;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
-
- public class CanvasView extends SurfaceView implements SurfaceHolder.Callback
- {
- private SurfaceHolder myHolder;
- private Paint ballPaint; // Paint used to draw the cannonball
- private int screenWidth; // width of the screen
- private int screenHeight; // height of the screen
- private int ballRadius;
- private CanvasThread myThread;
- //控制循環
- private boolean isLoop;
-
- public CanvasView(Context context) {
- super(context);
- // TODO Auto-generated constructor stub
- myHolder=this.getHolder();
- myHolder.addCallback(this);
- ballPaint=new Paint();
- ballPaint.setColor(Color.BLUE);
- isLoop = true;
- }
-
- public void fireBall(float startX,float startY)
- {
- System.out.println("Fire");
-
- }
-
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- // TODO Auto-generated method stub
-
-
- }
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh)
- {
- super.onSizeChanged(w, h, oldw, oldh);
- screenWidth = w; // store the width
- screenHeight = h; // store the height
- ballRadius=w/10;
- }
-
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- myThread = new CanvasThread();
- System.out.println("SurfaceCreated!");
- myThread.start();
-
- }
-
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- // TODO Auto-generated method stub
- // 停止循環
- isLoop = false;
- }
- public void drawGameElements(Canvas canvas)
- {
- canvas.drawCircle(100, 100,ballRadius,ballPaint);
-
- }
- private class CanvasThread extends Thread
- {
- @Override
- public void run()
- {
- while(true)
- {
- synchronized( myHolder )
- {
-
- Canvas canvas = myHolder.lockCanvas(null);//獲取畫布
- drawGameElements(canvas);
- myHolder.unlockCanvasAndPost(canvas);//解鎖畫布,提交畫好的圖像
- //System.out.println("run");
- }
- }
- }
-
- }
- }
事件處理 類:
- package com.example.bonusball;
-
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Rect;
- import android.view.GestureDetector;
- import android.view.GestureDetector.SimpleOnGestureListener;
- import android.view.MotionEvent;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.widget.Toast;
- public class BallActivity extends Activity {
-
- private GestureDetector myGestureDetector;//監聽手勢
- private CanvasView myCanvas;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- myCanvas=new CanvasView(this);
- setContentView(myCanvas);
- myGestureDetector = new GestureDetector(this, new MyGestureListener());
-
- }
- @Override
- public boolean onTouchEvent(MotionEvent event)
- {
- return myGestureDetector.onTouchEvent(event);
-
- }
-
-
- private class MyGestureListener extends SimpleOnGestureListener
- {
- public boolean onDown(MotionEvent e1) {
- Toast.makeText(getApplicationContext(), "onDown", Toast.LENGTH_SHORT).show();
- return true;
- }
-
- @Override
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
- {
- System.out.println("Fling");
- return true;
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_ball, menu);
- return true;
-
- }
- }