我是Android的新手。我需要在活动开启时在我的应用中自定义动画。
我在我的应用styles.xml
<style name="YourAnimation.Activity" parent="@android:style/Animation.Activity">
<item name="android:windowEnterAnimation">@anim/fade_in</item>
<item name="android:windowExitAnimation">@anim/fade_out</item>
</style>
然后将样式应用于同一文件中的主题。
<style name="YourTheme" parent="android:Theme.Translucent">
<item name="android:windowAnimationStyle">@style/CustomAnimationActivity</item>
</style>
然后在我的AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/YourTheme" >
</application>
但是,当我正在运行时,会发生以下错误。
我想我需要在项目中的某处添加动画ml文件。但是,不知道该怎么做。有人请帮帮我。
提前致谢。 :)
-edit -
以下是fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="1.0"/>
</set>
以下是fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="0.0"/>
</set>
崩溃日志
05-20 15:36:47.216 3557-3557/com.myayubo E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.myayubo, PID: 3557
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myayubo/com.myayubo.PreSplash}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
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:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:122)
at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
at com.myayubo.PreSplash.onCreate(PreSplash.java:23)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5299)
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:829)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:7)
在Android Studio中:
然后,您可以像CustomAnimationActivity
一样为styles
创建/添加项目。
编辑以下崩溃日志
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.`
只需更改styles.xml
以扩展AppCompat
主题,例如
<style name="YourTheme" parent="android:Theme.AppCompat.Light">
<item name="android:windowAnimationStyle">@style/CustomAnimationActivity</item>
</style>
此外,您的Activity
应该延伸AppCompatActivity
(或Activity
)。
答案 1 :(得分:2)
尝试在动画文件夹中添加以下xml文件
<强> fadein.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:zAdjustment="top"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="700" />
<强> fadeout.xml 强>
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
执行此操作后,只需在您的启动活动中添加以下代码即可 (注意。在完成您的意图后放置此代码)
this.http.post(url, emplyee, options)
.subscribe((response) => {
//handle response here
})
.catch(this.handleError);