Android啓動動畫實現

首先提一下實現機制:

1.將manifest中launcher頁設爲我們需要顯示的啓動頁面。
2.在啓動動畫頁面中我們先加載我們需要的啓動頁面(動畫、文字、廣告等)。
3.在啓動頁的activity中利用線程的postDelayed方法來延遲3s,3s後便執行跳轉到主界面或者
登錄界面(也可以通過檢查SharedPreferences中是否記住了用戶歷史賬號信息,有記住就直接
執行登錄操作,沒有就跳轉到登錄頁面)。
PS:解決啓動頁面白屏傳送

實現步驟:

一、新建啓動頁StartActivity

package cn.com.box.black.bbnotepad.Activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.airbnb.lottie.LottieAnimationView;

import cn.com.box.black.bbnotepad.Bean.SuccessBean;
import cn.com.box.black.bbnotepad.Listener.TListener;
import cn.com.box.black.bbnotepad.Model.LoginModel;
import cn.com.box.black.bbnotepad.R;

import static cn.com.box.black.bbnotepad.Server.AUTO_LOGIN;
import static cn.com.box.black.bbnotepad.Server.user_id_remember;
import static com.githang.statusbar.StatusBarTools.getStatusBarHeight;

/**
 * Created by gcj on 2017/11/28
 */
public class StartActivity extends AppCompatActivity {
    private Handler handler = new Handler();
    private LottieAnimationView mAnimationView;
    private String username,userpass;
    private CheckBox checkBox;
    private String userid;
    private boolean check;
    TListener<SuccessBean> tListener = new TListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean loginBean) {
            userid = loginBean.getSuccess().toString();
            //記錄用戶id
//            showToast(userid);
            user_id_remember= Integer.parseInt(userid);
            if(loginBean.getSuccess().toString().equals("0")){
                Toast.makeText(StartActivity.this,"登陸失敗",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(StartActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
            else{
//                Toast.makeText(LoginActivity.this,"登陸成功",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setClass(StartActivity.this, MainActivity1.class);
                startActivity(intent);
                finish();
            }
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(StartActivity.this,"登陸失敗",Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(StartActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        getSupportActionBar().hide();
        if (Build.VERSION.SDK_INT <= 19) {
            setStatusBarColor4_4(this, ContextCompat.getColor(this, R.color.back_red) );
        } else {
            setStatusBarColor(this,ContextCompat.getColor(this, R.color.back_red) );
        }
        // 注意:此處將setContentView()方法註釋掉
         setContentView(R.layout.activity_start);
        check=getUser_remember();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                gotoLogin();
            }
        }, 3000);
    }

    /**
     * 前往註冊、登錄主頁
     */
    private void gotoLogin() {
        username = getUser_name();
        userpass = getUser_password();
        if(AUTO_LOGIN==0) {
            LoginModel loginModul = new LoginModel();
            loginModul.getUserList(username, userpass, tListener);
        }else{
            Intent intent = new Intent(StartActivity.this, LoginActivity.class);
            startActivity(intent);
            finish();
        }

        //取消界面跳轉時的動畫,使啓動頁的logo圖片與註冊、登錄主頁的logo圖片完美銜接
        overridePendingTransition(0, 0);
    }

    /**
     * 屏蔽物理返回鍵
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        if (handler != null) {
            //If token is null, all callbacks and messages will be removed.
            handler.removeCallbacksAndMessages(null);
        }
        super.onDestroy();
    }
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    static void setStatusBarColor(Activity activity, int statusColor) {
        Window window = activity.getWindow();
        //取消狀態欄透明
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //添加Flag把狀態欄設爲可繪製模式
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        //設置狀態欄顏色
        window.setStatusBarColor(statusColor);
        //設置系統狀態欄處於可見狀態
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        //讓view不根據系統窗口來調整自己的佈局
        ViewGroup mContentView = (ViewGroup) window.findViewById(Window.ID_ANDROID_CONTENT);
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            ViewCompat.setFitsSystemWindows(mChildView, false);
            ViewCompat.requestApplyInsets(mChildView);
        }
    }
    static void setStatusBarColor4_4(Activity activity, int statusColor) {
        Window window = activity.getWindow();
        ViewGroup mContentView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);

//First translucent status bar.
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        int statusBarHeight = getStatusBarHeight(activity);

        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mChildView.getLayoutParams();
            //如果已經爲 ChildView 設置過了 marginTop, 再次調用時直接跳過
            if (lp != null && lp.topMargin < statusBarHeight && lp.height != statusBarHeight) {
                //不預留系統空間
                ViewCompat.setFitsSystemWindows(mChildView, false);
                lp.topMargin += statusBarHeight;
                mChildView.setLayoutParams(lp);
            }
        }

        View statusBarView = mContentView.getChildAt(0);
        if (statusBarView != null && statusBarView.getLayoutParams() != null && statusBarView.getLayoutParams().height == statusBarHeight) {
            //避免重複調用時多次添加 View
            statusBarView.setBackgroundColor(statusColor);
            return;
        }
        statusBarView = new View(activity);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight);
        statusBarView.setBackgroundColor(statusColor);
//向 ContentView 中添加假 View
        mContentView.addView(statusBarView, 0, lp);
    }

    public String getUser_id(){
        SharedPreferences sp;
        sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        return sp.getString("id","");
    }
    //獲取保存的用戶名
    public String getUser_name(){
        SharedPreferences sp;
        sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        return sp.getString("username","");
    }
    //獲取保存的密碼
    public String getUser_password(){
        SharedPreferences sp;
        sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        return sp.getString("userpass","");
    }
    public boolean getUser_remember(){
        SharedPreferences sp;
        sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        return sp.getBoolean("remember",false);
    }
    public void showToast(String content){
        Toast.makeText(this,content,Toast.LENGTH_SHORT).show();
    }
}

二、啓動界面佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorLogin"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:scaleType="fitCenter"
        app:srcCompat="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:textSize="18sp"
        android:gravity="center"
        android:padding="20dp"
        android:text="啓動" />
    
</LinearLayout>

三、修改啓動頁面(manifest)

<activity
     android:name=".Activity.StartActivity"
     android:theme="@style/AppTheme.Launcher">
     <intent-filter>
           <action android:name="android.intent.action.MAIN" />

           <category android:name="android.intent.category.LAUNCHER" />
     </intent-filter>
</activity>

四、啓動頁面主題(styles.xml)

<style name="AppTheme.Launcher" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- 通過windowBackground可以設置背景色、背景圖片、能解析出圖片的XML文件等-->
        <!--<item name="android:windowBackground">@drawable/layer_launcher</item>-->
        <item name="windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowBackground">@color/back_red</item>
        <item name="android:windowIsTranslucent">true</item>
</style>


五、推薦一個動畫效果開源庫
GitHub:Lottie
參考使用:Lottie使用


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章