I've got the following problem:
I display a point on a SurfaceView (e.g. one specific pixel like (x=100,y=100)
on the screen.
Now I want to zoom to this specific point with different zoomlevels.
The zoomlevels are calculated by different scaleFactors. But I don't know
how I can display and scale the specific part of the screen?
Perhaps it would be also possible to move to the known displayed parts by "Scrolling about the screen" (using an OnTouchListener).
Have you tried to Canvas.scale() to the specific point in the drawing loop of the SurfaceView?
http://developer.android.com/reference/android/graphics/Canvas.html#scale%28float,%20float,%20float,%20float%29
Related
I am using a square sprite in Unity for making Android game. I want this rectangle to be always placed at the bottom of the screen. This strip acts as a ground and other objects can fall on it. I want the lower side of this rectangle to stick to the lower side of the screen. How to do it?.If i try to place it there manually then after I change the screen resolution the placement gets disturbed?Also the camera is not moving, so I only want to fix the position of this strip with respect to the camera once. (i think so). What should i do?
We need a little more information. Sprites at the lower side of the screen, usually intend to be in the UI. Is this the case? If it is, the way to do it is very different from a normal sprite in the world.
(Since I am not allowed to comment, I will try to answer for both cases, however I am not at a computer capable of running Unity, so I can't really provide a concrete answer (aka: Code))
UI:
Add an Image to your Canvas. Go to your anchor preset (inside the Rect Transform) and set it to the appropriate position. Then, in case you want the image to not be stretched, go to the Image component and Check on the "Maintain Aspect Ratio" (Or something like that) option. Add the sprite to the image and you are all set.
World:
Here, the situation is way more complicated. You first need to get the screen dimensions, then calculate the size of the object according to the aspect ratio, then have some Camera.ScreenToWorldPoint conversion and finally use the LookAt method in the Update() in order to have the sprite face the camera at all times. Or use the UI layer as described above, let's be honest that is probably what you need.
I'm creating an Android board game with several differently shaped board spaces (like Risk).
I want to be sure that my board appears correct and that OnTouchListeners stay in place on the GUI regardless of screen size/resolution.
Possible solutions I have thought of and their problems:
Create a single image for the board and assign OnTouchListeners based upon pixel geometry. Problem: If the user's display is a different resolution, my Listener might not be under the same pixels as my image (right?)
Create several ImageButtons and arrange them together. Problem: the ImageButtons might get rearranged based upon the display and I would end up with overlapping spaces or gaps.
Use Android custom drawing. If I do this, how would I link my Listeners to my Canvas and be sure that they are synced?
Basic question:
How to be sure that listeners sync with graphics in a GUI that uses irregular geometry?
I worked on an app with irregular touch areas so I can give you guidance on one way to achieve this.
Start with a single image for your entire board. This image is going to have a certain ("intrinsic") width and height regardless of any device resolutions.
Now here comes the tedious part. You (or maybe your graphic designer) will need to plot out coordinates of an irregular polygon for each touch area. These will be constants to your application.
When you are displaying your board, if you are zooming and panning on the image, you want to keep track of the transform matrix for the display. When the user touches the screen, you will get x,y coordinates from OnTouchListener and for those to be useful, you will have to "de-transform" the x,y to normalize it against the intrinsic dimensions of the board and your polygons.
We rolled our own hit-testing logic using an algorithm from http://alienryderflex.com/polygon/, but you can also try this: Create a Path out of your polygon coordinates (using moveTo(), lineTo(), and close()), then assign the Path to a Region using Region.setPath(). Once you have that, supposedly you should be able to hit-test using Region.contains(x,y), but I've never tried it so I can't guarantee that's going to work.
I have an app that's currently working with a single stage but i need to add a side display/section as a HUD, with scores/lives etc on it, so that the HUD is on the left, and the main hand screen on the right. The main game screen will be fixed and will not move around.
From researching I've found a couple of solutions.
1 - two stages
2 - a group with two groups to it, possibly using a horizontalgroup
3 - two cameras one stage
4 - one stage, one camera, but changing the position of the camera for each set of actors.
I think, option 1 is my preference, but i have some questions.
Do stages always fill the whole screen, or can i start then where i want? This would make it easier for the right hand screen to calculate positions based on 0,0 of that screen rather than always having to add the width of the HUD on to any calculations.
Do i need to work about viewports? Currently I'm not using one (which i think means my stage is set to scaling by default) but nothing looks stretched as a result of this. I don't know much about viewports, but there always seems to be a compromise to be made with them, i.e. black bars top or sides.
If I have two stages, do they each have their own camera? Do I need to with about this? Can I possibly aim the right hand camera at an offset so i can still draw things from 0,0 with that being the bottom left corner of the right stage, not the whole screen?
Finally, off topic, I am a little confused about spritebatch. I'm not currently using one, because I use a stage. Is that OK, or should i still be using one in conjunction with a stage somehow? And add all my actors to that?
It I understand correctly, you're using scene2d for your game world and also for your HUD. And the HUD doesn't overlay the game world, but rather uses its own portion of the screen exclusively.
Stages do not always fill the whole screen. They have no concept of filling or not filling anything, because they can have objects that are being drawn off screen. However, they are clipped to a rectangle defined by their Viewport.
In your case, it seems you need two Viewports, and therefore, two stages. You say you aren't using a Viewport, but you are...Stage automatically creates its own ScalingViewport that's set up like a StretchViewport. (ScalingViewport is not mentioned in the documentation, which is out of date.) StretchViewport is usually bad because your game will be distorted to fit whatever the aspect ratio of the device is.
ExtendViewports do not cause black bars as long as you don't set a max width/height on them and I think are usually the best choice for any game world view.
You can set your two Viewports to cover specific parts of the screen that you calculate yourself. Since this is a specialized case, I think you will have to directly subclass the Viewport class (not one of its subclasses) and manipulate each of them using viewport.setScreenBounds(...).
Regarding your last question: yes, each of the two stages has its own Viewport, and each Viewport has its own camera. Once you set up your two Viewports to each have their own portion of the screen, you can also set them to treat their respective bottom left corners as 0,0.
I'm working on creating a mobile app which overlays images on top of a google map.
I have a large number of image overlays ('GroundOverlay' objects in KML-speak). I'm running into several issues (mainly performance) when the map is scrolled or zoomed
Having tried several options, I think my next approach will be to combine all the image overlays into one image beforehand, and then simply display that image as a single overlay on the map. Problem is, I'm not sure where to start.
Does anyone have any experience in combining overlay images?
I think there are two problems that need to be solved
1) Calculate the larger 'bounding box' that will contain the final image. I have the bounding box for each overlay ('LatLngBox' in KML-speak), and I think the final box can be calculated by simply examining the values of each LatLngBox and generating the final box based on the min/max values. Anyone have any insight as to whether this will work?
2) Merge all the overlay images into a single final image. I have no idea where to start here such here. Generating the actual image isn't the problem, but rather where to place each overlay (ie pixel level) so that the resulting image is accurate.
Any tips/hints would be greatly appreciated.
Thanks
the static overlay images can be combined and drawn. but the moving(regularly updated) overlay images would be an issue if you still want to combine them and post as one. the best option i believe would be to combine the static overlay resources and keep them in one set and other moving images drawn separately.
Managed to figure this out on my own.
Answer to (1): The technique I outlined in my question works perfectly
Answer to (2): You can convert between lat/lng and x/y pixels of an image as described here: Convert Lat/Longs to X/Y Co-ordinates
End Goal: Getting a speedometer style needle to move around a dial according to test results.
Current Issue: At the moment I have placed the needle on top of the speed dial as a sperate image using FrameLayout. I have been scouring the web for a way to position the needle with coordinates so I can have it exactly where I want it on the dial. I would post an image but I'm new to the forum and I'm not allowed!
So in short can I position an image over another image with exact coordinates (allowing for rotation etc)? Am I going the right way about solving my problem?
There is the AbsoluteLayout, which lets you use absolute positions, but it is not recommended that you use it.
Instead, you should create your own extension of a View, that draws your dial and needle. For more info on this, look at Maurycy's comment, which recommends this part of the manual.
Calculate measurements and rotate image of indicator accordingly.