老规矩,效果图如下:
源码如下:
https://github.com/ok406lhq/StartPageDemo
启动页的开发主要包括了开屏闪屏和广告页的实现,效果如上图,具体的实现如下。
1、闪屏设计
实现方式有比较多种,其中有一种是通过Intent跳转的方式,跳转到一个布局只有布局中添加了闪屏图片的SplashActivity中,实现短暂的闪屏效果。这样做有一个小缺点(也可以忽略不计):
在启动App时有一瞬间的短暂白屏,随后再跳转到闪屏页面,这样的效果:
这样的话用户体验可能会不太好,如何实现秒开闪屏那种效果呢?就像支付宝、知乎那种,接下来就是第二种方式,也就是本次实现用到的。其实很简单,只要在AndroidManifest中用到闪屏那个Activity中加上一个样式就可以实现了。话不多说,上代码:
<activity
android:name=".StartPageActivity"
android:theme="@style/SplashActivityTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
在style.xml中编辑主题样式SplashActivityTheme:
<!-- Base application theme. -->
<style name="SplashActivityTheme" parent="android:Theme.Holo.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowBackground">@drawable/splash</item>
</style>
如此一来,应用在启动时将直接将闪屏图片显示,达到更好的体验效果~
当然,在闪屏页StartPageActivity中也是要做一些处理的,不然看不到效果的,我们将跳转到主页面用延迟1秒来执行:
Thread myThread = new Thread() {//创建子线程
@Override
public void run() {
try {
sleep(1000);//使程序休眠一秒
Intent it = new Intent(getApplicationContext(), InitAdvActivity.class);
startActivity(it);
finish();//关闭当前活动
} catch (Exception e) {
e.printStackTrace();
}
}
};
myThread.start();//启动线程
这些都很简单,接下来是来设计广告页~
2、广告页设计
这个也不难,简单说一下逻辑,这边我用随机数实现广告页的随机出现,以此来模拟一些App广告页的效果。也就是说,不是每次打开应用都会有广告,这是随机控制的。布局文件会在Demo源码中可以找到~
贴一下代码:
public class InitAdvActivity extends BaseActivity {
private int[] picsLayout = {R.layout.layout_hello, R.layout.layout_hello2,
R.layout.layout_hello3, R.layout.layout_hello4, R.layout.layout_hello5};
private int i;
private int count = 5;
private Button mBtnSkip;
@SuppressLint("HandlerLeak")
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 0) {
mBtnSkip.setText("跳过 (" + getCount() + ")");
handler.sendEmptyMessageDelayed(0, 1000);
}
}
};
private LinearLayout linearLayout;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Random r = new Random();
i = r.nextInt(10);
Log.i("test", "随机数是:" + i);
if (i < 5) {
//随机概率会出现广告页
setContentView(R.layout.activity_adv);
initView2();
} else {
startActivity(new Intent(InitAdvActivity.this, MainActivity.class));
finish();
}
}
private void initView2() {
linearLayout = findViewById(R.id.advLine);
View view = View.inflate(InitAdvActivity.this, picsLayout[i], null);
linearLayout.addView(view);
mBtnSkip = view.findViewById(R.id.btn_skip);
mBtnSkip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(InitAdvActivity.this, MainActivity.class));
handler.removeMessages(0);
finish();
}
});
handler.sendEmptyMessageDelayed(0, 1000);
}
public int getCount() {
count--;
if (count == 0) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
}
return count;
}
}
3、彩蛋
其实,这些实现都并不难,相信大家都能很容易就看懂。
接下来说一个问题,那就是状态栏的问题,其实现在很多App都使用了沉浸式状态栏的效果(我也不知道这个概念对不对,大概意思是指状态栏透明)来使得页面更加好看,呈现更多的内容。所以我们在启动页开发的时候如果没有处理状态栏的话,那么效果将会比较差,我截了几张图,可以看一下:
惨不忍睹。。
具体实现也很简单:
所有Activity继承一个BaseActivity,在BaseActivity中做状态栏的处理即可:
public class BaseActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTranslucent(this);
}
public static void setTranslucent(Activity activity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// 设置状态栏透明
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
}
}
ps
好了,大概内容就这么多,比较基础,其实启动页这一项,可以延伸出很多,包括我之前公司,是这样需求的:通过服务器后台来控制广告页的显示图片以及显示概率。这就需要在加载App时对网络图片资源进行缓存,我用到的是Picasso这个图片处理框架,除此之外,应该还可以有更多不一样的扩展。需求不同,可能实现的方式也不一样,加油吧~