在Android平台下。下載一個應用后,首次打開映入眼簾的便是Splash Screen,暫且不說Android的設計原則提不提倡這樣的Splash Screen。先來看看一般使用Splash Screen的場景:
1,第一次安裝后,簡單APP的閃屏達到品牌營銷的目的,復雜點的APP用來提供新手指導;
2。版本號更新。說明版本號新特性。
有人對這樣的設計嗤之以鼻。有人趨之若鶩,孰好孰壞不在我們探討之列。
1,簡單的Splash Screen
這樣的Splash Screen實現及其簡單。經常使用來顯示產品Logo或者版本號號等簡單信息,我們僅僅須要想辦法讓WelcomeActivity運行幾秒種后自己主動跳轉到應用主界面就可以;
我們僅僅須要用到一個簡單的方法:
//3s后,運行run方法啟動主界面Activity
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
//啟動主Activity后銷毀自身
finish();
}
}, 3000);
2,涉及復雜操作的Splash Screen
所謂復雜操作是由於往往這樣的應用在進入界面之前須要進行非常多后台操作,通過Splash Screen讓用戶等待。一般涉及的操作有:
- 從網絡獲取數據並存儲到本地
- 下載圖片
- 獲取和解析JSON/XML等文件
- 發送數據到服務端
- 身份驗證
- 。。。。
反正一般都是相似於網路下載這樣的些耗時操作,但又不得不在應用進入主界面前須要做的工作。依據應用的不同,所做的工作也不同,這里我們就以遠程獲取一張圖片為例,我們在進入歡迎界面后,開始從遠程下載一張圖片,完畢后我們便進入主界面,將下載好的圖片顯示在主界面;
- 圖片地址::
- 創建SplashScreen布局:
res/layout/splash_screen.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:gravity="center" android:orientation="vertical" android:layout_height="match_parent" android:layout_width="match_parent">
<ImageView android:id="@+id/appImage" android:src="@mipmap/logo" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView android:gravity="center" android:text="Welcome to MS_Movie" android:layout_marginTop="15dp" android:textSize="30sp" android:textColor="#00ACED" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
布局效果
- 創建MainActivity布局:
res/layout/activity_main.xml
,僅僅用來顯示從遠程獲取的圖片
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</RelativeLayout>
4.完畢SplashScreenActivity,我們使用AsyncTask來運行獲取和解析數據,通過Intent將數據傳遞給MainActivity,
public class SplashScreenActivity extends Activity {
private static final String url="http://www.jycoder.com/json/Image/1.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
/* * 簡單Splash Screen的實現 * */
/* * 3s后,運行run方法啟動主界面Activity new Handler().postDelayed(new Runnable() { @Override public void run() { Intent i = new Intent(SplashScreen.this, MainActivity.class); startActivity(i); //啟動主Activity后銷毀自身 finish(); } }, 3000); * */
//在后台運行任務,傳入url
new FetchDataTask().execute(url);
}
public class FetchDataTask extends AsyncTask<String,Void,Bitmap>{
//運行前調用
@Override
protected void onPreExecute() {
super.onPreExecute();
}
//運行后台任務
@Override
protected Bitmap doInBackground(String... strings) {
Bitmap bitmap=null;
try {
//通過傳入的圖片地址,獲取圖片
HttpURLConnection connection= (HttpURLConnection) (new URL(strings[0])).openConnection();
InputStream is=connection.getInputStream();
bitmap= BitmapFactory.decodeStream(is);
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
//任務完畢時調用
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
//將獲得的數據通過Intent傳送給MainActivity
Intent intent=new Intent(SplashScreenActivity.this,MainActivity.class);
//注意,intent傳遞圖片時,圖片對象大小不應該超過40K
intent.putExtra("Image",result);
startActivity(intent);
//啟動MainActivity后銷毀自身
finish();
}
}
}
5.完畢MainActivity。這里須要注意怎樣接受Intent傳遞的對象
public class MainActivity extends Activity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView= (ImageView) findViewById(R.id.image);
Intent intent=getIntent();
if(intent!=null){
//注意Intent傳遞對象的方式
Bitmap bitmap=intent.getParcelableExtra("Image");
imageView.setImageBitmap(bitmap);
}
}
}
6.記得在Manifest.xml中增添聯網權限。將SplashScreenActivity設為啟動Activity
<?xml version="1.0" encoding="utf-8"?
> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.coder.splashscreen" > <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/logo" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".SplashScreenActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity"/> </application> </manifest>
我第一次用我存儲在遠程的一張圖片的時候出錯了,遇到了一個非常有意思的問題:
后來發現這樣的錯誤是由於用Intent傳遞圖片時,大小不能超過40K,記住哦
完畢后的效果:
總結:
以上的樣例都非常easy,但基本上Splash Screen的思路無非這幾種。希望能對你有所幫助。
記得關注下方微信公眾平台~~~O(∩_∩)O哈哈~
參考資料:How to implement Android Splash Screen
- 微博: @明桑Android黑歷史
- 郵箱: <13141459344@163.com>
- 個人主頁: 明桑戰勝Android汪的黑歷史
微信公眾號: ITBird