There are many answers to how to rotate image in y axis. Also, there are answers to rotate layer in z axis. However, what I want is to put many image side by side in a linear layer (or whatever), and rotate them in Y axis for a specific degree at once. just like the picture attached. In addition, the ImageViews still can be touched to invoke events.
I think this might help you:
ObjectAnimator anim = ObjectAnimator.ofFloat(<yourView>,"rotationY",0f,180f);
anim.setDuration(ANIMATION_TIME);
anim.start();
Or maybe without animation you can use this answer: link
Related
I'm trying to make a small circle move in another bigger circle as a ball moving in circle relative to Android phone tilting. I'm doing this in Godot but I think the principle is the same in all game engines probably. I make a scene in Godot and add two sprites as the two circles as the following picture. I get the accelerometer 3D vector, use x and y values and calculate the angle in x and y plate (relative to y axis i.e Vector2(0, 1) ) and rotate the mentioned scene to that degree using an animation, using this code
func _process(delta: float) -> void:
var vec3 = Input.get_accelerometer()
accelVec = Vector2(-stepify(vec3.x, 0.1), -stepify(vec3.y, 0.1))
var angle = accelVec.angle_to(Vector2(0, 1))
rotateTween.interpolate_property(self, "rotation", rotation, angle, 0.2,
Tween.TRANS_LINEAR)
rotateTween.start()
return
But the problem lies in here that when the x value of accelerometer 3D vector changes from a positive to negative value i.e when the ball is at top of the circle and is going to go to the other half of the circle, it actually moves from bottom of the circle to the desired point rather than from top of the circle.
I hope I explained the problem well, though I doubt it.
I added the Gif that shows actual test on an android phone here Testing in Android.
Any idea how to solve this problem?
Thanks in advance.
This is because Tween is interpolating linear values. It doesn't know it's working with angles, or that they should wrap around.
So when you're at -179 degrees and you tween to 179--a difference of 2 degrees--Tween just sees -179 -> 179 and goes through the whole circle.
You probably don't need a Tween here at all because _process() happens every frame.
I have two ImageViews inside a constraint layout. One on top and one on bottom. I want to move the bottom one onto the top one. Of course I know it doesn't change it's actual position which I am ok with. When I use TranslateAnimation to move to the specified coordinates the image goes down and to the right instead of moving up and to the right. Also when it moves to the right it is slightly off. I can use view.animate().y(getTop).x(getLeft); and it moves the image into the correct position but then it is stuck there. I can't get it to move back to its original like it would if fillAfter was false.
First I get the destination location
float x = playerOneCard.getLeft();
float y = playerOneCard.getTop();
This code moves the card to the correct location but does not reset it after being moved. I need it to essentially move there, go back to it's original place, and repeat as many times as the button is pressed.
playerOneCardMove.animate().x(x).y(y);
This code moves the image but down and towards the right no where close to lining up with the desired destination.
TranslateAnimation translateAnimation = new
TranslateAnimation(Animation.ABSOLUTE,x,Animation.ABSOLUTE,y);
translateAnimation.setDuration(500);
translateAnimation.setFillAfter(false);
playerOneCardMove.startAnimation(translateAnimation);
Figured it out. Since I was going from the absolute position of the first image the top left of that image is 0,0. So it was moving accordingly. Instead I measured the distance from the top of the image on the bottom to the top of the image above it and then moved it according to distance. I did the same for both x and y coordinates.
float distanceY = Math.abs(playerOneCardMove.getTop() - playerOneCard.getTop());
float distanceX = Math.abs(playerOneCard.getLeft() - playerOneCardMove.getLeft());
TranslateAnimation translateAnimation = new TranslateAnimation(Animation.ABSOLUTE,distanceX ,Animation.ABSOLUTE,(distanceY) - (distanceY*2));
translateAnimation.setDuration(1000);
translateAnimation.setFillAfter(false);
playerOneCardMove.startAnimation(translateAnimation);
Hope this helps someone else.
I'm trying to rotate a figure based on the mouse position where a horizontal swipe will rotate it along the y axis and a vertical swipe will rotate it along the z axis. Now i followed the tutorial on android development and was able to create a rotation about the z axis. I now want to combine it with rotation along the y axis. Here is a part of my code:-
Matrix.setRotateM(mRotationMatrix,0,mAngle,0,0,-1.0f);
//i added the following line
Matrix.setRotateM(mRotationMatrix2,0,mAngle2,0,-1.0f,0);
Matrix.multiplyMM(scratch,0,mMVPMatrix,0,mRotationMatrix,0);
//and this line too
//i am rotating the already rotated figure on a different axis this time
Matrix.multiplyMM(temp,0,scratch,0,mRotationMatrix2,0);
mTriangle.draw(temp);
But when i run the above code, I am getting strange results. The code was working right before i added the 2 lines of code and the float matrix variable temp.
You should multiply only once with MVP, and do no transform after, so at the end just before drawing.
Since VP is doing the transformation from space to camera to screen (so further transforms would be awkward, like having a camera looking the screen displaying another camera ;-) ).
I'm trying to make an imageview rotate around it's Y axis, similar to a card flipping, but it should have the same picture on both sides.
To do this, I use an ObjectAnimator in an AnimationSet (though this happens when the Y rotation is the only animation in the set, as well as when it's combined with other animations) to rotate it from 0 to -180 around Y axis. This works fine the first time. However, if I click it again, the image will suddenly get mirrored then rotate 180 degrees around Y axis.
How come the image gets mirrored/reversed before animating? My current idea is that it retains some property, so while it first goes from 0 to -180, next time it will jump from -180 to 0, then rotate from 0 to -180 again. I'm not sure if that is the cause, however if it is, how can I work around this and prevent it from happening?
Thanks!
If i understand you right, this happens because ObjectAnimator doesn't actually applies final value itself. You have to use AnimationListener and onAnimationEnd to save view's state.
I'm currently working on an Android game
I want to create an image flip effect using animation.
How would I do it?
There is a good example of such here
try this:
Android Animation - Flip
or more complex:
http://www.inter-fuser.com/2009/08/android-animations-3d-flip.html
I managed to achieve this using setScaleX (for flip/reflection around the Y axis). Use setScaleY for flip/reflection around the X axis.
final ValueAnimator rotation = ValueAnimator.ofFloat(0, 360);
Then in your onAnimationUpdate function, set scale on X or Y using the cosine function on your range of angles.
view.setScaleX((float)Math.cos(Math.toRadians((Float)valueAnimator.getAnimatedValue())));
Hope this helps