Android animation sequence - android

I am facing problem in getting a sequence of animation on a particular view.
I used animationset in my code and i have set the offset for each animation and the duration for the animation correctly.
somebody pls help with this.
Thanks...

To play back animations sequentially, just use the set the android:ordering property of the <set> tag to have the value "sequentially".
Then, all set items will be animated in their sequence of declaration.

For page transition use the below snippet
pageTransition(Context context, View view){
Animation mAnim = AnimationUtils.loadAnimation(context, R.anim.slide_top_to_bottom);
mAnim.setRepeatMode(Animation.ABSOLUTE);
view.startAnimation(mAnim);
}
refer the below link for more details deVogella

Related

Android animation only works once?

So i'm trying to do 2 animations simultaneously, one to move a textview, and one to show a linearlayout (also 2 animations to hide them). I have another animation working as intended to show/hide a seperate layout. When i execute showing the view with 2 animations, it works once, it hides fine, but then doesn't work again. Then when i show the other view, it plays all 3 animations (not intended). I can't figure out why this is happening? When i try to show the 2 animations it does nothing, but then when i try the other show view its like it was added to a queue and it shows all 3.
My code for initiating the two animations:
LinearLayout layoutMsgs = (LinearLayout)findViewById(R.id.layoutMsgs);
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.msgs_show);
anim.setAnimationListener(new AnimListener(layoutMsgs, View.VISIBLE)); // sets visibility on animation end
layoutMsgs.startAnimation(anim);
TextView tvMsgs = (TextView)findViewById(R.id.tvMsgs);
Animation tvAnim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.tvmsgs_show);
tvMsgs.startAnimation(tvAnim);
My code for hiding the two animations:
LinearLayout layoutMsgs = (LinearLayout)findViewById(R.id.layoutMsgs);
Animation animLayout = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.msgs_hide);
animLayout.setAnimationListener(new AnimListener(layoutMsgs, View.INVISIBLE));
layoutMsgs.startAnimation(animLayout);
TextView tvMsgs = (TextView)findViewById(R.id.tvMsgs);
Animation animMsgs = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.tvmsgs_hide);
tvMsgs.startAnimation(animMsgs);
Then this is the other animation that is working fine, it's only one animation, no textView, just a layout
LinearLayout pokeLayout = (LinearLayout)findViewById(R.id.layoutPokes);
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.poke_show);
anim.setAnimationListener(new AnimListener(pokeLayout, View.VISIBLE));
pokeLayout.startAnimation(anim);
So how can i fix this? Sorry if my explanation is bad i'm finding it difficult to explain all the details, please ask for any missing information.
solved by using solution in this post Android: Using ObjectAnimator to translate a View with fractional values of the View's dimension
wasn't pretty as I had to create seperate translate X and translate Y res files for each view I need to move.. ended up with like 15 anim files. Would still like to know if theres a better solution

How to start Android animation from the values where it was stopped?

I have an image view on which I apply a rotate animation. The animation works fine. On touch down, I attempt to stop the rotate animation using cancel(), resetting the animation using reset() and clearing the animation on the view using clearAnimation(). But the animation comes back to the original position. How to stop animation with the values it was when touch down event happens and restart from where it was stopped?
My animation is defined in the xml as below
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="360"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="true"
android:fillEnabled="true"
android:duration="200000"
android:repeatMode="restart"
android:repeatCount="infinite"
android:interpolator="#android:anim/linear_interpolator"/>
I am attempting to stop the animation using the following code
private void stopAnimation(){
mRotateAntiClockwiseAnimation.cancel();
mRotateAntiClockwiseAnimation.reset();
imageView.clearAnimation();
mRotateAntiClockwiseAnimator.end();
mRotateAntiClockwiseAnimator.cancel();
stopAnimationForImageButton();
}
I am setting the animation on my view using the following code
mRotateAntiClockwiseAnimation = AnimationUtils.loadAnimation(context, R.anim.rotate_anticlockwise);
mRotateAntiClockwiseAnimation.setFillEnabled(true);
mRotateAntiClockwiseAnimation.setFillAfter(true);
imageView.setAnimation(mRotateAntiClockwiseAnimation);
mRotateAntiClockwiseAnimation.startNow();
imageView.invalidate();
As u see, even using cancel() or reset() did not help to stop the animation at the point where it was touched down. Any pointers would help
I think I got it working by starting the animator and then setting the currentPlayTime(). The documentation clearly tells (which I just stumbled upon) that if the animation has not been started, the currentPlayTime set using this method will not advance the forward!
Sets the position of the animation to the specified point in time. This time should be between 0 and the total duration of the animation, including any repetition. If the animation has not yet been started, then it will not advance forward after it is set to this time; it will simply set the time to this value and perform any appropriate actions based on that time. If the animation is already running, then setCurrentPlayTime() will set the current playing time to this value and continue playing from that point. http://developer.android.com/reference/android/animation/ValueAnimator.html#setCurrentPlayTime(long)
private void stopAnimation(){
mCurrentPlayTime = mRotateAntiClockwiseAnimator.getCurrentPlayTime();
mRotateAntiClockwiseAnimator.cancel();
}
private void startAnimation() {
mRotateAntiClockwiseAnimator.start();
mRotateAntiClockwiseAnimator.setCurrentPlayTime(mCurrentPlayTime);
}
The pause feature has been added in API level 19. Here you can read how implement it. This method use ObjectAnimator which is not much more complicated than usual Animation class that you use. However, there is another alternative trick that can be useful.
With best regards.

