65.9K
CodeProject 正在变化。 阅读更多。
Home

Android 应用的高级启动屏幕

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.80/5 (41投票s)

2010年9月29日

CPOL

4分钟阅读

viewsIcon

511906

downloadIcon

24387

启动屏幕 Android 教程。

引言

每个人都希望自己的应用程序对用户来说美观且有吸引力。有很多应用程序,至少是桌面应用程序,主要是游戏,它们使用闪屏。这很不错,而且,在闪屏运行时,您可以初始化您的应用程序。有很多教程解释如何开始 Android 编程,我在这里不会重复。您可以在互联网上找到它们。所以,我只会展示编程部分。

开始

使用以下设置创建一个新的 Android Eclipse 项目

Project name : AdvancedSplashDemo
Build target: I've set it to Android 2.1
Application name: Advanced Splash Demo
Package name: Advanced Splash Demo
Create Activity: MainActivity – it will be the application itself 

所以,由于完成工作后我们不需要闪屏,第一个想法是使用另一个会启动主活动然后悄然消失的活动。让我们为闪屏创建一个布局 – 它将是一个线性布局,里面有一个 Image View。在 appfolder/res/layout 文件夹中创建一个新的 Android XML 文件“splash.xml”。不要让它填满父布局,因为我们希望它确实像一个闪屏。Image View 也必须包裹内容。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:id="@+id/TheSplashLayout"
  android:layout_gravity="center"
  >
    <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:id="@+id/SplashImageView"
        android:layout_gravity="center"        
        android:src="@drawable/lnxins"        
        >
    </ImageView>
</LinearLayout>

这里的 gravity 属性值设置为“center”以使闪屏位于屏幕中央。在 appfolder/res/drawable 文件夹中添加一些图片,然后按 F5 刷新项目。我添加了 lnxins.png,正如您所见,将其设置为 Image View 的源。

到目前为止,让我们看看应用程序清单。它现在只有一个设置为启动器的“.MainActivity”活动。我们将它设置为默认类别,并添加另一个具有闪屏布局的闪屏活动,并将其设置为启动器。打开清单并打开应用程序选项卡。对于主活动,将 Android intent category 更改为 default。在 application 节点附近,按“Add…”按钮,选择在顶层创建新元素,然后双击 Activity。对于新活动,单击“Name*”超链接并输入“SplashScreen”类。在 sources 中,将为闪屏活动添加一个新类。接下来,再次为 SplashScreen 节点按“Add…”按钮,并添加 intent filter。同样,只为新添加的 intent filter 添加一个 action 和一个 category。

将 action 设置为 android.intent.action.MAIN ,并将 category 设置为 android.intent.category.LAUNCHER。这样,Splash screen 活动将首先运行。清单应如下所示

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.yourname.main"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>    
        <activity android:name="SplashScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"></action>
                <category android:name="android.intent.category.LAUNCHER"></category>
            </intent-filter>
        </activity>
    </application>
</manifest> 

一点编码

打开 SplashScreen.java 类。现在它只重写了 onCreate 方法。重写 onTouchEvent 方法,以便用户可以在任何时候关闭闪屏。并且不要忘记同步,否则会出现随机崩溃。这是类的代码

 public class SplashScreen extends Activity {
    
    /**
     * The thread to process splash screen events
     */
    private Thread mSplashThread;    

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Splash screen view
        setContentView(R.layout.splash);
        
        final SplashScreen sPlashScreen = this;   
        
        // The thread to wait for splash screen events
        mSplashThread =  new Thread(){
            @Override
            public void run(){
                try {
                    synchronized(this){
                        // Wait given period of time or exit on touch
                        wait(5000);
                    }
                }
                catch(InterruptedException ex){                    
                }

                finish();
                
                // Run next activity
                Intent intent = new Intent();
                intent.setClass(sPlashScreen, MainActivity.class);
                startActivity(intent);
                stop();                    
            }
        };
        
        mSplashThread.start();        
    }
        
    /**
     * Processes splash screen touch events
     */
    @Override
    public boolean onTouchEvent(MotionEvent evt)
    {
        if(evt.getAction() == MotionEvent.ACTION_DOWN)
        {
            synchronized(mSplashThread){
                mSplashThread.notifyAll();
            }
        }
        return true;
    }    
} 

现在是时候进行第一次运行了。闪屏等待 5 秒。看起来不好,只是两个黑色屏幕,一个接一个。

