2011-07-20 99 views
5

我試圖做一個應用程序,使用戶能夠觸摸屏幕並根據用戶的手指座標繪製圖像。這裏是我的代碼:安卓繪圖觸摸事件

public class DrawingBoard extends View { 

     Drawable editIcon = getResources().getDrawable(R.drawable.icon); 
     Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background); 

     float xPos = 0; 
     float yPos = 0; 

     public DrawingBoard (Context context) { 
      // TODO Auto-generated constructor stub 
      super (context);    
     } 
     @Override 
     protected void onDraw (Canvas canvas) { 
      super.onDraw(canvas); 

      canvas.save(); 
      canvas.drawBitmap(mBitmap, 0, 0, null); 
      canvas.translate(xPos, yPos); 
      editIcon.draw(canvas); 
      canvas.restore(); 

      invalidate(); 
     } 
     @Override 
     public boolean onTouchEvent (MotionEvent event) { 

      switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN : 
        xPos = event.getX(); 
        yPos = event.getY(); 
        break; 
      } 

      return true; 

     } 
    } 
} 

但是,每當我試圖點擊模擬器的屏幕上,沒有顯示圖像....

請指出我的錯誤... THX

回答

0

我已多次發佈此問題的答案,此代碼正在100%工作。如果u有任何仍查詢則u可以聯繫我

但此代碼將工作繪製圖像在Google Maps上:

public boolean onTouchEvent(MotionEvent event, MapView mapView) 
    { 

     if (event.getAction() == 1) 
     {     
      GeoPoint p = mapView.getProjection().fromPixels(
       (int) event.getX(), 
       (int) event.getY()); 
       Toast.makeText(getBaseContext(), "lat and longtd is \n "+ 
        p.getLatitudeE6()/1E6 + "," + 
        p.getLongitudeE6() /1E6 , 
        Toast.LENGTH_LONG).show(); // 
       mapView.getOverlays().add(new MarkerOverlay(p)); 
       mapView.invalidate(); 
     } 
       return true; 


    } 

,並定義另一個(第2)覆蓋類...此事件的會得到。

class MarkerOverlay extends Overlay 
{ 
    private GeoPoint p; 
    private Projection projection; 

    public MarkerOverlay(GeoPoint p) 
    { 
     this.p = p; 
    } 

    @Override 
    public boolean draw(Canvas canvas, MapView mapView,boolean shadow, long when) 
    { 
     super.draw(canvas, mapView, shadow);     

     //---translate the GeoPoint to screen pixels--- 
     Point screenPts = new Point(); 
     mapView.getProjection().toPixels(p, screenPts); 

     //---add the marker--- 
     Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.pir_pictr);    
     canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null); 

     return true; 
    } 



} 
+4

invalidate()這是什麼問題與谷歌地圖辦?? –

+0

@SimonAndréForsberg老兄,爲什麼你放棄這個答案的投票。你做了什麼?你看不到。此代碼將通過Google地圖在觸摸事件上繪製圖像。我不知道你有什麼問題。如果你不明白,那就給我一個機會,我會詳細說明它。 –

1

您不必在onTouchEvent()

 @Override 
     protected void onDraw (Canvas canvas) { 
      super.onDraw(canvas); 

      canvas.save(); 
      canvas.drawBitmap(mBitmap, 0, 0, null); 
      canvas.translate(xPos, yPos); 
      editIcon.draw(canvas); 
      canvas.restore(); 

     //  invalidate(); 
     } 
     @Override 
     public boolean onTouchEvent (MotionEvent event) { 

      switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN : 
        xPos = event.getX(); 
        yPos = event.getY(); 
        invalidate(); // add it here 
        break; 
      } 

      return true; 

     } 
+0

如果我在onDraw中直接使用xPos和yPos,爲什麼我們需要在onTouchEvent中失效? – mehulmpt