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使用


  • 4
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Android 应用程序的启动过程中,启动动画可以为用户带来更好的体验。实现启动动画的方法有很多种,以下是其中一种较为简单的实现方式: 1. 在 res/drawable 目录下创建一个 XML 文件,用于定义动画效果,例如 splash_animation.xml: ```xml <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true"> <item android:drawable="@drawable/splash1" android:duration="200" /> <item android:drawable="@drawable/splash2" android:duration="200" /> <item android:drawable="@drawable/splash3" android:duration="200" /> <item android:drawable="@drawable/splash4" android:duration="200" /> </animation-list> ``` 2. 在 res/layout 目录下创建一个布局文件,用于显示启动页的 Logo 和动画效果,例如 activity_splash.xml: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/logo" android:scaleType="centerCrop" /> </RelativeLayout> ``` 3. 在 SplashActivity 类中设置启动页的 Logo 和动画效果: ```java public class SplashActivity extends AppCompatActivity { private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); imageView = findViewById(R.id.imageView); imageView.setImageResource(R.drawable.splash_animation); AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable(); animationDrawable.start(); new Handler().postDelayed(new Runnable() { @Override public void run() { Intent intent = new Intent(SplashActivity.this, MainActivity.class); startActivity(intent); finish(); } }, 1000); } } ``` 在上述代码中,使用 AnimationDrawable 类来加载并播放动画效果,使用 Handler 类来延迟启动主界面。启动动画就完成了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值