Android custom interpolator with xml

I tried making my custom interpolator after Android APIs. So this is an 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 is simply updated. Why is this? And why, if I try to use this, then:

 <?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> 

using this code:

 AnimationSet animation = (AnimationSet)AnimationUtils.loadAnimation(AnimationActivity.this, R.anim.animation); animationView.startAnimation(animation); 

exceptions are 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) 

This is strange, I never create my custom interpolator using xml (I wanted to do this in xml just to practice it). Thanks in advance.

Now I am trying:

 <scale android:fromXScale="1" android:toXScale="5" android:pivotX="50%" android:pivotY="50%" android:duration="3000"/> 

and it doesn’t work - the image disappears and returns after 5 seconds. Why is this? Why does the image disappear instead of resizing?

+6
source share
1 answer

Look at the mistake she throws. "customInterpolator" is not an actual interpolator. You just created this tag from nothing. You must use the built-in android interpolator classes if you want to change them. For instance:

customInterpolator.xml

 <accelerateInterpolator xmlns:android="http://schemas.android.com/apk/res/android" android:factor="2" /> 

Check out this link for the different types of interpolators available on Android.

If you do not want to use one of the existing interpolators for Android, you can create your own software tools.

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 interpolation value between 0 and 1 for time t. i = f (t)

Note. If you do this like this, you cannot reference this interpolator in XML. You must create your animation programmatically.

+3
source

Source: https://habr.com/ru/post/983938/


All Articles