开发者

Android - Set focus on a circle that I have drawn in my onDraw method

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开发者_JAVA技巧 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)


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