I tried to make my custom interpolator following the android API GUIdes. So this is the interpolator.
<?xml version="1.0" encoding="utf-8"?>
<customInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:factor="2"
/>
But when I try to use it in animation:
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android">
android:fromXScale="1"
android:toXScale="5"
android:duration="3000"
android:interpolator="@anim/custom_interpolator"/>
</scale>
And nothing happens - the view just refresh itself. Why is that ? And why if I try to use is in that way:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="1"
android:toXScale="5"
android:duration="3000"
android:interpolator="@anim/custom_interpolator"/>
</set>
with this code :
AnimationSet animation = (AnimationSet)AnimationUtils.loadAnimation(AnimationActivity.this, R.anim.animation);
animationView.startAnimation(animation);
an exceptions is thrown:
java.lang.RuntimeException: Unknown interpolator name: customInterpolator
at android.view.animation.AnimationUtils.createInterpolatorFromXml(AnimationUtils.java:422)
at android.view.animation.AnimationUtils.loadInterpolator(AnimationUtils.java:285)
at android.view.animation.Animation.setInterpolator(Animation.java:391)
at android.view.animation.Animation.<init>(Animation.java:255)
at android.view.animation.ScaleAnimation.<init>(ScaleAnimation.java:63)
at android.view.animation.AnimationUtils.createAnimationFromXml(AnimationUtils.java:119)
at android.view.animation.AnimationUtils.createAnimationFromXml(AnimationUtils.java:115)
at android.view.animation.AnimationUtils.createAnimationFromXml(AnimationUtils.java:92)
at android.view.animation.AnimationUtils.loadAnimation(AnimationUtils.java:73)
at com.example.someone.studyproject.AnimationActivity$4.onClick(AnimationActivity.java:61)
at android.view.View.performClick(View.java:4633)
at android.view.View$PerformClick.run(View.java:19330)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
It's weird, I never create my custom interpolator with xml (I wanted to do it in xml just to practice it). Thanks in advance.
Now I try :
<scale
android:fromXScale="1"
android:toXScale="5"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"/>
and this doesn't work - the picture disappear and after 5 sec it is back. Why is that ? Why picture disappear instead of resizing?
Have a look at the error it is throwing. "customInterpolator" is not an actual interpolator. You just created that tag from nothing. You have to use the inbuilt android interpolator classes if you want to modify them. For example:
customInterpolator.xml
<accelerateInterpolator
xmlns:android="http://schemas.android.com/apk/res/android"
android:factor="2" />
Have a look at this link for different types of interpolators available in Android.
If you don't want to use one of the existing android interpolators, you can create your own programmatically.
CubicAccelerateDecelerateInterpolator.java
public class CubicAccelerateDecelerateInterpolator implements Interpolator
{
@Override
public float getInterpolation(float t)
{
float x = t * 2.0f;
if (t < 0.5f)
{
return 0.5f * x * x * x;
}
x = (t - 0.5f) * 2 - 1;
return 0.5f * x * x * x + 1;
}
}
As you can see, you need to create a function that returns the value of interpolation between 0 and 1 for time t. i = f(t)
Note: If you do it this way, you cannot reference this interpolator in XML. You have to create your animation programatically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With