一点美化

首先,让我们使闪屏透明。在 appfolder/res/values 中,添加新的 Android XML 文件 styles.xml 并添加一个透明主题

<resources>
    <style name="Theme.Transparent" parent="android:Theme">
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:backgroundDimEnabled">false</item>
    </style>    
</resources> 

这里有一些解释:正如您所见,style 的 parent 是 android:Theme ,所以我们可以将其应用于我们的活动。而且正如您所见,属性的名称很清晰,您可以理解它们的含义。

接下来,我们将此主题应用于我们的闪屏。在清单文件中,为闪屏活动设置“theme”属性为刚刚创建的主题

<activity 
    android:name="SplashScreen"
    android:theme="@style/Theme.Transparent"            
>
    <intent-filter>
        <action android:name="android.intent.action.MAIN"></action>
        <category android:name="android.intent.category.LAUNCHER"></category>
    </intent-filter>
</activity> 

假设我们正在开发一个游戏应用程序。而游戏玩家不喜欢任何东西干扰他们的游戏过程。他们中的大多数更喜欢全屏模式。因此,为主活动设置全屏主题

<activity android:name=".MainActivity"
          android:label="@string/app_name"
          android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
          >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

运行它。看起来好多了。现在我们将让它淡入淡出。在 appfolder/res 文件夹中创建一个新文件夹“anim”,并在其中添加两个 Android XML 文件 – appear.xmldisappear.xml。它们将是 alpha 动画。

Appear.xml

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

Disappear.xml

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

在这些动画中,它们只是在给定时间内将对象的 alpha 通道从 fromAlpha 值更改为 toAlpha 值。现在在 styles.xml 中添加新样式

<style name="Animations" parent="@android:Animation" />
    <style name="Animations.SplashScreen">
        <item name="android:windowEnterAnimation">@anim/appear</item>
        <item name="android:windowExitAnimation">@anim/disappear</item> 
    </style>
</style>

因此,在窗口上,将执行“appear”动画,在窗口退出时执行“disappear”动画。将此样式添加到 Theme.Transparent 主题

<style name="Theme.Transparent" parent="android:Theme">
        ………
<item name="android:windowAnimationStyle">@style/Animations.SplashScreen</item></style>  

好的,再次运行它的时间到了。现在它看起来不错。而且更多…

不要枪击程序员,他能画什么就画什么…

让我们创建一个动画闪屏。作为一名艺术家,我不是很好,所以我使用了 Gimp 的 Script-Fu 来生成一组动画帧。首先,删除 splash.xml 中的 android:src 属性。然后,在 drawable 文件夹中,创建 flag.xml

 <?xml version="1.0" encoding="utf-8"?>
<animation-list     
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/flaganim"
    android:oneshot="false"
    >
    <item android:drawable="@drawable/f03" android:duration="100" />
    <item android:drawable="@drawable/f04" android:duration="100" />
    <item android:drawable="@drawable/f05" android:duration="100" />
    <item android:drawable="@drawable/f06" android:duration="100" />
    <item android:drawable="@drawable/f07" android:duration="100" />
    <item android:drawable="@drawable/f08" android:duration="100" />
    <item android:drawable="@drawable/f09" android:duration="100" />
    <item android:drawable="@drawable/f10" android:duration="100" />    
</animation-list> 

这里有一组帧,“oneshot”属性表示循环播放。要运行动画,我们需要更改闪屏类的代码。在 onCreate 方法中,添加以下内容

 final ImageView splashImageView = (ImageView) findViewById(R.id.SplashImageView);
 splashImageView.setBackgroundResource(R.drawable.flag);
 final AnimationDrawable frameAnimation = 
(AnimationDrawable)splashImageView.getBackground(); 

我们已经为闪屏设置了动画,但这里有一个小问题。我们不能从 onCreate 方法启动它。动画必须从 GUI 线程启动。为此,我们将使用 ImageView 类的“post”方法。它会将我们的 runnable 添加到消息队列中,当 GUI 线程可用时,它将启动它

splashImageView.post(new Runnable(){
            @Override
            public void run() {
                frameAnimation.start();                
            }            
        }); 

我们做到了

shot.jpg

就是这样。祝您 Android 编程愉快。

谢谢!

历史

  • 2010年9月29日:初始帖子
© . All rights reserved.