当前位置:操作系统 > 安卓/Android >>

Android中SurfaceView学习

  SurfaceView和View的明显不同在于Surface不需要通过线程来更新视图,但在绘制之前必须使用lockCanvas方法锁定画布,并得 到画布,然后绘制,完成后用unlockCanvasAndPost方法解锁画布。SurfaceView类的事件处理和View一样。

             首先来看一个简单的框架。

绘制界面类:


[java]
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.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)
 {补充:移动开发 , Android ,

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,