Animation on changing ImageBitmap of ImageButton

Let's say I have an ImageButton btn. I have two different Bitmap, bmp1 and bmp2. By default, btn displays bmp1, like this:
btn.setImageBitmap(bmp1);
Can I somehow make an animation, which crossfades bmp1 and bmp2, when calling the following:
btn.setImageBitmap(bmp2);
So the question is, is it possible - if yes, how - to animate the changing of bitmaps on ImageButtons?
The way I see it, there are two main ways to implement this functionality. Note that there may be other methods that will make this happen exactly as you are describing, but these would be my approaches.
First Approach: Leveraging the onAnimationEnd() Callback
Here, you would want to essentially fade out the first ImageButton, change the resource in the onAnimationEnd() callback, and then fade it back in. To do this, you would have to implement the below code.
Create an Animation resource in XML that would be used to fade the View in:
<!-- Filename: fadein.xml -->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Note that you might want to change the duration here-->
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="250"/>
</set>
Create an Animation resource in XML that would be used to fade the View out:
<!-- Filename: fadeout.xml -->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Note that you might want to change the duration here-->
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="250"/>
</set>
Load the fade out and fade in Animations in your code:
// Obtain a reference to the Activity Context
Context context = getContext();
// Create the Animation objects.
Animation outAnimation = AnimationUtils.loadAnimation(context, R.anim.fadeout);
Animation inAnimation = AnimationUtils.loadAnimation(context, R.anim.fadein);
Assign a new AnimationListener to this Animation, and build it out as follows:
outAnimation.setAnimationListener(new AnimationListener(){
// Other callback methods omitted for clarity.
public void onAnimationEnd(Animation animation){
// Modify the resource of the ImageButton
yourImageButton.setImageResource(R.drawable.[your_second_image]);
// Create the new Animation to apply to the ImageButton.
yourImageButton.startAnimation(inAnimation);
}
}
Assign the Animation to the ImageButton, and start the first Animation:
yourImageButton.startAnimation(outAnimation);
Then, if you wish to animate back to the previous image, you would simply do the same but in the reverse order.
Second Approach: Overlay a Second ImageButton
For this approach, you would simply assign two ImageButtons to the same coordinates on the screen, and with the same width. Then, you would fade out the first ImageButton and fade in the second one after the first Animation ended (much like the example above).
I hope that this answer aligns with your expectations. My apologies if there are any code errors, as I am doing this without an editor at the moment. Please let me know if you would like any additional clarification!
It is not possible to animate image button source changes. The workaround would be to stack two ImageButtons and animate its alfa channel separately to get expected behavior.

Apply Two Animations to the Same View?

I applied an entry animation to one of my ImageButton (fade_in.xml which is in the project anim/ folder). Now, after a button click, I want to apply an exit animation (fade_out.xml which is in the same folder)
When I do that, the entry animation happens. However, the exit one do NOT !! It seems that each View will accept ONLY one animation.
Is this true? How can I work around this problem?
-
-
UPDATE:
This is in the onCreate() method for setting the entry animation:
Animation fade = AnimationUtils.loadAnimation(this, R.anim.fade_in);
fade.setStartOffset(600);
img.startAnimation(fade);
img.setvisibility(View.VISIBLE);
And this is in the onClick() method for some button b1:
Animation fade2 = AnimationUtils.loadAnimation(this, R.anim.fade_out);
fade.setStartOffset(500);
img.startAnimation(fade2);
img.setvisibility(View.INVISIBLE);
You can use ViewFlipper with getInAnimation and getOutAnimation methods.
Other solution is setting animation in your code(as far I understand you set animation in xml file).

Reanimating a view when using layoutanimation

Using a layoutanimation on one of my views.
android:layoutAnimation="#anim/animate_layout"
The animation is done each time one enters that view. It will however not run if that view was already active and the user changed to another application and then returned to it. In order to do that, I need to overwrite the onresume() method and call it from there.
How can I call the layoutAnimation from within the code in order to run it again for the whole layout?
Another option, besides what Pentium10 suggested, would be trying to do this:
Animation animation = AnimationUtils.loadAnimation(ctx, android.R.anim.fade_out);
target.startAnimation(animation);
Where ctx could be something like YourActivity.this, and target is the View you want to animate.
Look into LayoutAnimationController and start()
I'm also trying to get my animation to start over when the user gets to se the layout again. I get force close with this code implemented.
public void onResume(){
super.onResume();
View layout = findViewById(R.layout.main);
Animation animation = AnimationUtils.loadAnimation(CrookTranslate.this, R.anim.fade_from_left);
layout.startAnimation(animation);
}
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:animation="#anim/from_left"
android:id="#+id/fade_from_left"
android:delay="3.0"
/>
enter code here

Categories

Resources