2010-06-11 127 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);