如何使 animatorInflater 正确?
how to make animatorInflater correct?
我正在尝试更正 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.access0(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.access0(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
然后总是会出现一个对话框说 "Unfortunely, Activity has stopped"
您正在加载一个 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);
我正在尝试更正 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.access0(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.access0(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
然后总是会出现一个对话框说 "Unfortunely, Activity has stopped"
您正在加载一个 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);