When using Android's VelocityTracker to track MotionEvents, is it necessary to account for screen density to return the "density independent velocity"?
velocityTracker.computeCurrentVelocity(1); // pixels per milliseconds
float velocityY = velocityTracker.getYVelocity();
Do I have to multiple this velocityY with a screen scale factor so that I can track the same physical gesture velocity across all devices?
For most uses you you might have, you don't need to worry about that.
Let's think about the case of scrolling a custom view: if the user moves his finger 1cm, it could be 100px or 400px depending in the screen density. Anyway, what you want to move is to move the drawing the same number of pixels, without scaling them.
One exception could be if you are writing a game and the distance the user drags his finger means the power they are going to use to perform some action. In this case it would probably be fair to adjust for the screen density.
Summing up: if you want to update the interface, you don't need to worry about it. If what you want to do with this velocity is independent of the screen, you probably want to adjust it.
Related
I am using the formula to calculate speed and am using the screen width as distance and fps as time. The speed of the sprite is still different when moving across the screen. How should I change the formula in order to make the speed feel the same on all screen sizes?
public static float distanceX = Screen.width;
public static float time = 60f ;
public static float speed = Player.distanceX / Player.time;
To answer the question, you have to define what the "feel" needs to be. For example, suppose the device is held in landscape orientation. A 4:3 device is much narrower than a 16:9 device, so if you have a bunch of squares arranged horizontally on the screen, you'll have more of them on the 16:9 device than on the 4:3 device.
If your goal is to move from one side of the screen to the other in a constant amount of time, then the formula you show in your question is correct.
If your goal is to move past N squares in a constant amount of time, then you need a different formula. It sounds like this is what you want.
One approach is to define a device-independent "arena" in which everything moves, and then fit it to the device. For example, you could define the world to be 1000x1000, and that things move at 100 units per second. You then fit the arena to the screen by either cutting off the top and bottom, or letter-boxing the sides. A side-scrolling platformer would usually match the arena to the device height, and then show a wider or narrower field of view horizontally. (For an example of this approach, see Android Breakout.)
On a related note, it's unwise to assume that a device has a certain fixed frame rate. Some advice can be found in this appendix.
I am using the getPressure(index) method from the MotionEvent instance to get a value of the pressure applied to screen.
I am trying to figure out how to convert that value to at least an approximation of a standard measurement unit.
in Android the pressure value is a float ranging from 0 to 1. I need to express it in Newtons in some way.
From what i understood this is different across devices so its not possible get a really precise unit measurement but i am fine with an approximation.
Like what amount in newtons is normal for a stylus touching the screen on full force (the device measuring 1.0f of pressure)
I think you can only guess, and know that the results will be affected by huge uncertainity. Solutions I see:
Put an object of appropriate, known weight on the screen. Don't know about screens, but if it needs human skin to trigger the event, you can put your finger on the screen (making no strength on it) and then put some object on your finger.
Take a stylus, and by debugging learn how much force you need to get a 0.5f result. Then take a scale (foreign speaker here; I mean the tool that measures weights..?) and apply the same pressure on it with the stylus, and read the results.
In both cases, you can have a single map point (e.g., 0.5f -> 10 N), and then assume a linear dependency (knowing also that 0f -> 0 N) to fill the whole range.
With some patience you can fill different values too - I would not expect the relation to be linear actually.
getPressure returns a value 0-1 because the way pressure is calculated is device-dependant. Some devices will calculate the value from how much of the area of your finger is touching the screen. So from that it's probably not possible to convert to newtons in a way which will work on multiple Android devices unless you write a solution for each one.
I'm attempting to put together a slick animation in which a view that takes up most of the screen space rotates to reveal another view on the rear side:
rotatingView.animate().rotationY(90)
.setDuration(250)
.setInterpolator(new AccelerateInterpolator())
.withEndAction( /* Runnable that sets up the rear side of the view,
then rotates Y another 90 degrees */);
Problem is, about 80% of the way through this animation, the view disappears. The view doesn't reappear until about 20% of the way into the second half. Put another way, it disappears at 11 o'clock and reappears at 1 o'clock. Why is this?
I finally stumbled on the answer to this type of problem. UsesetCameraDistance(), but note that:
The distance is expressed in "depth pixels." The default distance depends on the screen density. For instance, on a medium density display, the default distance is 1280. On a high density display, the default distance is 1920.
So, to "raise" the camera to the point where the near edge of a view doesn't clip it, set the distance to several times the "default" distance. In my case with several tablet screens, a value of 4000f was needed. See the link for more onsetCameraDistance()parameter calculation.
You may want to combinesetCameraDistance()with clipToPadding="false" and clipChildren="false" in your XML file; these control masking of one view by another.
Also, I suspect YMMV across different Android releases and vendors.
I'm looking for an Algorithm or Pattern to calculate where objects can be placed on multiple resolutions. So far, I have got the X and Y screen size, but im not sure how to turn it into a calculation that would place something such as a drawText() at a location on the screen no matter the screen size.
I was thinking perhaps using percentages might be easier to work with. I have a program that draws text to the screen at the top left corner indicating what position the screen has been touched.
when i run this on the emulator, with the
drawText(info, 10,10, paint);
it displays great, but when i run it on my phone (Droid 2 Global) the top of the text is cut off.
In short:
Is there any way to draw something to the screen (using SurfaceView) that will remain in the same spot over multiple screen dimensions / dpi?
Thanks for your time.
It's no perfect solution that i've seen so far.
I came across this issue by specifying the position for the particular item for the specific screen ratio(native screen resolution in an emulator), then recalculate its position and scale it up/down when running it in the different screen size.
displayXpos = constDevelopmentScreenXpos (targetDeviceScreenHeight/constDevelopmentScreenHeight) etc..
displayXScale = similarAlgorithm
This is not the best, but it give you some idea.
Also, i fill some 'limbo' area with a background and choose to not care it when the target device X:Y screen ratio is different from development device.
HTH
ya, it is better to use calculation in percentage
first get the total size avail of the screen, then calculate percentage on it and the place the control based on calculation
to get avail size
Display mDisplay= activity.getWindowManager().getDefaultDisplay();
int width= mDisplay.getWidth();
int height= mDisplay.getHeight();
I am trying to make an app whereby a picture will be shown, there're a few objects that user needs to identify (i.e. a cup), by touching the cup in the picture on the screen, a circle will be drawn if it's valid.
So far, I have a surfaceview with a bitmap as drawable to be displayed full screen, and upon touching the screen, a circle will be drawn to the view.
I could only think of getting the coordinate of the cup manually in the picture(hardcoded), and check it against the coordinate from touch event.getX() and event.getY(). But this would not work as screen resolution changes.
What is a better way in doing so? It's like I'm trying to find a way to precisely allow touch on certain areas which I've defined in my app.
You can store the coords as you were suggesting but adjust them based on the size of the SurfaceView vs. the size of the BitmapDrawable. For example, if you know a given area is 100px from the left, you could take your scale as surfaceView.getWidth() / bitmapDrawable.getIntrinsicWidth(). Multiply your scale times your value (100px) to get the final position. If you're keeping the same aspect ratio on the image, you need to get your scale from the larger of the two (width vs. height) and use that same scale for height and width.
In a given resolution, is the location of cup fixed or it varies there as well.
Also, what about the orientation (landscape, portrait)? does the location change accordingly as well?
There are standard resolutions, so you can always think of mapping the location with resolution.
Also, you can get X, Y coordinate and check if it falls within a range based on height and width of cup image.
First of all find out the size of screen (screen dimensions).
Now place objects relatively corresponding to your screen area.
eg. keep cup on SCREENWIDTH - 60 and check coordinates of touch similarly.