歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android中SurfaceView學習

SurfaceView和View的明顯不同在於Surface不需要通過線程來更新視圖,但在繪制之前必須使用lockCanvas方法鎖定畫布,並得 到畫布,然後繪制,完成後用unlockCanvasAndPost方法解鎖畫布。SurfaceView類的事件處理和View一樣。

首先來看一個簡單的框架。

繪制界面類:

  1. package com.example.bonusball; 
  2.  
  3. import Android.content.Context; 
  4. import android.graphics.Canvas; 
  5. import android.graphics.Color; 
  6. import android.graphics.Paint; 
  7. import android.graphics.RectF; 
  8. import android.util.AttributeSet; 
  9. import android.view.SurfaceHolder; 
  10. import android.view.SurfaceView; 
  11.  
  12. public class CanvasView extends SurfaceView implements SurfaceHolder.Callback 
  13. { 
  14.     private SurfaceHolder myHolder; 
  15.     private Paint ballPaint; // Paint used to draw the cannonball  
  16.     private int screenWidth; // width of the screen  
  17.     private int screenHeight; // height of the screen  
  18.     private int ballRadius; 
  19.     private CanvasThread myThread; 
  20.     //控制循環  
  21.     private boolean isLoop; 
  22.  
  23.     public CanvasView(Context context) { 
  24.         super(context);   
  25.         // TODO Auto-generated constructor stub  
  26.         myHolder=this.getHolder(); 
  27.         myHolder.addCallback(this); 
  28.         ballPaint=new Paint(); 
  29.         ballPaint.setColor(Color.BLUE); 
  30.         isLoop = true; 
  31.     } 
  32.  
  33.     public void fireBall(float startX,float startY) 
  34.     { 
  35.         System.out.println("Fire"); 
  36.  
  37.     } 
  38.  
  39.     @Override 
  40.     public void surfaceChanged(SurfaceHolder holder, int format, int width, 
  41.             int height) { 
  42.         // TODO Auto-generated method stub  
  43.  
  44.  
  45.     } 
  46.     @Override 
  47.     protected void onSizeChanged(int w, int h, int oldw, int oldh) 
  48.     { 
  49.         super.onSizeChanged(w, h, oldw, oldh); 
  50.         screenWidth = w; // store the width  
  51.         screenHeight = h; // store the height  
  52.         ballRadius=w/10; 
  53.     } 
  54.  
  55.     @Override 
  56.     public void surfaceCreated(SurfaceHolder holder) { 
  57.         // TODO Auto-generated method stub  
  58.         myThread = new CanvasThread(); 
  59.         System.out.println("SurfaceCreated!"); 
  60.         myThread.start();   
  61.  
  62.     } 
  63.  
  64.     @Override 
  65.     public void surfaceDestroyed(SurfaceHolder holder) { 
  66.         // TODO Auto-generated method stub  
  67.         // 停止循環  
  68.         isLoop = false; 
  69.     } 
  70.     public void drawGameElements(Canvas canvas) 
  71.     { 
  72.         canvas.drawCircle(100, 100,ballRadius,ballPaint); 
  73.  
  74.     } 
  75.     private class CanvasThread extends Thread 
  76.     { 
  77.         @Override 
  78.         public void run() 
  79.         { 
  80.             while(true) 
  81.             { 
  82.                 synchronized( myHolder ) 
  83.                 { 
  84.  
  85.                     Canvas canvas = myHolder.lockCanvas(null);//獲取畫布  
  86.                     drawGameElements(canvas); 
  87.                     myHolder.unlockCanvasAndPost(canvas);//解鎖畫布,提交畫好的圖像  
  88.                     //System.out.println("run");  
  89.                 } 
  90.             } 
  91.         } 
  92.  
  93.     } 
  94. } 

事件處理 類:

  1. package com.example.bonusball; 
  2.  
  3. import android.os.Bundle; 
  4. import android.app.Activity; 
  5. import android.view.Menu; 
  6. import android.content.Context; 
  7. import android.graphics.Canvas; 
  8. import android.graphics.Color; 
  9. import android.graphics.Paint; 
  10. import android.graphics.Rect; 
  11. import android.view.GestureDetector; 
  12. import android.view.GestureDetector.SimpleOnGestureListener; 
  13. import android.view.MotionEvent; 
  14. import android.view.SurfaceHolder; 
  15. import android.view.SurfaceView; 
  16. import android.widget.Toast; 
  17. public class BallActivity extends Activity { 
  18.  
  19.     private GestureDetector myGestureDetector;//監聽手勢  
  20.     private CanvasView myCanvas; 
  21.     @Override 
  22.     public void onCreate(Bundle savedInstanceState) { 
  23.         super.onCreate(savedInstanceState); 
  24.         myCanvas=new CanvasView(this); 
  25.         setContentView(myCanvas); 
  26.         myGestureDetector = new GestureDetector(this, new MyGestureListener()); 
  27.          
  28.     } 
  29.     @Override 
  30.     public boolean onTouchEvent(MotionEvent event) 
  31.     { 
  32.         return myGestureDetector.onTouchEvent(event); 
  33.  
  34.     } 
  35.  
  36.      
  37.     private class MyGestureListener extends SimpleOnGestureListener   
  38.     {   
  39.         public boolean onDown(MotionEvent e1) {     
  40.             Toast.makeText(getApplicationContext(), "onDown", Toast.LENGTH_SHORT).show();   
  41.             return true;     
  42.         }   
  43.          
  44.          @Override     
  45.          public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)   
  46.          { 
  47.              System.out.println("Fling"); 
  48.             return true;   
  49.          } 
  50.     } 
  51.     @Override 
  52.     public boolean onCreateOptionsMenu(Menu menu) { 
  53.         getMenuInflater().inflate(R.menu.activity_ball, menu); 
  54.         return true; 
  55.      
  56.     } 
  57. } 
Copyright © Linux教程網 All Rights Reserved