I have an ImageView that is rotated using RotateAnimation(). I am writing a test case where I want to test that after a button is clicked this view is rotated. So I thought that I can get the rotation angle before the click and after it to check the difference:
oldRotationDegrees = view.getRotation();
//click that button and wait for 3 seconds
assertTrue(Math.abs(view.getRotation() - oldRotationDegrees) > 0);
However this difference is always 0, because getRotation() always returns 0. I have checked that there is really a rotation visible, so it seems that RotateAnimation() doesn't affect the value returned by getRotation().
So how can I verify that the image has been rotated after the button is pressed?
In case it matters, I am using Robotium for these tests.
Update: I am now testing also with robolectric (unit tests) and the behaviour is (without surprise) the same.
Update 2: This answer https://stackoverflow.com/a/4213493/2160877 explains why this happens.
Related
I've been using this code for many years and it works perfectly:
final int orientation = myActivity.getWindowManager().getDefaultDisplay().getRotation() * 90;
The * 90 part happens to yield rotation in degrees, which is convenient but not essential. The main point is that it returns 0, 1, 2 or 3 and lets you distinguish "normal" portrait / landscape from their upside down variants.
The current suggestion is getConfiguration().orientation, but there is only ORIENTATION_PORTRAIT and ORIENTATION_LANDSCAPE, which is not suitable for my needs - I need to detect the upside down positions as well. How to do this without using getDefaultDisplay().getRotation()?
You can use Context#getDisplay[1], retrieve the nullable Display object and then simply use Display#getRotation to get the roation.
E.g.
orientation = myActivity.getDisplay().getRotation()*90 (don't forget the null checks)
I hope this helps, out of curiosity, what is your use case where you need to destingush between these 4 cases?
I have two ImageView(s) set in my layout one on top of the other, say A and B.
I have set the alpha of B to 0 so it's totally faded as I want to control which image is displayed by setting the appropriate alpha to 1 in my code.
Now I also want to rotate the Images by 360 degrees before they fade out and before the new image comes on the screen when I click on the ImageView(s).
I'm using the ViewPropertyAnimator's rotation() and alpha() methods to do this.
This works without any issues when the App is installed and runs for for 1st time.
However, next time when I click on the Images, only the fade-in and fade-out animations work.
The rotation does not work.
Check this code :
boolean isImage1Showing = true;
//onClick() method for the 2 ImageView(s)
public void fadeAndRotate(View view){
if (isImage1Showing){
//Rotate the resource on 1st ImageView by 360 degrees
imageViewA.animate().rotation(360f).setDuration(2000);
imageViewA.animate().alpha(0).setDuration(2000);
//Displaying the other image on to the screen
imageViewB.animate().alpha(1).setDuration(2000);
isImage1Showing = false;
} else {
isImage1Showing = true;
imageViewB.animate().rotation(360f).setDuration(2000);
//Displaying the previous image
imageViewA.animate().alpha(1).setDuration(2000);
//fading out the currently displayed image
imageViewB.animate().alpha(0).setDuration(2000);
}
}
Use the
.rotationBy(float value)
method instead of the
.rotation(float value)
method. In your case it rotates TO 360f but you want it to rotate BY 360f.
So this code should work :
imageViewA.animate().rotationBy(360f).setDuration(2000);
I instantiate the following gameObject, which contains an Animator with the mode "always animate" on, the animation goes for 340ms, after that time I destroy the gameObject.
The gameObject Inspector properties:
I instantiate it using the following code:
instancia = (Instantiate(cardAnimation, new Vector3(0, 0, 0), Quaternion.identity) as GameObject).GetComponent<Image>();
instancia.rectTransform.SetParent(transform);
StartCoroutine(KillOnAnimationEnd());
Here is the Coroutine:
private IEnumerator KillOnAnimationEnd()
{
yield return new WaitForSeconds(0.34f);
DestroyImmediate(instancia);
}
Here is how the animation looks like when simulating in Unity (PC-Windows):
But on android after I open the chest it waits 340ms with nothing happening and then show the information above, does this have something to do with the plataform or is some unity or perhaps code related issue?
NOTE: I also have another animation in another scene that is just a already instantiated gameObject in the Hierarchy with always animated on and it works on Android.
--EDIT--
So I have ran the newest version of the app in a emulator which is almost about 1080x480 and the animation showed just as the PC, also running on a 720p smartphone did the job, the only problem I'm still having is with my QuadHD resolution from Galaxy S6, everything else shows but the animation, I have even tried making the animation run without any script so it runs in a loop, but it doesn't show up in galaxy screen.
Given the news about the issue I think this might change a little bit the perspective of answers and perhaps help someone else solve the same problem in the future.
Okay, figured out the problem, its something to do with "rotation" in animations using Unity3D in 2D mode, gonna be reporting it form Unity so it is fixed.
The solution: Animate your UI only using scale/position, if used rotation it will not show on high resolution display.
I am pretty sure your WaitForSeconds(0.34f) is not working properly because there is no thing such as yield keyword in Java. I recommend you to use a invoke method instead to call your method that destroys your GameObject.
I am trying to do an animation whenever my character jumps. It works in the following way: when I jump, I first draw the animation of my char. If it is finished, I start modifying his position and also draw another texture, for the inAir moment. The problem is that the animation runs instantly (I tried putting the speed 1 frame per second,but it still runs instantly) and the character jumps before the animation is done. I suspect it considers it finished before it actually finishes. This is how I check if the animation is finished: first I check if the animation start (the jump button is pressed) and then I have an if condition like this:
if (animationStarted && rightJumpAnim.isAnimationFinished(System.currentTimeMillis()))
{
animationStarted = false;
animationFinished = true;
timePassed1 = 0;
}
After this, I put animationFinished to false when he touches the ground again. Any advice?
P.S. here is the code for running the jumpAnimation:
if (toDrawJumpRight1 == true) {
animationStarted = true;
timePassed1 += Gdx.graphics.getDeltaTime();
spriteBatch.draw(rightJumpAnim.getKeyFrame(timePassed1, false), posX + cameraX, posY + cameraY, sizeX, sizeY);
}
Your problem should be in the very first line you provided to us:
if (animationStarted && rightJumpAnim.isAnimationFinished(System.currentTimeMillis()))
The argument you must pass is the STATETIME of the animation.
if (animationStarted && rightJumpAnim.isAnimationFinished(timePassed1))
Edit:
Some more Informations to this problem:
You say you want 1 frame stay 1 Second.
Now we say, you have approx 5 frames.
5frames * 1 second = 5seconds for finish
so if statetime is over 5 seconds, your animation is played ONE time (is finished)
Animation.isAnimationFinished(float stateTime) checks now, if the current stateTime from your animation is higher than the 5 seconds.
System.currentTimeMillis is at this moment:
1440618906341
now.. whats bigger?
Have a nice Evening (I recommend using TimeUtils.millis instead of System.currentTimeMillis, guess it's optimated, fo' sure.)
I have made 2 different animations in Adobe flash professional cs5.5 for an Android aplication.
And I want a code that makes it possible for a user of the app to play the animation as often they want, so if the user wants the animation to play 1 time the first animation will be playes, if the user wants to play it 2 times the animation 1 and 2 will be played, if the user wants the animation 3 times played the animation 1, 2 and 1 will be played and so on.
Can somebody help me with this problem and tell me if this is possible in jquery.
If I was you, the way I would approach this is by having a keyframe after each of the animations where you can type some code.
On the menu page or where ever you have the code for how many times the code should run, define a variable and call it something like "runTimes" which should become the amount of times the animation should run.
At the end of the animations do a simple if statement to check what the value of "runTimes" is and then decrement it. Depending on the value, it should use gotoAndPlay/gotoAndStop.
So, put this on the keyframe after the first animation:
if (runTimes > 0) {
runTimes--;
gotoAndPlay(<FIRST FRAME OF SECOND ANIMATION>);
} else {
gotoAndStop(<FRAME OF MAIN MENU>);
}
and this after the second animation:
if (runTimes > 0) {
runTimes--;
gotoAndPlay(<FIRST FRAME OF FIRST ANIMATION>);
} else {
gotoAndStop(<FRAME OF MAIN MENU>);
}
On the mainmenu frame, let's assume you have a textbox named "numTimes_txt" for the number of times to play and a button "playAnimations_btn" to start the animations.
import flash.events.MouseEvent;
var runTimes:int = 0;
playAnimations_btn.addEventListener(MouseEvent.CLICK, playAnims);
function playAnims(e:MouseEvent):void {
runTimes = parseInt(numTimes_txt.text);
play(); // or gotoAndPlay(<FIRST FRAME OF FIRST ANIMATION>);
}
I haven't tested this as I don't have my IDE on me right now, but this should work if I understand the question properly.