Place image in Android coordinates

I have a program in which I would like to place the image in the coordinates of the touch event. Now I have the coordinates, I just need help in placing the image. I will use drawable.

Edit ** I also want to overlay it on top of another image. I can not find any documentation about this. I think it should be easy.

Can anybody help me?

EDIT **** Got this, now I just need to figure out how to get the middle of the image at the touch point, and not in the upper left corner:

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; } }); 
+6
android image
source share
3 answers

put the desired image on top in the xml of your application. set it invisible or leave ...

replace:

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

before the class constructor:

 ImageView touchView2; 

and in the constructor method (onCreate)

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

Now configure onTouchEventListener, having received all the touches on the screen. If these coordinates are in the position you like, call the placeImage method with the X and Y coordinates pressed. This method is placed outside the class constructor (onCreate), so the first method below should be right:

 @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; } 

now the placeImage method:

 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); } 

This should be your answer ... now you only need to make the touchView visible:

 touchView2.setVisibility(0); 
+4
source share
 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 ); 

what I draw a basket icon in the center below

0
source share

To place the image at specific coordinates, you will need to draw an image on the canvas. To get the coordinates of a touch event, use the following code:

 @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
source share

All Articles