2016.12.08 PS:
早期通过Intellij IDEA开发,那时还没有AndroidStudio,对应源码下载地址:http://download.youkuaiyun.com/detail/asmcvc/6885709
现在整理成AndroidStudio工程,源码上传到github上了:https://github.com/pythonstar/mobilesafe
项目使用Intellij IDEA开发,可以参考:http://blog.youkuaiyun.com/asmcvc/article/details/17144951配置开发环境。
项目名称为mobilesafe,主Activity为SplashActivity。
1、splash界面设计
界面是一个RelativeLayout显示一个背景图,右上角显示程序的当前版本号,中间显示一个加载的环形进度条。
主程序的图标资源为appicon.png,背景图片为logo2.jpg,分别将这两个图片复制到drawable目录下,drawable没有的话可以创建(也可以复制到drawable-hdpi目录下)。
对应的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:id="@+id/r1_splash" android:background="@drawable/logo2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="版本号:"
android:autoText="false" android:id="@+id/tv_splash_version"
android:textColor="#0aff00" android:textSize="20sp" android:layout_gravity="right"
android:layout_alignParentTop="true" android:layout_alignParentRight="true"/>
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar2" android:layout_gravity="center_vertical"
android:layout_marginBottom="139dp"
android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"/>
</RelativeLayout>
2、AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mobilesafe"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="17"/>
<application
android:label="@string/app_name"
android:icon="@drawable/appicon"
android:allowBackup="true">
<activity android:name="SplashActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
3、SplashActivity启动代码:
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置无标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置全屏模式
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//设置局部,显示版本号
setContentView(R.layout.activity_splash);
r1_splash = (RelativeLayout)findViewById(R.id.r1_splash);
tv_splash_version = (TextView)findViewById(R.id.tv_splash_version);
tv_splash_version.setText("版本号:" + getVersion());
//显示渐进动画
AlphaAnimation aa = new AlphaAnimation(0.3f, 1.0f);
aa.setDuration(2000);
r1_splash.startAnimation(aa);
}
获取当前应用程序版本号的函数:
//获取当前应用程序的版本号
private String getVersion() {
String version = "";
//获取系统包管理器
PackageManager pm = this.getPackageManager();
try {
PackageInfo info = pm.getPackageInfo(getPackageName(), 0);
version = info.versionName;
} catch (Exception e) {
e.printStackTrace();
}
return version;
}
4、运行效果图: