【问题标题】:Android splash screen animation not workingAndroid启动画面动画不起作用
【发布时间】:2015-03-29 13:48:22
【问题描述】:

当我的初始屏幕完成(2 秒)并移动到我的主要活动时,我希望这个动作带有 alpha 动画。一切正常,除了动画...

这是我的代码。有什么问题吗? (我有用于启动画面的文本和图像)

MainActivity.java(这里没有真正接触过任何东西)

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

SplashActivity.java

public class SplashActivity extends Activity {
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        int delay = 2000;
        new Handler().postDelayed(new Runnable() {
            public void run() {
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
                SplashActivity.this.finish();
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            }
        }, delay);
    }
    public void onBackPressed(){}
}

fade_in.xml

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromAlpha="0.0"
    android:toAlpha="1.0"
    android:duration="2000" />

fade_out.xml

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:duration="2000" />

AndroidMenifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.id11201478.WhatIf" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
        </activity>
        <activity
            android:name=".SplashActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activty_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="#FFFFFF"
    android:id="@+id/splashLayout"
    android:layout_height="match_parent">

    <TextView
        android:text="What If?"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="30dp"
        android:textSize="50dp"
        android:id="@+id/appName" />

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/appImage"
        android:src="@drawable/image"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

</LinearLayout>

谢谢!

【问题讨论】:

  • 我试过你的代码,动画效果很好,你能把xml文件和清单文件也贴出来吗??
  • 是的,我添加了它们,这可能与 API 有关吗?我正在使用 API 19...
  • 还在和我一起工作,你能告诉我你在上面测试的 android 版本吗
  • 是 API 版本 19。

标签: android animation splash-screen


【解决方案1】:

您可以尝试移动您的代码

overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

MainActivity。所以就变成了这样。

public class MainActivity extends Activity {
    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
    } 
} 

【讨论】:

  • 试过了,但仍然没有动画进入 MainActivity :(
  • 您可以尝试将overridePendingTransition 代码移到setContentView 之前吗?
猜你喜欢
  • 2022-12-20
  • 2017-06-15
  • 1970-01-01
  • 1970-01-01
  • 2014-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多