我尝试按照Android API指南制作自定义插值器。 这就是插值器。
<?xml version="1.0" encoding="utf-8"?>
<customInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
android:factor="2"
/>
但是,当我尝试在动画中使用它时:
<?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>
而什么也没有发生-视图只是刷新了自己。为什么会这样呢?如果我试图以那种方式使用它,为什么会出现这种情况:
<?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>
使用以下代码:
AnimationSet animation = (AnimationSet)AnimationUtils.loadAnimation(AnimationActivity.this, R.anim.animation);
animationView.startAnimation(animation);
抛出异常:
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)
很奇怪,我从来没有用XML创建过自定义插值器(我想在XML中这样做只是为了练习)。 提前谢谢。
现在我尝试:
<scale
android:fromXScale="1"
android:toXScale="5"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"/>
为什么这样做不起作用——图片会消失,5秒钟后又会出现。为什么会这样?为什么图片会消失而不是调整大小?