一个高级的Android启动画面

来源:互联网 发布:数据库百科 编辑:程序博客网 时间:2024/06/09 22:23

转自:http://www.qtprogrammer.org/?p=281

 

开始

用一下步骤建立一个新的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 – 这个是真正的应用程序

所以,在启动画面结束后,我们就不再需要它了,第一个想法就是,用另外一个activity去,启动main antivity并且这个activity会在启动完成后死掉。让我们为启动画面建立一个布局里面有一个线性布局和一个ImageView。建立一个新的Android XML 文件 “splash.xml” 在应用程序目录/res/layout 文件夹.不要让它填充父窗口,这样我们会让启动窗口更真实.ImageView 控件要设置根据内容改变大小:

<?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” 是为了让启动窗口处在屏幕的中间.增加一些图片到应用程序目录/res/drawable 文件夹并且按F5来刷新工程。就像你能看到的,我已经加入了 lnxins.png,并且把它设置成了Imageviewsrc属性了.

 

现在,我们来看应用程序的manifest.它现在只有一个设置为启动的“.MainActivity”.然而我们要把它设置为一个默认的类并且,把另外那个带有启动布局的启动画面窗体,设置为启动器。打开manifest,并且能看到Application 标签页。把main activity,的Android intent category的属性设置成default。在Application 标签页上靠近Application Nodes的地方选择 Add,选则create a new element at top level 并且在Activity 上双击。在新的Activity的右边属性设置界面上,点击“Name*”链接并且键入“SplashScreen”类。在代码中,会为splash 窗体建立一个新类。接下来,SplashScreen节点再次点击“Add…”按钮,增加一个intent filter

再一次,改manifest文件,为刚增加的intent filter,增加一个action和一个category

action改为android.intent.action.Main,category 设置为android.intent.category.LAUNCHER.

这样,这个启动窗体,就会第一个启动。现在这个manifest就会像下面这个样子:

<?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 class.它只重写了onCreate方法。重写 onTouchEvent 方法让用户可以在任何时候,关闭启动画面。并且不要忘记synchronization,不然,你的程序会随机的崩溃。下面是这个类的代码:

 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秒钟。看起来,它并不美,只是连着两个黑色的黑色屏幕,一个切换到另一个。

 

一点美化处理

首先,我们让启动屏幕 透明。在应用程序目录/res/values,新建一个新的Android XML 文件style.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>

这里需要作一些阐述:就像你能看到的,这个样式的父亲是:androidTheme。所以我们可以把它应用到我们的activity种。就像你能看到的,这些属性的名字都非常清楚,你能够明白他们的意思。 

 

接下来,我们要把这个主题应用到我们的启动画面中,在manifest 文件,把启动画面窗体的“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>

我们假设我们真在开发的是一款游戏。并且玩家,不喜欢有东西打扰它们,使屏幕从他们的游戏画面中切换出来。他们大多数更喜欢全屏。 所以,把main 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>

运行它,更好看了,现在让我们给它淡入淡出得效果。在应用程序目录/res/res文件夹新建文件夹anim,并且加入appear.xmldisappear.xml。他们是透明动画。

 

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。现在在style.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>

好了,是时候,再次运行了,现在,会很飘飘,不是,是更漂亮。

 

 

不要针对一个写代码的,他会尽力做好

让我们建立一个有动画的启动画面,我能力不是很强,所以我做动画,把每一帧都做好。首先,我们把splash.xml 中的ImageViewandroidsrc属性去掉。然后在,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”属性是表示循环,要运行这个动画,我们需要改变splash

Screen 类中的代码。在onCreate 方法中,加入下面代码:

 final ImageView splashImageView = (ImageView) findViewById(R.id.SplashImageView);

 splashImageView.setBackgroundResource(R.drawable.flag);

 final AnimationDrawable frameAnimation =

(AnimationDrawable)splashImageView.getBackground();

我们在启动画面中加入动画,但是有个小问题,我们不能再onCreate 方法中启动动画。

动画必须在一个GUI 线程中启动,这样,我们需要使用ImageView 类的“post” 方法。

它会把我们的线程加入到一个消息队列,当GUI线程启动的时候,他就会执行了:

splashImageView.post(new Runnable(){

            @Override

            public void run() {

                frameAnimation.start();                

            }            

        });

下面是我们的效果图:

 

就这样了,祝你Android编程愉快。

谢谢!

原创粉丝点击