Android 画图类View与SurfaceView之学习

分享到:

http://blog.csdn.net/andyhuabing/article/details/12653183

Canvas 的使用:

lockCanvas  锁定画布,绘图之前必须锁定画布才能得到当前画布对象

unlockCanvasAndPost 开始绘制时锁定画布,绘制完成之后解锁画布

TestSurfaceView.java 代码:

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class TestSurfaceView extends SurfaceView implements  SurfaceHolder.Callback {
	//控制循环
	private boolean mbLoop = false;
	
	//定义SurfaceHolder对象
	private SurfaceHolder mSurfaceHolder = null;
	private int miCount = 0;
	public int x = 50, y = 50;
	private int mWidth = 1280, mHeight = 720;
	private Bitmap mBitmap = null;

	public TestSurfaceView(Context context) {
		super(context);
		
		//实例化SurfaceHolder
		mSurfaceHolder = this.getHolder();
		
		//添加回调
		mSurfaceHolder.addCallback(this);
		this.setFocusable(true);
	}
	
	  public void setDisplayWH(int w, int h) {  
	        mWidth = w;  
	        mHeight = h;  
	    }
	  
	   public void setBitmap(Bitmap bitmap) {  
	        this.mBitmap = bitmap;  
	    }  

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		mbLoop = true;
		
		Thread th = new Thread(new Runnable(){

			@Override
			public void run() {
				while(mbLoop){
					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					synchronized(mSurfaceHolder){
						drawBitmap();
						DrawData();
					}
				}

			}
			
		});
		th.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
		
	}  
	
    private void drawBitmap() {  
        // 锁定画布,得到canvas  
        if (mSurfaceHolder == null || this.mBitmap == null)  
            return;  
  
        Canvas canvas = mSurfaceHolder.lockCanvas();  
        if (canvas == null) {  
            return;  
        }  
  
        // 绘图  
        Paint paint = new Paint();  
        paint.setAntiAlias(true);  
        paint.setColor(Color.BLUE);  
  
        canvas.drawBitmap(this.mBitmap, 200, 0, paint);  
  
        // 绘制后解锁,绘制后必须解锁才能显示  
        mSurfaceHolder.unlockCanvasAndPost(canvas);  
    } 
    
    // 绘图方法  
    private void DrawData() {  
        if (mSurfaceHolder == null)  
            return;  
  
        // 锁定画布,得到canvas  
        Canvas canvas = mSurfaceHolder.lockCanvas();  
        if (canvas == null) {  
            return;  
        }  
  
        if (miCount < 100) {  
            miCount++;  
        } else {  
            miCount = 0;  
        }  
  
        // 绘图  
        Paint mPaint = new Paint();  
        mPaint.setAntiAlias(true);  
        mPaint.setColor(Color.BLACK);  
  
        // 绘制矩形--清屏作用  
        canvas.drawRect(0, 0, mWidth, mHeight, mPaint);  
          
        switch (miCount % 4) {  
        case 0:  
            mPaint.setColor(Color.BLUE);  
            break;  
        case 1:  
            mPaint.setColor(Color.GREEN);  
            break;  
        case 2:  
            mPaint.setColor(Color.RED);  
            break;  
        case 3:  
            mPaint.setColor(Color.YELLOW);  
            break;  
        default:  
            mPaint.setColor(Color.WHITE);  
            break;  
        }  
  
        // 绘制矩形--  
        canvas.drawCircle(x, y, 50, mPaint);  
          
        // 绘制后解锁,绘制后必须解锁才能显示  
        mSurfaceHolder.unlockCanvasAndPost(canvas);  
    }  
	
} 
昵    称:
验证码:

相关文档: