i wonder how to get current angle / degrees in rotate animation when animation ends or gets interrupted?
my idea is when the animation gets interrupted when some button is clicked, the rotation will go counter-clockwise (current flow is clockwise), start from last angle /degree right when it gets interrupted.
i've tried using applyTransformation, but it seems not called :(
thanks!
For this you must have to rotate your object not with simple rotate animation.
But Need to rotate with ObjectAnimator.
Here is simple example to find rotation angle of ball(imageview) .
ImageView ball;
int i = ball.getMeasuredHeight();
ObjectAnimator animation = ObjectAnimator.ofFloat(ball, "rotation", 0, 360);
animation.setDuration(10000);
ball.setPivotX(0);
ball.setPivotY(i / 2);
animation.setRepeatCount(ObjectAnimator.INFINITE);
animation.setInterpolator(new LinearInterpolator());
animation.start();
To get current rotation angle use:
ball.getRotation();
Related
My application is used in landscape orientation. What I want to do is move an element from off the screen at all until it enters the screen and then when pressing a button, it leaves the screen again by scrolling to the left.
The animation starts from the default position of the view (i.e the position you have specified in your xml file). if you do not what the animation to start from that position, you can either:
change the initial position of the view in the xml file.
or set the x and y coordinates of the view at run time and then start your animation like so:
ObjectAnimator translateX = ObjectAnimator.ofFloat(view, "translationX", deltaX);
All you need is a TranslationX animation, see below, or check out this link. https://developer.android.com/training/animation/reposition-view
ObjectAnimator translateX = ObjectAnimator.ofFloat(view, "translationX", deltaX);
ObjectAnimator translateY = ObjectAnimator.ofFloat(view, "translationY", deltaY);
If you have any question, please feel free.:)
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 trying to create a mirror reflection of a view, for that I tired the Y axis ObjectAnimator animation using the following code:
ObjectAnimator animation = ObjectAnimator.ofFloat(view, "rotationY", 0.0f, 360f);
animation.setDuration(3600);
animation.setRepeatCount(ObjectAnimator.INFINITE);
animation.setInterpolator(new AccelerateDecelerateInterpolator());
animation.start();
This works perfectly but I want the animation to stop after reaching the desired end position rather than returning back to the original place.
After some research I found that setting the setFillAfter(boolean) method of Animation object to true does that thing.
Now my problem is that i am unable to convert the ObjectAnimator animation to a standard Animation code. to get access to that method.
I realized i could use RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY) to rotate my view and also keep it at the ending place.
But I couldnt figure out what should be the pivotx and pivoty values so that the view stays at the same location and flips around.
Usually an ObjectAnimator stays at the desired location when the animation ends. Im guessing the line
animation.setRepeatCount(ObjectAnimator.INFINITE);
is what is making your animation start over and over in an infinite loop, remove this line and you should be fine...
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.