I'm trying to get a background in a live wallpaper to behave like a regular wallpaper with regard to scrolling when the user changes homescreens. I know the method required for this is onOffestsChanged, but I can't seem to get it working.
Does anyone have advice or a code snippet to get this working?
Have your engine implement onOffsetsChanged. The xOffset variable is a float value from 0 to 1 with 0 being the leftmost screen and 1 being the rightmost. Use the width of the screen (from onSurfaceChanged) and the width of your image to determine the left x coordinate with which to draw your image.
(screenWidth - yourImageWidth) * (1 - xOffset);
This should work with both screenWidth > yourImageWidth and screenWidth < yourImageWidth.
Related
I'm using GVR Android library version 1.190 and trying to play both 360 and 180 degrees videos in the video360 example project.
In both cases the 2D view (MonoscopicView) starts the playback fine, but the viewer camera position is never centered to the center of the video. It instead starts randomly off-centered by horizontal axis. Same behavior on multiple devices.
Anyone knows how to center the view to the video center when 2D view starts?
Turns out that sensor data from Sensor.TYPE_GAME_ROTATION_VECTOR are having very different values (angles) every time my activity register a listener to it. It only takes a small tilt of the phone to get really different values. Different devices also respond differently but they all have offset readings.
This lead to the initial view angle being positioned (usually) 90 degrees either to the left or right from the center of video.
Thanks to this post, I managed to calculate the initial heading offset and rotate phone position matrix to compensate.
Add a member variable private float initialHeading with initial value 0.
Then, in PhoneOrientationListener's onSensorChanged add the following code after the Android to OpenGL matrix rotation:
if (initialHeading == 0) {
initialHeading = (float) ((angles[0] + 2 * Math.PI) % (2 * Math.PI));
}
float angle = (float) ((Math.PI - initialHeading) * 180 / Math.PI);
Matrix.rotateM(phoneInWorldSpaceMatrix, 0, angle, 0, 1, 0);
I am creating layout for finger selection. In this I am trying to achieve click events for each individual finger. This layout should be uniform on any type of screen resolution.
My approach:
Inside relative layout, I am assigning radio buttons (not radio group but individual) to each finger inside hand image using margins and padding but it is not resting properly over finger image. They are slightly moving left or right.
Problem in this - radio button positions is changing if screen resolution changes.
I failed to find library for such click events. Also in SO I didn't find any related questions. Can someone guide me in this to library or example or better approach than this?
A several years ago I worked on the similar task. Unfortunately, I don't remember the whole solution, but idea was pretty simple. In my case it was an image of the map where a user could select a district by tapping the map. I knew the resolution of the original image that I used to display in UI. I encoded each district against its boundaries so it gave me a list of pair's number. I had a touch listener attached to ImageView that was used to display the map. So every time a user clicked on the map I got a position of his click, multiply this value by a scale factor(this one was calculated based on the size of original image and the one that was scaled by Android). Then I checked if that value laid in any polygons.
So to make it more clear:
Let width, height = size of original image
x, y = user touch
scaleWidth, scaleHeight = size of the image displayed by Android on the user device
scaleX = scaleWidth / width, scaleY = scaleHeight / height
originalX = scaleX * x, originalY = scaleY * y
Then check if originalX and originalY fits in polygons. In your case those polygons could be just squares around every finger.
I have a huge Quad and a PNG as a child object.
The PNG covers the whole game area / screen.
I know how to detect taps, but how can I detect when a user tapped on the left side of the PNG/quad vs. the right?
Should I place 2 gameobjects above the big quad to capture inputs on right or left? Or maybe I could capture the tap on the main big quad but I do a calculation to find out where it as tapped, like this:
if (tappedPosiion > quadWidth / 2)
{
// it's the right side
} else {
// its the left
}
The problem is I don't know how do I find out the width of the current game object!
Renderer.bounds.size should work (or collider.bounds.size)
http://answers.unity3d.com/questions/24012/find-size-of-gameobject.html
You can then access the width by getting the x value:
"size.x is the width, size.y is the height and size.z is the depth of the box."
http://docs.unity3d.com/ScriptReference/Bounds-size.html
Now you have the width, so it should be relatively easy to find out where they tapped.
if(tappedLocation == width/2)
//middle
if(tappedLocation > width/2)
//right side
if(tappedLocation < width/2)
//left
pretty basic but answers your question on how to find width and see where they tapped based on that information.
I've an implementation of a translucent to opaque ActionBar effect (Google Play's App Pages, Google Music) in an app I'm developing, I've made a ScrollView that notifies when the scrolling changes, my listener then calculates a ratio of opacity for the ActionBar relative to some max height. This is working great, but now I've implemented something like this in my scroll change listener.
float ratio = (float) Math.min(Math.max(t, 0), limitHeight) / limitHeight;
toolBarDrawableAlpha = (int) (ratio * 255);
toolBarTypeColorDrawable.setAlpha(toolBarDrawableAlpha);
int dexEntryPictureNewSize = Math
.max(dexEntryPictureMinSize, (int) ((1 - ratio / 2) * dexEntryPictureSize));
dexEntryPicture.getLayoutParams().height = dexEntryPictureNewSize;
dexEntryPicture.getLayoutParams().width = dexEntryPictureNewSize;
dexEntryPicture.requestLayout();
int dexEntryPictureNewBottomMargin = Math.max(dexEntryPictureMarginBottom,
Math.min(dexEntryPictureMaxMarginBottom,
(int) ((ratio * 1.5) * dexEntryPictureMaxMarginBottom)));
((FrameLayout.LayoutParams) dexEntryPicture
.getLayoutParams()).bottomMargin = dexEntryPictureNewBottomMargin;
dexEntryPicture.requestLayout();
int dexEntryNameNewTopPadding = Math.max(dexEntryNamePaddingTop,
Math.min(dexEntryNameMaxPaddingTop, (int) ((ratio) * dexEntryNameMaxPaddingTop)));
dexEntryName.setPadding(dexEntryName.getPaddingLeft(), dexEntryNameNewTopPadding,
dexEntryName.getPaddingRight(), dexEntryName.getPaddingBottom());
As you can see, I use the same ratio calculated for the opacity to change some height, width, margin, padding values in my views, this is working great too, but I've been wondering if there's a way to accomplish this in a more efficient way or if I'm making some horrible performance mistakes, I only have one device to test this and it's running fairly snappy, really smooth but would like to know if lesser devices won't whine about it.
If you are wondering what's the final effect that I'm going for, it's some sort of parallax (?) scrolling where a fixed background view has another centered view in it (like a profile image circle) and this centered view shrink and expands relative to the opacity ratio, also some paddings are moved so the view seems to be pushed to top.
I have an onTouchListener on an ImageView and I use event.getX() or getY().
My goal is to display an image and launch a dialog or something when the user touch a particular part of my image.
The problem is that with different screen, the X et Y values change for the same part of my image view.
How can I get the real position of the event in pixel on every screen ?
For instance I would like to display an Android face, and do something when the user click in his eyes...
Write your code to detect the touch at a specified point using a fixed resolution, e.g. 480x640. Then get the resolution of the device the app is running on using DisplayMetrics. Calculate xScale and yScale (e.g. DisplayMetrics.widthPixels / 480) and multiply your x and y by these scale factors.