How can I control the animation speed in android - android

I have a music app and the music notations perform an animation from right to left, I want to control the animation speed, while they are performing.Can anyone please help me for that?
My animation layout is:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="500%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="10000"
android:fillAfter="true"
android:repeatCount="infinite"/>
And my code to perform the animation is:
Animation am=AnimationUtils.loadAnimation(this, R.anim.note);
music1.startAnimation(am);

set your android:duration with this help you can control your animation speed
android:duration="300"

change in the duration will control the speed of animation in your file.
android:duration="10000" // here you have to change the time in milliseconds.

you can implement onTouchListener to control your animation speed at the run time. inside touch listener event put coding to change your animation duration.

From official documentation:
ValueAnimator animation = ValueAnimator.ofFloat(0f, 100f);
animation.setDuration(1000);
animation.start()
In your case:
music.setDuration(setValue);

Related

Android animation rotate infinite, without pausing

I can rotate my image infinitely. But my problem is that the image pauses shortly when it reaches 360ยบ and then starts rotating again. It happens the same even when I applied "linear_interpolator".
What I want to do is that the image does not pause at all when it starts the next round. So it has to rotate infinitely with same speed at any degree.
Here is my - code. Thanks
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:interpolator="#android:anim/linear_interpolator"
android:duration="1400"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="360"
android:repeatMode="restart"
android:repeatCount="infinite" />
</set>
How I call it on my code
rotate= AnimationUtils.loadAnimation(context, R.anim.loop_rotate)
binding.imgSecondLayout.startAnimation(rotate)
Thanks for help! :)
Add animation.setRepeatCount(Animation.INFINITE) to your java class where animation is called.
My final code is given here:
Animation animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.loop_rotate);
animation.setInterpolator(new LinearInterpolator());
animation.setRepeatCount(Animation.INFINITE);
animation.setDuration(1400);
youractivity.startAnimation(animation);
This is due to the small delay after the animation completes its duration (1400 ms in ur case). you can remove this delay for smooth animation.
Remove repeatMode attribute and instead add this line :
android:startOffset="0" //Delay in milliseconds before the animation runs
The animation will be smooth without any delays

Android fade out animation using Cosine function

I in my Android Honeycomb application there is a View which has a fade in and fade out animation. Here's most part of the animation code:
<scale
android:fromXScale="1.0"
android:toXScale="1.0"
android:fromYScale="1.0"
android:toYScale="0.0"
android:pivotX="0%"
android:pivotY="0%"
android:duration="500" />
The way it's working right now the animation happens in a linear time but I want to do it according the Cosine function, that is from Cos(0) until Cos(0,5*pi) the fade out case.
Is it possible to do such thing?
Thank you.
You are free to create your own Interpolator. (See the interface description here.) This can then be applied with Animation.setInterpolator(Interpolator). Or, see this discussion for ways of applying custom Interpolators in XML.
If you don't want linear interpolation, you have choice between:
AccelerateDecelerateInterpolator, AccelerateInterpolator,
AnticipateInterpolator, AnticipateOvershootInterpolator,
BounceInterpolator, CycleInterpolator, DecelerateInterpolator,
LinearInterpolator, OvershootInterpolator
http://developer.android.com/reference/android/view/animation/Interpolator.html
So maybe what is the closest to a Cosine is:
AccelerateDecelerateInterpolator
An interpolator where the rate of change starts and ends slowly but
accelerates through the middle.

want a ImageView to come from upside down

i have a window and a imageview in it and i want the imageview to come from upside down animated effect how i do that
ImageView img_sliding=(ImageView)findViewById(R.id.img_sliding);
You have to write your own translate animation. a very good tutorial could be found here..
Here is a litte snipped that you can use and adapt it to your needs:
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="100%"
android:toYDelta="0%"
android:duration="300"
android:zAdjustment="top"
android:fillAfter="true" />
create a new xml file in res/anim and then set the animation to your imageview like this:
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.YOURANIMATION)
and set it to the iamgeView
imageview.setAnimation(anim);
Use a scale animation to scale from -1 to 1. This should have the effect of flipping the imageview

Efficient way of using TranslateAnimation to move something across the screen over and over

I've had trouble finding good documentation about animations in Android, but I managed to get my animation working. What I'm concerned about the way I did it is efficiency/performance.
My goal was to have a cloud image move across the screen over and over and repeat forever. To do this I ended up putting 2 identical ImageViews positioned over each other and defining 2 animations:
cloud_slide_exit.xml:
<translate
android:fromYDelta="0"
android:toXDelta="-100%p"
android:duration="25000"
android:repeatCount="infinite"
android:repeatMode="restart"
/>
cloud_slide_enter.xml:
<translate
android:fromYDelta="0"
android:fromXDelta="100%p"
android:duration="25000"
android:repeatCount="infinite"
android:repeatMode="restart"
/>
I applied the first animation to "image1" and the second to "image2." The first animation moves "image1" from right (on-screen) to left (off-screen) while the second animation moves "image2" (which starts off screen) from right (off-screen) to left (on-screen). The end result when you view both animations is that the image moves across the screen. When it reaches the left end of the screen it will start coming from the right side of the screen. This is the only way I could think of doing this.
Is there a more efficient way to go about doing this animation? I'd like to find a more efficient way because I actually have 3 clouds moving which means that I have 6 ImageViews/TranslateAnimations.
replace your code by following code:
cloud_slide_exit.xml:
<translate
android:fromXDelta="100%p"
android:toXDelta="0%p"
android:duration="25000"
android:repeatCount="infinite"
android:repeatMode="restart"
/>
cloud_slide_enter.xml:
<translate
android:fromXDelta="100%p"
android:toXDelta="0%p"
android:duration="25000"
android:repeatCount="infinite"
android:repeatMode="restart"
/>

Rotate button about X-axis in an Android app

I want to rotate a button about X-axis when clicked and then display a different image so it creates an effect that after button click it's flipping and showing a different image which is at its backside.
I'm using following xml for rotation of button:
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<set android:interpolator="#android:anim/decelerate_interpolator">
<rotate
android:fromDegrees="0"
android:toDegrees="-360"
android:pivotX="25%"
android:pivotY="25%"
android:fromXDelta="0"
android:toXDelta="0"
android:fromYDelta="0"
android:toYDelta="0"
android:duration="400" />
</set>
</set>
But it's rotating the button in 2-D plane about the button's center.
I have a flip example here:
http://www.inter-fuser.com/2009/08/android-animations-3d-flip.html
I'm afraid the conventional graphics and animation APIs are 2D. To use that 3rd dimension you'd need to look into OpenGL, which is non-trivial.
You might be able to fake a depth effect by writing a custom animation that uses setPolyToPoly to warp your initial rect into a trapezoid.
It's been a while since this question is posted, but just for the record - there is a way in newer versions of android, and for back compatibility use http://nineoldandroids.com/

Categories

Resources