作为Android 开发工程师,面对不同品牌的手机,还有各种高低档位,高低内存的手机进行适配,是一件非常麻烦的事情,其实有时候感觉比IOS,我们Android 工程师要做的事情多很多.
做启动页的时候,就要考虑这些情况:
- 如何把内存控制在最小范围
- 如何快速的加载
- 如何使他不会卡顿(因为在正式发布后,一些低版本的手机,打开启动页,就会闪退,或者卡顿,在高版本手机上,就不会存在这样的情况)
- 如何让图片放入资源文件的时候更小,有时候一张高清度的图片,可以达到6-6M,如果事前不进行处理,会直接增大apk的,版本发布后体积
解决办法:
- 在公司情况允许的条件下,尽量让UI采用颜色浅一点的启动页,深色的启动页会直接增大APK的体积,然后页面做的简单点.
- 做出来的图片,先对其在线进行压缩
- https://tinypng.com/ 该网站只支持图片小于5M的图片,超过了要给钱才能进行压缩
- http://optimizilla.com/zh/ 大于5M用这个压缩下,压了之后还感觉比较大,继续用上面的进行压缩.
- 一般这样做之后,图片可以做到70-90%左右的压缩率
- 给启动页单独设置一个主题背景, 这样可以做到App启动的时候秒开的效果,而不是在他的xml文件直接放置一张图片做背景
- 一般启动页都会有一个定时,为了是给后面的页面启动留点时间.
- 定时的handler 尽量采用如图下的方式,进行消息发送,并在页面销毁的时候,清空掉队列中的消息.好避免handler内存泄露问题出现.handler也必须设置为静态,(即使这样,也会有一些内存泄露的情况,完全没有,那是非常难的)
代码如下:
1.清单文件:
<application
android:name=".app.BaseApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:persistent="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".ui.activity.SplashActivity"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
..................
2. style.xml文件:
<!--Theme.PageIndicatorDefaults-->
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:background">@drawable/background_splash</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
3.activity
public class SplashActivity extends AppCompatActivity {
private static MyHandler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { //启动模式
finish();
return;
}
final Boolean isLogin