2012-03-12 91 views
0

有類,即在畫布上繪製一些領域安卓單擊畫布

package com.cerbertek; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Region; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

public class PlayGameView extends SurfaceView implements SurfaceHolder.Callback { 

    private CanvasThread canvasthread; 
    private Context mContext; 
    private Region firstRec; 

    private class CanvasThread extends Thread { 
     private SurfaceHolder _holder; 
     private boolean _run = false; 

     public CanvasThread(SurfaceHolder surfaceHolder) { 
      _holder = surfaceHolder; 
     } 

     public void setRunning(boolean run) { 
      _run = run; 
     } 

     @Override 
     public void run() { 
      Canvas c; 
      while (_run) { 
       c = null; 
       try { 
        c = _holder.lockCanvas(null); 
        synchronized (_holder) { 
         onDraw(c); 
        } 
       } finally { 
        if (c != null) { 
         _holder.unlockCanvasAndPost(c); 
        } 
       } 
      } 
     } 
    } 


    public PlayGameView (Context context, AttributeSet attrs) { 
     super(context, attrs); 
     SurfaceHolder holder = getHolder(); 
     holder.addCallback(this); 
     canvasthread = new CanvasThread(getHolder()); 
     setFocusable(true); 
    } 

    @Override 
    public void onDraw(Canvas canvas) {  
      Paint paint = new Paint(); 
      Bitmap wrench = BitmapFactory.decodeResource(getResources(), R.drawable.wrench); 
      canvas.drawColor(Color .BLACK); 
      for(int i = 0; i < 4; i++) { 
       for(int j = 0; j < 4; j++) { 
        int left = canvas.getWidth()/2 - wrench.getWidth()*2 + j*wrench.getWidth(); 
        int top = 0 + i*wrench.getHeight(); 
        canvas.drawBitmap(wrench, left, top, null); 

        Log.d(i + " " + j, left+ " " + top); 
       } 
      } 

    } 



    @Override 
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { 


    } 


    @Override 
    public void surfaceCreated(SurfaceHolder arg0) { 
     canvasthread.setRunning(true); 
     canvasthread.start(); 
    } 


    @Override 
    public void surfaceDestroyed(SurfaceHolder arg0) { 
     boolean retry = true; 
     canvasthread.setRunning(false); 
     while (retry) { 
       try { 
         canvasthread.join(); 
         retry = false; 
       } catch (InterruptedException e) { 
         // we will try it again and again... 
       } 
     } 
    } 
} 

,所以我想檢測,其中i點擊(用於爲例有2個rects,我想檢測RECT我點擊了什麼) 。我可以設置setOnClickListener我的觀點,但如何獲得點擊的位置? 然後我想同時提請設置的區域(這是正確的,是嗎?不?),並在活動我會檢查被點擊座標包含地區

所以 1)我怎樣才能獲得的點擊 2的座標)什麼是做所有這些事情的很好的方法,因爲我的想法經常很差

回答

3

看看setOnTouchListener

OnTouchListener實現具有以下簽名的方法:

公共布爾onTouch(視圖V,MotionEvent事件)

的MotionEvent有大約在觸控實際發生的信息。 (event.getX()event.getY()

+0

好的,我會試試!謝謝 – user1128677 2012-03-12 19:41:50