当前位置:   article > 正文

Android程序的启动页面(闪屏页面)设计_android启动页变成桌面logo

android启动页变成桌面logo

Android程序的启动页面(闪屏页面)设计

        每个Android应用启动之后都会出现一个Splash启动界面,显示显示广告信息或产品的LOGO、公司的LOGO或者开发者信息。如果应用程序启动时间比较长,那么启动界面就是一个很好的东西,可以让用户耐心等待这段枯燥的时间。

一.启动页面的基本知识

(一)Splash界面显示的信息

  突出产品LOGO,产品名称,产品主要特色;
  注明产品的版本信息;
  注明公司信息或者开发者信息;
展示广告信息等;
  背景图片,亦可以用背景颜色代替;
  

(二)处理后台资源

  
  大多数的Splash界面都是会等待一定时间,然后切换到下一个界面;
  其实,在这段时间里,可以对系统状况进行检测,比如网络是否通,电源是否充足;
  或者,预先加载相关数据;
  为了能让启动界面展现时间固定,需要计算执行以上预处理任务所花费的时间,那么:启动界面SLEEP的时间=固定时间-预处理任务时间;
  

(三)Splash页面的实现方法,这里提供三种

1.使用SplashActivity,设置SplashActivity在AndroidManifest.xml为先显示Activity对象,然后让SplashActivity在多秒后跳转到主页页面并关闭页面。

2.在主页的布局xml文件中在原本的布局上层添加可以控制的全屏的布局,在MainActivity中通过代码控制表面上的全屏布局对象,在多秒后隐藏该布局,就可以显示出要显示的主页页面。

3.在MainActivity的onCreate方法中弹出一个全屏的对话框,这里的对话框也是可以全屏的自定义View,同样也是在多秒后关闭对话框,显示主页。

       从开发的复用性,第一个和第三个方法是可以很方便的移植到其他程序,本文介绍第一种方法。

二.启动页面的程序示例(使用SplashActivity)

效果:

z1

(一)SplashActivity的设计

package com.example.splash;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 启动页面的设计
 */

public class SplashActivity extends Activity {

    //显示广告页面的时间,5 秒
    long showTime=5;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        textView= (TextView) findViewById(R.id.text);

        //延迟5000ms跳转到主页面
        handler.postDelayed(myRunnable, showTime*1000);
        handler.sendEmptyMessage(111);//給Handler对象发送信息
    }

    //创建Handler对象
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==111){

                Log.e("TAG","what"+showTime);
                textView.setText(""+showTime);
                showTime--;//时间减一秒
                if (showTime>0){
                    handler.sendEmptyMessageDelayed(111,1000);//一秒后給自己发送一个信息
                }

            }

        }
    };

    //创建Runnable对象
    Runnable myRunnable=new Runnable() {
        @Override
        public void run() {
            jundToMainActivity();
        }
    };

    //跳转到主页的方法,并关闭自身页面
    public void jundToMainActivity(){
        Intent intent = new Intent(SplashActivity.this,
                MainActivity.class);
        startActivity(intent);
        finish();
    }

    //关闭页面
    public void closeSplash(View view){
        Log.e("TAG","closeSplash");
        handler.removeCallbacks(myRunnable);//移出Runnable对象
        jundToMainActivity();

    }

    //回退键的监听方法,
    // 这里如果直接关闭页面,线程没有关闭的话,5秒后还是会启动主页面,除非移出线程对象
    @Override
    public void onBackPressed() {
       // super.onBackPressed();不让它关闭
        Toast.makeText(this,"广告之后更精彩!",Toast.LENGTH_SHORT).show();

        //如果按回退键,关闭程序,代码设计
       // finish();//关闭页面
       // handler.removeCallbacks(myRunnable);//取消runnable对象

    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90

(二)activity_splash.xml文件的设计

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0"
    >

    <!--我发现如果TextView没有设置clickable属性,点击居然无效-->
    <Button android:layout_width="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_height="wrap_content"
        android:text="关闭"
        android:layout_margin="10dp"
        android:onClick="closeSplash"
        android:clickable="true"
        />

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是启动页面!"
        android:textSize="30sp"
        android:layout_centerInParent="true"
        />
    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text"
        android:text="5"
        android:textSize="30sp"
        android:layout_alignParentBottom="true"
       android:layout_centerHorizontal="true"
        android:layout_margin="20dp"
        android:background="#f00"
        />

</RelativeLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

(三)MainActivity的设计

package com.example.splash;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
/**
*这里只是简单显示主页面
*/
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView textView=new TextView(this);
        textView.setText("主页欢迎你");
        textView.setTextSize(30);
        setContentView(textView);

    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

(四)AndroidMainfest文件的主要代码

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

</activity>

<activity android:name=".MainActivity"/>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

(五)sytle的设计(位置values–>styles.xml)

<!-- Splash Screen -->
<style name="SplashThemeStyle">
    <item name="android:padding">0dp</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowNoTitle">true</item>
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

       到这里一个完整的启动页面的设计已经完成了.
       我的这个启动页面也是做了比较复杂的判断,我之前看很多启动页面没有监听回退键,这个明显是会出现问题的

这里提示一下后面两种方法的主要代码:

第二种方法:

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

        <TextView android:id="@+id/info"   
                        android:layout_width="fill_parent"  
            android:layout_height="wrap_content"   
                        android:gravity="center"  
            android:paddingTop="10px"   
                        android:text="This is a splash !" />  
    </LinearLayout>  

    <TextView android:layout_width="fill_parent"  
        android:paddingTop="10px"   
                android:layout_height="wrap_content"  
       android:text="This is a Context" />  
</LinearLayout>  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

说明:
这里有一个id为splashscreen的LinearLayout,是程序启动时显现的部分。当启动完成后,它会被隐藏。

第三种方法:

  @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);       
        setContentView(R.layout.main);  
shwoDialog();

}

/**
*这里的CursorDialog是一个继承自Dialog的类,里面封装了很多方法
*/
 private void shwoDialog() {
        //显示启动页
        ImageView iv = new ImageView(this);
        iv.setBackgroundResource(R.mipmap.welcomebg);
//下面的方法都是自己封装的!
        CursorDialog dialog = new CursorDialog.Builder(this)
                .full()
                .setView(iv)
                .builder();
    dialog.show(3000);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

上面就是显示启动页面的常用方法。
第一种方法也是我建议使用的,这个可以响应很多事件,并且可以播放MP4文件当启动页面等等,移植也是非常方便。
需要上面程序源码可以留言。

z2

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/144727
推荐阅读