Android - 将焦点设置在我在 onDraw 方法中绘制的圆圈上

发布于 2024-11-19 19:03:52 字数 142 浏览 5 评论 0原文

安卓问题。 我制作了一个自定义 ImageView 类,其中有一个 onDraw 方法,它将在特定像素上绘制一个圆圈(使用画布)。当我使用这个自定义图像视图并打开我的图像时,我想将焦点设置在我绘制的圆圈上(例如,就像谷歌地图对您当前位置所做的那样。焦点设置为您当前的点)

Android Question.
I have made a custom ImageView class and inside it I have an onDraw method which will draw a circle on particular pixels (using canvas). When I use this custom imageview and open up my image I would like to set the focus on the circle that I have drawn (e.g like google maps do with your current location. The focus is set to your current point)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够爱げ你 2024-11-26 19:03:52

谷歌上的地图服务器所做的是提供一组定制的图块,以便正确显示中心,较新的版本当然是基于矢量的,因此他们只需绘制视图,使其在他们想要的位置居中。

在不了解应用程序的详细信息的情况下,您可能需要

创建自己的容器类,可能是 FrameLayout

public class myMapFrameLayout extends FrameLayout {

覆盖 onDraw 或 onDispatchDraw ,以便您可以适当地布局图块

弄清楚在哪里绘制位图,以便您需要的 x,y 位于屏幕中心,然后绘制您需要填充空白空间的其他图块,具体取决于图块移动到中心的方式所需的坐标

想象一个比实际屏幕大的虚拟屏幕,所有图块都其周围大小相同

1 2 3

4 X 5

6 7 8

假设 X 是显示器的大小并代表当前图块,您需要找出移动图块的方式以及哪些其他图块 1,2,3,4,5,6 ,7 或 8 你需要填充移动造成的空白

如果你必须从 0,0 绘制图块 +x 你需要一些图块 4,从 0,0 绘制 +y 意味着 2 的一些,两者都意味着1,2,4都是需要的等等,所以弄清楚组合并加载你需要的图块,并找出每个图块的绘制位置。这将为您提供新的虚拟图块,并显示中心。

我认为这与客户端位图绘制方法所能达到的效率差不多。

更新

由于您的评论表明您只有一张非常大的图像,如果您需要的 x,y 比显示器的尺寸更接近边缘,那么这将是一个

问题更少你仍然可以在需要的地方绘制图像,只需测量屏幕并绘制位图,目标x,y位于中心

所以如果屏幕是500x500并且你的图像是5000x5000并且中心位于位置x=1000 y=1000 那么

drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)

源矩形将为 1000-250,1000-250,500,500,目标矩形将为 0,0,500,500

250 是显示的中心 x 和中心 y,1000 是目标 x/y 坐标,并且500是显示的大小。

同样,对于位于边缘的目标,您的屏幕中将出现一个空白多边形,因为您没有无限的地图图块。

或者,您可以使用布局参数来超大框架布局,只需在 x 和 y 方向上平移画布即可获取画布以 x,y 为中心,您需要使用类似的计算,这可能会更具性能,但不太确定

请记住,如果您的图像非常大,您将使用大量内存

What the map server does on google is deliver a customized set of tiles so that the center is displayed properly, the newer version is of course vector based so they simply draw the view so it's centered where they want it.

Without knowing the details of your application you probably need

Create your own container class, probably FrameLayout

public class myMapFrameLayout extends FrameLayout {

The override either onDraw or onDispatchDraw so that you can layout your tile appropriately

Figure out where to draw your bitmap so that the x,y you need will be in the center of the screen, then draw the other tiles that you need to fill in the blank space at the coordinates required dependent on which way the tile was moved to get centered

Think of a virtual screen that is larger than the actual screen with tiles all around it that are the same size

1 2 3

4 X 5

6 7 8

Assuming that X is the size of the display and represents the current tile you need to figure out which way to move the tile, and which other tiles 1,2,3,4,5,6,7 or 8 you need to fill in the empty space caused by move

If you had to draw the tile +x from 0,0 you need some of tile 4, drawing +y from 0,0 means some of 2 and both mean 1,2,4 are all needed and so on, so figure out the combinations and load the tiles you need, and figure out the drawing positions of each. That would give you your new virtual tile with the center displayed.

That's about as efficient as you can get I think with a bitmap drawing method on the client side.

UPDATE

Since your comment indicates you have only one very large image this is going to be a bit of a problem if the x,y you need as anything closer to the edges than the size of the display

None the less you can still draw the image where you need it, just measure the screen and draw the bitmap with the target x,y in the center

So if the screen was 500x500 and your image was 5000x5000 and the center was at position x=1000 y=1000 then

drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)

where source rectangle would be 1000-250,1000-250,500,500 and dst rectangle would be 0,0,500,500

The 250 is the center x and center y of the display, 1000 are the target x/y coordinates, and 500 is the size of display.

Again, with targets that are at the edges you are going to have a blank polygon in your screen since you dont have an infinite map tile

Alternatively you could oversize your framelayout using layoutparams and just translate the canvas in the x and y to get the canvas centered to the x,y you need using similar calcs which may be more performant, not really sure

Keep in mind you are going to be using a lot of memory if your image is really big

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文