2010-06-11 126 views
6

我有一個程序,我想在觸摸事件的座標上放置圖像。我現在有了座標,我只需要在那裏放置圖像。我將使用drawable。座標位置圖像Android

編輯** 我也想疊加在另一張圖片上。我無法找到任何有關這方面的文檔。我認爲這應該很容易。

任何人都可以幫助我嗎?

編輯**** 明白了,現在我只需要弄清楚如何在觸摸點獲得圖像的中間,而不是左上角:

final View touchView2 = findViewById(R.id.ImageView02); 
    touchView.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      Log.v("x,y",event.getY()+","+event.getX()); 
      touchView2.bringToFront(); 
      int y = (int)event.getY(); 
      int x = (int)event.getX(); 

      touchView2.layout(x, y, x+48, y+48); 
       return true; 
     } 
    }); 

回答

0
Drawable recycle_bin = context.getResources().getDrawable(android.R.drawable.ic_menu_delete); 
int w = recycle_bin.getIntrinsicWidth(); 
int h = recycle_bin.getIntrinsicHeight(); 
int x = getWidth()/2 - w/2; 
int y = getHeight() - h - 5; 

recycle_bin.setBounds(x, y, x + w, y + h); 
recycle_bin.draw(canvas); 

這我怎麼在底部中心

+0

對我來說這不太合適,但謝謝。 – shaneburgess 2010-06-11 15:36:29

0

畫一個回收站圖標,在某一個地方一個圖像座標,你將不得不在畫布上繪製圖像。要獲得觸摸事件的座標,使用下面的代碼:

@Override 
public void onTouchEvent(MotionEvent event) { 
    if (event.getAction() == MotionEvent.ACTION_MOVE) { 
     mTouchX = event.getX(); 
     mTouchY = event.getY();//stores touch event 
    } else { 
     mTouchX = -1; 
     mTouchY = -1; 
    } 
    super.onTouchEvent(event); 
} 
+0

我有共同點擊座標,我怎樣一個形象的點擊位置?我的應用程序是支持的API-10我不能使用setX的()和塞蒂()。 – 2014-05-06 13:56:27

4

地方,你想在你的應用程序的XML頂部的圖像。 設置無形或消失......

取代:

final View touchView2 = findViewById(R.id.ImageView02); 

類構造函數之前:

ImageView touchView2; 

,並在構造函數(的onCreate)方法

touchView2 = (ImageView) findViewById(R.id.ImageView02); 

現在通過在屏幕上獲得所有觸摸來設置onTouchEventListener。 如果這些座標是在一個位置,你喜歡打電話與按下X和Y placeImage方法座標。這種方法被放置在類的構造函數(的onCreate)所以下面第一個方法應該是正確的外:

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    super.onTouchEvent(event); 
    int eventAction = event.getAction(); 
    switch(eventAction) { 
     case MotionEvent.ACTION_DOWN: 
      float TouchX = event.getX(); 
      float TouchY = event.getY(); 
      placeImage(TouchX, TouchY); 
      break; 
    }   
    return true; 
} 

現在placeImage方法:

private void placeImage(float X, float Y) { 
    int touchX = (int) X; 
    int touchY = (int) Y; 

    // placing at bottom right of touch 
    touchView2.layout(touchX, touchY, touchX+48, touchY+48); 

    //placing at center of touch 
    int viewWidth = touchView2.getWidth(); 
    int viewHeight = touchView2.getHeight(); 
    viewWidth = viewWidth/2; 
    viewHeight = viewHeight/2; 

    touchView2.layout(touchX - viewWidth, touchY - viewHeight, touchX + viewWidth, touchY + viewHeight); 
} 

這應該是你的答案......現在你只需要使touchView可見:

touchView2.setVisibility(0);