ImageView and Preset Position

I have an image in action. How can I establish the position of this image in my activity. I know how to do this in an XML file, but I want to do it in action, because I have an onTouch method where I get the coordinates, where I clicked, and I want to draw these images in these coordinates.

+6
source share
2 answers

@ edi233--

I think you can get touch x and y so that you can calculate the top edge and left edge. I think that (0,0) will be the upper left corner, so if you touch (100,75), you need to set the image margins in the upper part - 75 and leave up to 100

LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.setMargins(100,75, 0,0); // OR params.topMargin= 100; image.setLayoutParams(params); 
+10
source

use View.layout (int, int, int, int) to set its position

+3
source

Source: https://habr.com/ru/post/911933/


All Articles