Android 创建android Splash界面以及避免app闪屏现象
参考:
创建Android启动界面:http://www.cnblogs.com/xiongbo/archive/2011/05/17/splash.html
android颜色color.xml设置:http://blog.163.com/gis_warrior/blog/static/19361717320128149423856/
android开发之提高应用启动速度_splash页面瞬时响应_避免APP启动闪白屏:http://blog.csdn.net/jason0539/article/details/45559507?utm_source=tuicool
#########################################################################3
新建项目SplashDemo,默认生成一个空Activity(MainActivity)
新建一个空Activity,命名为SplashActivity
修改res/values下的style.xml(如果没有则新建一个):
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="AppSplash" parent="android:Theme">
<item name="android:windowBackground">@drawable/draw</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
添加一个自定义主体AppSplash,添加窗口背景图片以及设置窗口没有标题栏(这里为了避免app闪白屏)
同样在res/values目录下修改colors.xml(如果没有新建一个):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFFFFF</color><!--白色 -->
</resources>
修改activity_splash.xml的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white"
>
<!-- android:scaleType是控制图片如何resized/moved来匹对ImageView的size
CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽 -->
<ImageView
android:scaleType="centerInside"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/wordpress_logo"
android:src="@drawable/draw2">
</ImageView>
<!-- android:typeface 字体风格 -->
<TextView
android:id="@+id/versionNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_gravity="center_horizontal|bottom" />
</LinearLayout>
修改SplashActivity.xml的代码如下:
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;
import android.widget.TextView;
public class SplashActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
//Display the current version number
PackageManager pm = getPackageManager();
try {
PackageInfo pi = pm.getPackageInfo("com.zj.splashdemo", 0);
TextView versionNumber = (TextView) findViewById(R.id.versionNumber);
versionNumber.setText("Version " + pi.versionName);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
//splash_view=(ImageView)findViewById(R.id.splash_view);
new Handler().postDelayed(new Runnable() {
public void run() {
/* Create an Intent that will start the Main WordPress Activity. */
Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
}
}, 2900); //2900 for release
}
}
在SplashActivity活动中显示图片(居中)和版本号(下部)
最后修改AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zj.splashdemo" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name=".SplashActivity"
android:label="@string/app_name"
android:theme="@style/AppSplash"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这样运行程序即可