使用MotionLayout 实现启动页动画 Splash

效果图

capture.gif

项目地址

具体代码实现

activity_splash.xml

<androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/_splashMotion"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:gravity="center"
    android:orientation="vertical"
    app:layoutDescription="@xml/activity_motion_scene">

    <ImageView
        android:id="@+id/main_image2"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@drawable/ic_sun"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="ContentDescription" />
    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/aplashTv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:lineSpacingExtra="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:text="" />


    <TextView
        android:id="@+id/app_name_by"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_marginBottom="12dp"
        android:text="@string/app_name_by "
        android:textColor="#dd000000"
        app:layout_goneMarginBottom="60dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:textSize="14sp"/>

</androidx.constraintlayout.motion.widget.MotionLayout>

xml/activity_motion_scene/xml

<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto">

    <Transition
        motion:constraintSetEnd="@id/end"
        motion:constraintSetStart="@id/start"
        motion:duration="4000"
        motion:motionInterpolator="easeInOut">
        <OnClick
            motion:clickAction="toggle"
            motion:targetId="@id/main_image2"
            motion:dragDirection="dragRight" />
        <KeyFrameSet>
            <KeyTimeCycle
                motion:motionTarget="@id/main_image2"
                motion:motionProgress="60"
                motion:framePosition="50"
                motion:wavePeriod="43"
                motion:waveShape="sawtooth"
                motion:waveOffset="60"
                />
            <KeyPosition
                motion:framePosition="10"
                motion:keyPositionType="parentRelative"
                motion:motionTarget="@id/main_image2"
                motion:pathMotionArc="startHorizontal"
                motion:curveFit="spline"
                motion:percentX="2"
                motion:percentY="0" />
            <KeyPosition
                motion:framePosition="50"
                motion:keyPositionType="pathRelative"
                motion:motionTarget="@id/main_image2"
                motion:pathMotionArc="flip"
                motion:curveFit="spline"
                motion:percentX="1.6"
                motion:percentY="0.55"/>
            <KeyPosition
                motion:framePosition="85"
                motion:keyPositionType="pathRelative"
                motion:motionTarget="@id/main_image2"
                motion:pathMotionArc="flip"
                motion:curveFit="spline"
                motion:percentX="1.3"
                motion:percentY="-0.65"/>
            <KeyAttribute
                motion:motionTarget="@id/main_image2"
                motion:framePosition="30"
                android:rotationX="50"
                android:rotationY="50"/>

        </KeyFrameSet>
    </Transition>

    <ConstraintSet android:id="@+id/start">

        <Constraint
            android:id="@+id/main_image2"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:alpha="0"
            motion:drawPath="path"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintRight_toRightOf="parent"
            motion:layout_constraintTop_toTopOf="parent" />

        <Constraint
            android:id="@+id/app_name_by"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:alpha="0"
            motion:drawPath="path"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintLeft_toLeftOf="parent"
            motion:layout_constraintRight_toRightOf="parent"  />

    </ConstraintSet>

    <ConstraintSet android:id="@+id/end">
        <Constraint
            android:id="@+id/main_image2"
            android:layout_width="50dp"
            android:layout_height="50dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintLeft_toLeftOf="parent"
            motion:layout_constraintRight_toRightOf="parent"
            motion:layout_constraintTop_toTopOf="parent"  />
        <Constraint
            android:id="@+id/app_name_by"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="60dp"
            motion:layout_constraintBottom_toBottomOf="parent"
            motion:layout_constraintLeft_toLeftOf="parent"
            motion:layout_constraintRight_toRightOf="parent"   />

    </ConstraintSet>

</MotionScene>

SplashActivity

package com.e9ab98e991ab.motionsplash

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.motion.widget.MotionLayout
import kotlinx.android.synthetic.main.activity_splash.*

class SplashActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        }
        main_image2.post {
            main_image2.performClick()
        }
        _splashMotion.setTransitionListener(object : MotionLayout.TransitionListener {
            override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) {
            }

            override fun onTransitionChange(p0: MotionLayout?, p1: Int, p2: Int, p3: Float) {
            }

            override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) {
                startActivity(Intent(this@SplashActivity, MainActivity::class.java))
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
                this@SplashActivity.finish()
            }

            override fun onTransitionTrigger(p0: MotionLayout?, p1: Int, p2: Boolean, p3: Float) {
            }
        })
    }
}

实现原理

第一步:SplashActivity 中使用MotionLayout (id: _splashMotion)添加 ImageView (id: main_image2)

第二步:MotionScene->Transition添加 如下事件监听 并在MotionScene 布局中调整动画角度。

<OnClick
    motion:clickAction="toggle"
    motion:targetId="@id/main_image2"
    motion:dragDirection="dragRight" />

第三步 SplashActivity 中 执行 performClick()开始执行动画

main_image2.post {
    main_image2.performClick()
}

第四步 在SplashActivity(id: _splashMotion) 设置动画监听

_splashMotion.setTransitionListener(object : MotionLayout.TransitionListener {
	//开始时
    override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) {
    }

    override fun onTransitionChange(p0: MotionLayout?, p1: Int, p2: Int, p3: Float) {
    }
	//结束
    override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) {
        startActivity(Intent(this@SplashActivity, MainActivity::class.java))
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out)
        this@SplashActivity.finish()
    }

    override fun onTransitionTrigger(p0: MotionLayout?, p1: Int, p2: Boolean, p3: Float) {
    }
})
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在 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 类来延迟启动主界面。启动动画就完成了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Godery

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值