简体   繁体   English

如何在Android中的确切触摸位置放置图像图标

[英]How to place an image icon at the exact touch location in android

I am placing an icon on my screen in my android app on the first touch. 第一次触摸时,我在Android应用程序的屏幕上放置了一个图标。 I have been able to implement the same, but my question is regarding how the position of this image can be. 我已经能够实现相同的功能,但是我的问题是关于此图像的位置如何。

When i use a factor of 20 and use the following code in side my onTouch event, 当我使用20的因子并在我的onTouch事件中使用以下代码时,

Bitmap img = BitmapFactory.decodeResource(Res, R.drawable.myPoint);
canvas.drawBitmap( img,  vec.get(i).x - 20, vec.get(i).y - 20, null);

The image seems to be superimposed on the point of my touch (i am not sure which coordinates). 图像似乎叠加在我的触摸点上(我不确定哪个坐标)。 And the output looks like this 输出看起来像这样 在此处输入图片说明

When i do not use any factor and use the following code 当我不使用任何因素并使用以下代码时

      Bitmap img = BitmapFactory.decodeResource(Res, R.drawable.myPoint);
       canvas.drawBitmap( img,  vec.get(i).x , vec.get(i).y , null);

The image seems to be placed slightly away from the point of touch as seen here. 如此处所示,图像似乎放置在离触摸点稍远的位置。 在此处输入图片说明

My aim is to have the pointed tip of the image(on the right) correctly point to the exact coordinates where the touch happened so that i can shoot out a path from there to the next click position like this. 我的目的是使图像的尖端(在右侧)正确指向触摸发生的确切坐标,这样我就可以像这样从此处拍摄到下一个单击位置的路径。 在此处输入图片说明

Will this require measuring the dimensions ie width and height of the image to place the image at the required location.How does android place such large images by default with respect to a normal small touch point. 这是否需要测量尺寸(即图像的宽度和高度)以将图像放置在所需位置。默认情况下,Android如何相对于正常的小触摸点放置如此大的图像。 Is the centroid of the image taken into consideration? 是否考虑了图像的质心?

The x and y arguments of drawBitmap refer to the top left corner of the bitmap. drawBitmap的x和y参数指向位图的左上角。

Here would be the proper (x, y) coordinates if there is no border around the image you gave (assuming it is symmetric along the x-axis): 如果您给定的图像周围没有边框(假设它沿x轴对称),则这将是适当的(x,y)坐标:

x = touchX - bitmapWidth;
y = touchY - bitmapHeight/2;

Measure the Bitmap you're drawing, then add those values to the draw coordinates. 测量要绘制的位图,然后将这些值添加到绘制坐标。

canvas.drawBitmap(img, vec.get(i).x + (img.x / 2), vec.get(i).y + img.y, null)

That code should give you an idea of what to do. 该代码应使您知道该怎么做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM