My task is to allow user to upload an image and let user to put a custom view anywhere he clicks. (Like taging a pic on facebook) That is simple by getting the touch location and dynamically adding view to a relative layout and setting leftMargin and topMargin.
But I want this custom component to get drawn in the 'exact' same position in every android device.
For example; Think that you uploaded a picture into your profile and tag someone, other people who look at your pic with different devices should see the tag in the exact same place relative to the pic.
I know that I have to take account of screen density as well but don't know how to manipulate x, y according to that.
Look at the example screenshot:
(Blue box is put by user click)
This screenshot is from a test device with screen density 1, another device with density 1.5 shows the blue box in a different position.
(Images Updated:)
This is a test code:
icon = new ImageView(getActivity());
icon.setBackgroundColor(getResources().getColor(R.color.blue));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
int density = this.getResources().getDisplayMetrics().density;
params.leftMargin = (int)(posX/density);
params.topMargin = (int)(posY/density);
Think that you uploaded a picture into your profile and tag someone, other people who look at your pic with different devices should see the tag in the exactly same place.
Then you do not want to be placing "a view to absolute X, Y for all android devices".
For example, suppose we have two devices, A and B. On Device A, your image is a small phone, and the image happens to be rendered at a size of 480 pixels high by 270 pixels wide. The user taps at a position that happens to be 80 pixels down from the top and 70 pixels in from the left.
Device B has a screen approximately the size of the country of Turkey. More specifically, your image is rendered at 1600km by 900km on an -mdpi screen density, in a portrait orientation, meaning the image is around 10,078,736,000 pixels high by 5,669,289,000 pixels wide.
You do not want to be putting your marker 80 pixels down from the top and 70 pixels in from the left. That will put the marker almost in the upper-left corner.
Assuming that you arrange to show your image at the same aspect ratio, you will want to save the percentages that the marker is from some corner. In this example, 80 pixels from the top is 80/480 or ~16.7% from the top, and 70 pixels in from the left is 70/270 or ~25.9% from the left. You would then apply the percentages to the size of the image on another device, and so show your marker ~16.7% from the top and ~25.9% from the left on the Turkey-sized screen. Or on a tablet. Or on a larger phone. Or on a TV. Or on whatever you are choosing to use.
Related
Is it possible to crop an image on Android?
For example:
If the image must fit in a 400w x 400h space and it is 600w x 400h, can you horizontally center align it within the 400w x 400h placeholder space and crop out 100 pixels on the left and 100 pixels on the right.
OR
If the image must fit in a 400w x 400h space and it is 400w x 600h, can you vertically center align it to the image placeholder and crop out 100 pixels on the top and 100 pixels on the bottom.
This may also apply to images of many different resolutions coming in from a server - regardless of the resolution of the image, can you accomplish the above?
I found this but this is specifically for users to be able to crop their images manually in the app (https://medium.com/mindorks/android-top-image-cropper-libraries-3bc4a4f8f2df), but I need to have the image pull in to a set placeholder size in the app from the API.
Thanks!
Im facing the next problem: I have an imageView that change the size programmatically because de density hdpi has a 480x800 resolution. I change the size with layoutParams.
After I change the size, I want to position the imageView. When I position the view for example to the coordinate (0,0) I saw that between the imageView and the limit of the screen is a little space, like 25 inches. The imageView moves to other place and when I want to position it again to (0,0), the space between the imageView and the limit of the screen disappear. Why is happening that? I need to fix that because the first time is showing wrong, I don’t want the space!
Hope your understand
Greets
I am developing a simple game using libgdx in android. I created an actor. I want to resize the actor according to the screen size. Can anyone please suggest me a simple tutorial to do the same
well it depends on how you set the viewport of your Stage.
lets say your stage viewport is set to stage.setViewport(16,10,false) within your resize()-method.
This way, your screen will always show the region from (0,0) in the bottom left to (16,10) in the top-right corner...
If you create an Actor with width=8 and height=5, it will always be half of the screen width and half of the screen height big.
Of course, the example above will usually distort your screen when it resizes... So you could use the setViewport(16,10,true); This way, you will keep the aspect ratio, but might need to resize or reposition some actors within the resize() method, e.g. when they are to be shown on the right edge of the screen.
The other way is by setting the viewport to the same coordinate systems as the pixels on the screen: stage.setViewport(width,height,false); (within the resize(...)-method). This way, one unit in the stage coordinate system equals one pixel on the screen.
When doing so, you will have to resize the actors in the resize() method after setting the viewport:
actor.setSize(0.2f*stage.getWidth(), 0.2f*stage.getWidth() * actor.getHeight()/actor.getWidth()):
Doing so will always make the actor be exactly one fifth of the screen width and have its height matched so it doesn't get distorted.
Hope this helps a bit...
If you are using the latest version (nightlies) you may have seen the Viewport class.
This class is what you are looking for. Lets say you have a 1600 * 900px monitor and you set your Viewport to 16 * 9, every unit in your Viewport will be 100px on your Screen.
This means, if you set your Stages Viewport to 16 * 9 an Actor with the size of 1 will be 100*100px big. If you are using a 800*450px monitor it will be 50*50px big.
There are also different Viewport types:
StretchViewport: Your Virtual Viewport gets stretched to fill the whole Screen. So if you have a 1600 * 900 monitor and a Virtual Viewport of 16/10, a 1x1 Actor will be 100px wide, but only 90px heigh. So the aspect ratio is not the same anymore.
FitViewport: If your Virtual Viewport has a different aspect ratio the Screen will be filled with black bars. So on a 1600*900 Screen with 16/10 virtual Viewport a collumn of 80px width on both sides of the Screen will be black ((virtualWidth / wirtualHeight) * realHeight = realWidth, physicalWidth - realWidth = blackWidth, to have the game in center blackWidth/2 on both sides), to keep aspect ratio.
There are also others, but those are the most important 2 imho. You can read more on the article i linked.
If you are not using the latest nightlies, you need to use camera. The constructor of the OrthographicCamera takes the viewport width and height. Those are the same as for the viewport. The difference is, that it is a "StretchViewport" automatically, so you have to manually set the glViewport like in this tutorial.
I hope i could help.
Say I have a background image as such.
http://i.imgur.com/rRCtzyl.png
I want to put a button or text right inside the white box. My text or button must scale and fit the box perfectly when the image is scaled according to device screen size. The image does not fit the screen vertically so it is scrollable. I know that I need to put them in a group if I want them both to scroll together.
But how do I place the button inside the box? Since its scrollable I dont know the exact x and y values of the box. Not to mention these values will change according to device screen size. And how do I make the buttons scale to fit the box with all screen sizes?
This image is only an example. Of course I wouldn't have a problem if I drew the box myself programmatically. I would know where to put the button. Assume I dont know the coordinates and the size of the box since I didn't d
If your box object is called box, set the button to the box.x and box.y like this:
object1.x = box.x
object1.y = box.y
The scroll aspect should not matter if you put the button and image in same group and scroll the group. Then all you need to do is position the button relative to the image once, the rest will be taken care of by corona.
What will matter is the screen resolution. One way of tackling this is to view the image at full resolution and compute at what x and y pixels the box is located. For instance,
image: 200 x 300 pixels
box top left: 100, 100 (pixels from top left of image)
box width height: 20, 60
Then you use ratios: top left of button box is at 0.5, 0.3333 of image size; the button box width is 0.1 and height 0.2 of image size. Then when you start app, you compute what will be the image scaling factor based on actual resolution and you apply that to box coordinates:
device 300 x 400:
image will be scaled 1.5x in width, 1.333x in height
so button box should be at (200*1.5*0.5, 300*1.333*0.333)
with width 20*1.5 and height 60*1.333
I want to identify a certain part of an ImageView -- based on the actual size of the image regardless of how the image is scaled for the particular device.
Example: I have an image that is 480x320. I want a rectangle that (at this size) is defined as
Rect(10,35,20,50). What I'm wanting to do is figure out how I can define this rectangle relative to the actual size of the image when it scaled and displayed on the screen.
Can you just scale each part of the rectangle? For instance, if your image is displayed at 2x -- 960x480 -- then would the correct rectangle be (20,70,40,100)?
Use percentages from a pivot point: the top-left or the bottom-right.