Android碰撞检测——Region碰撞检查

Region碰撞检测   

  Region类在android开发中经常用到,特别是该类的一个判断点是否在矩形区域内的函数——Region.contains(int x,int y)经常用到。

      下面来一个例子:

package com.example.collidedemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.Region;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class RegionSurfaceView extends SurfaceView implements Callback,Runnable {
    Paint paint;
    SurfaceHolder surfaceHolder;
    Thread thread;
    boolean flag;
    int sleeptime=100;
    boolean isCollsion;
    /*定义一个矩形*/
    Rect rect=new Rect(0, 0, 100, 100);
    /*定义一个Region*/
    Region region=new Region(rect);
    public RegionSurfaceView(Context context) {
        super(context);
        paint=new Paint();
        paint.setStyle(Style.STROKE);
        paint.setAntiAlias(true);
        surfaceHolder=getHolder();
        surfaceHolder.addCallback(this);    
        thread=new Thread(this);
    }
    
    private void mydraw(Canvas canvas){
        canvas.drawColor(Color.WHITE);
        canvas.drawRect(rect, paint);
        if(isCollsion){
            paint.setColor(Color.RED);
            canvas.drawText("点击碰撞了", 10, 10, paint);
        }
        else{
            paint.setColor(Color.BLACK);
            canvas.drawText("没有碰撞了", 10, 10, paint);
        }
    }
    
    
    /**
     * 触点事件
     */
    @Override
    public boolean onTouchEvent(MotionEvent event){
        if(region.contains((int)event.getX(), (int)event.getY())){
            isCollsion=true;
        }
        else{
            isCollsion=false;
        }
        return super.onTouchEvent(event);
    }
    
    @Override
    public void run() {
        Canvas canvas=null;
        while (flag) {
            try {
                canvas=surfaceHolder.lockCanvas();
                synchronized (canvas) {
                    mydraw(canvas);
                }
            } catch (Exception e) {
                Log.e("region",e.getMessage());
            }
            finally{
                if(canvas!=null){
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }
            }
            try {
                Thread.sleep(sleeptime);
            } catch (Exception e) {
                Log.e("region",e.getMessage());
            }
        }
        
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        flag=true;
        if(!thread.isAlive()){
            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) {
        flag=true;
    }

}

 

posted @ 2013-04-06 17:41  消逝的落叶  阅读(1297)  评论(2编辑  收藏  举报