Android Studio逐渐淡出主流 [英] Android Studio fading splash into main

查看:176
本文介绍了Android Studio逐渐淡出主流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个android应用.刚开始,我就能实现我的启动画面.但是,我不喜欢它和主要活动之间的过渡.我希望启动画面淡出,而主画面淡入.由于我的背景图像相同,因此它们看起来像融为一体.做了一些研究,但还没有找到正确的答案. 在下面,我已经发布了我的代码.

I currently am working on an android app. Just started and I was able to implement my splash screen. However, I don't like the transition between that and the main activity. I want the splash screen to fade out and main to fade in. Looks like they blend together since I have the same background image for both. Did some research but havent been able to find the right answers. Below, I have posted my code.

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类

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);
}
}

随意删除此任务不需要的任何类或文件. 谢谢

Feel free to delete any classes or files not needed for this task. Thanks

推荐答案

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

You could use two .xml files to fade in a new Activity and fade out the current Activity.

fade_in.xml

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" />

fade_out.xml

fade_out.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" />

在如下代码中使用它:(在活动"内)

Use it in code like that: (Inside your Activity)

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

上面的代码将淡出当前活动的Activity,并淡出新启动的Activity.

The above code will fade out the currently active Activity and fade in the newly started Activity.

这篇关于Android Studio逐渐淡出主流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