Android making scale (ruler) on screen - android

In my application I want to create a ruler to measure length. I have gone through some ruler application in google store. I want to achieve somewhat as shown in the picture.
So my question is how do I draw this marking as per users mobile screen configuration. Is it using using some drawable image or some drawing on canvas.
I am new to android development. Also that the ruler when dragged downward marking and numbering move. So how to achieve that thing.

These kind of things that requires programmatical access to the data is mostly done using 'drawing to the canvas', which in this case is a window spawned by a service.
You should look into overlay activity implementations such as bubbles for android, which lets you peek into the way this is usually achieved, which is by creating a service that adds a window to the system that draws this, from there you can get the screen details, width and height to draw the ruler with.
Inside the spawned window you can add your own subclass of a view that implements the onDraw method in such a way that the ruler is drawn on the canvas.

Related

how to make a rectangle sprite stick to one side in Unity?

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.

Android game board with irregular board spaces

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.

Libgdx Hud with two stages

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.

Create draggable edge point on imageView

I want to create a resize feature of my application but I cannot find a sample implementation of it. Many tutorials offers pinch to zoom.
My problem now is how to implement draggable edges to resize the image just like in ordinary image editor. Do you have any idea how to do this? Just like putting circles/points on the 4 corners of the ImageView like the sample image below.
Is it possible to use the crop library of android to have draggable image?
You can draw the control point, and handle all festure in this point
last month i develop a Android handwritting note app. In this app, i inherit View, and control View.onDraw directly. I draw the image and all control indicator in onDraw, and handle screen touch within the scope of the screen.
This app isn't accomplished, it become a opensource project, you can see the source at https://bitbucket.org/gd920129/white-board/

Android View options - draw to a canvas directly or use views

I'm converting a project that I wrote in AIR a long time ago to native Android. In AIR, positioning views was fairly easy in x,y coordinate systems. With native Android though, I am trying to approach this in a correct way, but I'm unsure how to approach.
My view will consist of two circles in the background, with small objects within those circles that can roll around. There will be another view drawn on top of the circles to make it seem like there is glass over the circle, entrapping the small objects. The small objects are bound to the background circles.
I guess what I'm really asking is canvas drawing the best approach here, or is a view-based layout workable as well?
It sounds like your best option would be to use a SurfaceView. This is basically a hardware accelerated canvas. On of the benefits is that you can overlay standard widgets over top of it if you need to so you can mix and match custom and standard components. Here is a link to a website that walks you through getting a SurfaceView up and running

Categories

Resources