Android Studio淡入淡出启动画面进入主界面

8

我目前正在开发一款Android应用程序。刚开始时,我能够实现我的启动画面。然而,我不喜欢它和主要活动之间的过渡效果。我希望启动画面淡出,主要活动淡入。由于我在两者中都使用了相同的背景图片,所以它们看起来像是融合在一起。我做了一些研究,但还没有找到正确的答案。下面是我的代码:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;

public class Splash_screen extends Activity {

private Thread mSplashThread;

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_layout);
    final Splash_screen sPlashScreen = this;

    mSplashThread =  new Thread(){
        @Override
        public void run(){
            try {
                synchronized(this){
                    wait(3000);
                }
            }
            catch(InterruptedException ex){
            }
            finish();

            Intent intent = new Intent();
            intent.setClass(sPlashScreen, MainActivity.class);
            startActivity(intent);
        }
    };

    mSplashThread.start();
}

@Override
public boolean onTouchEvent(MotionEvent evt)
{
    if(evt.getAction() == MotionEvent.ACTION_DOWN)
    {
        synchronized(mSplashThread){
            mSplashThread.notifyAll();
        }
    }
    return true;
}
}

MainActivity class

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;

public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

可以随意删除与此任务无关的任何类或文件。 谢谢

1个回答

25

您可以使用两个 .xml 文件淡入一个新的Activity并淡出当前的Activity。

fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<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="500" />

淡出效果的XML文件

<?xml version="1.0" encoding="utf-8"?>
<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:fillAfter="true"
       android:duration="500" />

像这样在代码中使用它:(在您的Activity内部)

Intent intent = new Intent();
        intent.setClass(sPlashScreen, MainActivity.class);
        startActivity(intent);
overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

以上代码将淡出当前活动,并淡入新启动的活动。


我应该在哪个文件夹中创建这两个 .xml 文件?@Y.S - KimCheeFatChoyProgrammer
你需要在资源文件夹中创建一个名为"anim"的文件夹,并在该"anim"文件夹中创建这两个.xml文件。 - Yograj Shinde
棒极了。非常感谢@Y.S,运行得很好。这是一个令人欣慰的网站。 - KimCheeFatChoyProgrammer
我经常在启动画面上使用淡入效果。这样做还是一个好的实践吗? - Mateusz Kaflowski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接