Android UI經常使用執行個體 怎樣實現歡迎介面(Splash Screen)

來源:互聯網
上載者:User

標籤:基本   ble   com   javascrip   connect   大小   驗證   gravity   dcl   

在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等檔案
  • 發送資料到服務端
  • 身分識別驗證
  • 。。。。

反正一般都是相似於網路下載這樣的些耗時操作,但又不得不在應用進入主介面前須要做的工作。依據應用的不同,所做的工作也不同,這裡我們就以遠程擷取一張圖片為例,我們在進入歡迎介面後,開始從遠程下載一張圖片,完畢後我們便進入主介面,將下載好的圖片顯示在主介面;

  1. 圖片地址::
  2. 建立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>

布局效果

  1. 建立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黑曆史
  • 郵箱: <[email protected]>
  • 個人首頁: 明桑戰勝Android汪的黑曆史
  • 公眾號: ITBird

Android UI經常使用執行個體 怎樣實現歡迎介面(Splash Screen)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.