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

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.

Related

How to make setOnClickListener on a dynamic imageview

Working in Android Studio, I need to add in my app a click listener on my imageview which has a certain animation. This is the code I have in MainActivity:
myImage.startAnimation(myAnimation);
myImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//DO SOMETHING
}
});
myAnimation comes from an XML in anim folder which does a translational animation:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:interpolator="#android:anim/accelerate_interpolator"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:fromYDelta="0%p"
android:toYDelta="40%p"
android:duration="1000"/>
</set>
This works fine, although this click listener is set on the space that has the image on the layout (activity_main.xml) and does not follow the animation inserted in the image. If I click on the space which belongs to the image in the layout, the click listener starts even if the image is not there due to the animation.
Is there a way in which the click listener is attach to the imageview in motion?
Thank you
This happens because the xml Translate animation you are using, does not really animate the View's property, it just moves the pixels on the screen and so it just looks like it has changed position, that is why the OnClickListener is still active on the View's original position, because Android thinks the View never really moved.
Simple solution use ObjectAnimator or even better ViewPropertyAnimator. Both will animate the View's property and so the OnClicklistener will also change it's position with it.

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.

Translate animation works perfectly when defining with XML and only once perfectly by code - Android

I'm getting this weird issue. Basically I'm animating a view with translate animation. (Translate into the screen and out via 2 different events) My code for translate animation is:
final Animation animtopOut = new TranslateAnimation(0, 0, 0, -mainHeaderlayout.getMeasuredHeight());
animtopOut.setDuration(500);
animtopOut.setFillAfter(true);
mainHeaderlayout.setAnimation(animtopOut);
And the xml code is:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="#android:anim/accelerate_interpolator" >
<translate
android:fromYDelta="0%p"
android:toYDelta="-99%p"
android:duration="600"
android:fillAfter="true">
</translate>
</set>
Setting it using the code:
final Animation animtopOut = AnimationUtils.loadAnimation(mContext, R.anim.header_animate_out);
When I trigger the animation it works fine if I use the xml animation properties. The problem is when i use it via code. Which is what I want. It runs with translate animation only for the first time. The second time, when it is triggered, the view is inside the screen without animation. Please some one help me if I'm missing any properties. Thanks.
EDIT : (extra info)
There are actually two different animations that are triggered on the same view via two different events. I have actually posted one animation property. The other is almost the same. with just values are different.
Have you tried animation configuration like this
animtopOut.setRepeatCount(Animation.INFINITE);
animtopOut.setRepeatMode(Animation.RESTART);
animtopOut.setInterpolator(new LinearInterpolator());
?

Why Android animation is not freeze but return to start

I search but cant find out any infromation how to freeze last state of animation like rotation or else.
I think than animation it's change some pictures with show's last state.
I have next xml animation file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true">
<rotate android:fromDegrees="0" android:toDegrees="300"
android:duration="5000" android:pivotX="50%" android:pivotY="50%"
android:startOffset="10"
>
</rotate>
</set>
As you can see parameter toDegress it's setted to 300.
I use this animation xml to rotate text with next code
Animation animation1 = AnimationUtils.loadAnimation(this,
R.anim.myanimation);
animation1.setAnimationListener(this);
View animatedView1 = findViewById(R.id.rotatetext);
animatedView1.startAnimation(animation1);
But then animation stops text not rotated state no 300 degrees, it's returns to 0 degress.
How i can do what animation freeze on last frame?
Keep It Simple Stupid
Read,read and read documentation
When set to true, the animation transformation is applied after the
animation is over. The default value is false. If fillEnabled is not
set to true and the animation is not set on a View, fillAfter is
assumed to be true.
Must be a boolean value, either "true" or "false".
You can implement an AnimationListener and set to your animation object. write some code to change the View's state in the method onAnimationEnd().

Android animation sequence

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

Categories

Resources