如何使animatorInflater正确?

时间:2015-03-27 13:57:28

标签: android android-animation

我试图纠正AnimatorInflater,但它无法正常工作,我无法找到错误。

MainActivity.java:

package com.example.administrator.propertyanimation;

import android.animation.AnimatorInflater;
import android.animation.ObjectAnimator;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

import android.widget.Button;
import android.util.*;


public class MainActivity extends ActionBarActivity {

    private Button mbttn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mbttn = (Button) findViewById(R.id.btn);


        ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.propertyanimation);
                anim.setTarget(mbttn);
        anim.start();

        return ;
    }
}

propertyanimation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
    android:propertyName="rotation"
    android:valueFrom="0"
    android:valueTo="360"
    android:duration="3000"
    android:repeatCount="1"
    android:repeatMode="reverse"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"/>
</set>

这是logcat输出:

03-27 10:27:05.422    3825-3825/com.example.administrator.propertyanimation E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.administrator.propertyanimation, PID: 3825
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.administrator.propertyanimation/com.example.administrator.propertyanimation.MainActivity}: java.lang.ClassCastException: android.animation.AnimatorSet cannot be cast to android.animation.ObjectAnimator
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassCastException: android.animation.AnimatorSet cannot be cast to android.animation.ObjectAnimator
            at com.example.administrator.propertyanimation.MainActivity.onCreate(MainActivity.java:25)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            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:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
03-27 10:27:05.434      584-596/system_process W/ActivityManager﹕ Force finishing activity 

然后会出现一个对话框,表示&#34;不幸的是,活动已经停止&#34;

1 个答案:

答案 0 :(得分:4)

您正在加载AnimatorSet,然后尝试将其分配给ObjectAnimator,这会为您提供在logcat中看到的异常。

从propertyanimation.xml

中删除set标记
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="rotation"
    android:valueFrom="0"
    android:valueTo="360"
    android:duration="3000"
    android:repeatCount="1"
    android:repeatMode="reverse"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"/>

或者,如果您想保留该集,因为以后要添加它,可以更改代码以加载AnimatorSet,而不是像这样:

AnimatorSet anim = (AnimatorSet) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.propertyanimation);