I have a custom built compass that is used as part of a navigation app that I have written. I have also written an algorithm that that smooths out the compass (a type of low-pass filter). Everything works great and the compass is fluid. I pass in a current degrees and previous degrees arguments to the RotateAnimation. Problem is when you spin around quickly with the compass, after a certain point, the gap is so large between the current and previous degrees that the rotate switches direction to the path of least distance. It has to do with the sort of lag that happens from the low-pass filter - that it has to catch up wit the real-time direction. I was wondering if anyone has dealt with this (I have seen some compass apps from the store that handle this) or has some kind of algorithm/solution, to keep the orientation of the rotation. Here is some code:
RotateAnimation rotate = new RotateAnimation(startingPointer, pointerDeg, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setInterpolator(new LinearInterpolator());
rotate.setDuration(ROTATION_INTERVAL);
rotate.setFillAfter(true);
pointer.setAnimation(rotate);
startingPointer = pointerDeg;
Related
I am making a Rollette game and I want to play sound in each crossing section like if 1 is crossing it should find some trigger or collision detection and ping one tick sound. My problem is that I am not able to find the collision detection on image. Below is the approaches that I have done.
I have taken LayoutView and placed a Rollette wheel image inside it.
In each section (0-9) has taken a green small button which will be used to detect the collision with the arrow. Once it collides there will be a Tick sound with up-down animation in arrow. (Image attached).
Problem.
I am not able to find the new co-ordinate of views in each rotation. It is returning the same static location every time and hence collision is never happening.
Rotation Code..
final RotateAnimation rotate = new RotateAnimation(0, 360f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(10000);
rotate.setFillAfter(true);
Collision detection code...
Rect arrowBtnRect = new Rect();
arrowBtn.getGlobalVisibleRect(arrowBtnRect);
Rect btn0Rect = new Rect();
btn0.getGlobalVisibleRect(btn0Rect);
if(arrowBtnRect.intersect(btn0Rect)) {
System.out.println("Collision detected "+numberSelected);
numberSelected = "0";
return true;
}
Your idea is brilliant!
But I have certain remarks on your approach:
setFillAfter() is not helping, according to documentations:
If fillAfter is true, the transformation that this animation performed will persist when it is finished.
I recommend using ObjectAnimator instead of RotateAnimation. While RotateAnimation is just making the view looks as if it is rotating until you call fillAfter (which happens after animation stops), ObjectAnimator makes the view really change its actual angle while rotating. See this: https://stackoverflow.com/a/29465710/10005752
Usage:
Random random = new Random();
float randomAngle = (float) (random.nextInt(360) + 1);
ObjectAnimator rouletteAnimator = ObjectAnimator.ofFloat(rouletteView, View.ROTATION, 0f, randomAngle);
rouletteAnimator.setDuration(10000);
rouletteAnimator.start();
Also, to elaborate more on my comment I recommend that get all angles that have buttons on them before starting animating, and when animation starts keep using rouletteView.getRotation(); to check what angle is at the top.
Example:
Button1 is at angle 90 and rouletteView is rotating Counter Clockwise. Then if rouletteView.getRotation() == 360 - 90 is true then Button1 is touching the arrow.
I hope I'm clear!
I am about to leave a project and wanted to add a little Easter egg to the app. I was just wondering, is it possible to rotate the whole screen 360 degrees on a button click say? Or is this too hard?
I was potentially thinking you could take a screenshot, rotate that screenshot 360 degrees, and then delete it. But I'm not sure about the security implications of taking a screenshot the users phone
You could specify an id in your root layout (e.g #+id/main) and use a classic animation like this
final RotateAnimation rotateAnimation = new RotateAnimation(0.0f, 360.0f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(500);
findViewById(R.id.main).startAnimation(rotateAnimation);
I have a problem and I just can't manage to find a solution that works.
So, here is my problem. I have to make an ImageView to rotate itself, to 90 degree on orientation change.
I did that, and the image is actually rotating pretty awesome, but when the animation finishes, the image reset itself to previous position.
Here is the code I used to rotate the image:
Matrix matrix = mImageView.getImageMatrix();
RectF dst = new RectF();
matrix.mapRect(dst, new RectF(mImageView.getDrawable().getBounds()));
mAnimation = new RotateAnimation(0.0f, -90.0f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
mAnimation.setDuration(5000);
mImageView.startAnimation(mAnimation);
mImageView.setImageMatrix(matrix);
mCurrentOrientation = 1;
you can use this to animation persist after it has been done:
mAnimation.setFillEnabled(true);
mAnimation.setFillAfter(true);
One thing you can do is set the imageview to an already rotated version of the image and then rotate it from -90 to 0 degrees. That will work with the API you are already using. If you make the two calls, one right after it will work correctly without stutter.
The other option is to use a later API version. The Object Animator animations do not return the view to it's original position. More on this here.
How to rotate a drawable by ObjectAnimator?
The code is simple, but this doesn't work with gingerbread.
I'm making a compass that points to a user defined location. I'm using the rotateanimation to rotate the needle. When the needle points in the direction of the phone I know the phone is pointing in the direction I want. However, I wanted the needle to point in the correct direction irregardless of the phone's azimuth.
The problem is that it seems that rotateanimation does not rotate the needle according to the real world coordinates, and instead is relative to the phone's screen. So a 58 degree rotation of the needle does not match a 58 degree rotation in the real world. Is this true or am I making a mistake in my code?
The compass is meant to be used by placing the phone's back flat on a surface. I've also tried outputting the azimuth and it reads like this:
Azimuth Actual Phone angle
0 0
45 90
90 180
when it gets close to a full circle back it bounces between 120 and 340.
Here's the code:
direction = 360 - azimuth + rotate;
RotateAnimation animate = new RotateAnimation(rotateLast, direction, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animate.setFillAfter(true);
animate.setInterpolator(new LinearInterpolator());
animate.setDuration(10);
needle.startAnimation(animate);
rotateLast = direction;
azimuth is the phone's azimuth from the sensor, rotate is the user specified direction (in degrees from north), and direction is the required rotation of the needle.
rotateLast is the last position the needle was at, I'm using this because without it the needle reverts to zero degrees and flickers.
Thanks,
P.S. this has been driving me crazy
I figured it out, my math was all wrong and I misunderstood how azimuth affected the rotation.
I realized when I rotated the image to just azimuth and understood that it resulted in the needle pointing north. All I needed to do was just add the user direction. The math I was using caused the needle to rotate in unpredictable ways.
The answer is simply:
RotateAnimation animate = new RotateAnimation(-azimuth+rotation, -azimuth+rotation, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
the -azimuth is because of how rotateanimation rotates counter-clockwise.
In my application, I am rotating a clock hand to the desired amount of minutes. This works good with RotateAnimation. Now what I want to do is to return the hand back to the starting position (0 minutes).
For example:
I first rotate the hand like this:
final RotateAnimation anim = new RotateAnimation(0f, ammountDegress, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 1f);
anim.setFillAfter(true);
anim.setFillEnabled(true)
Then I would like to move the hand back to it's starting position, so that means I should be calling a RotateAnimation after the first animation ends.
The problem is that the pivots have changed, how I can I set the pivot position to the exact position as previous? So that the rotating point is again at the same position of the clock hand.
Edit To make it more clearer - I would like to have something like a fixed point pivot that doesn't change with the rotation, so I am always rotating the hand around the same point.
I hope you can understand what I mean.