You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Android启动屏的两个Lottie动画自适应居中并精准定位?

Android启动屏Lottie动画布局适配方案

针对你遇到的两个核心问题:title_animation视觉不居中需上移30dp、title需定位在title_animation下方约1/3位置,推荐使用ConstraintLayout重构布局(比RelativeLayout更适配多屏幕场景),方案如下:

核心优化点

  • 解决Lottie动画内容偏移:调整scaleType确保动画内容在视图容器内居中,再通过位移修正整体位置
  • 精准控制title位置:利用ConstraintLayout的相对约束+比例设置,实现跨屏幕的一致布局

完整布局代码

<androidx.constraintlayout.widget.ConstraintLayout 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/activity_splash"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/primary_color"
    tools:context=".activities.ActivitySplash.ActivitySplash">

    <!-- 图形动画:先居中,再上移30dp,同时修正动画内容居中 -->
    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/title_animation"
        android:layout_width="match_parent"
        android:layout_height="460dp"
        android:scaleType="centerInside" <!-- 替代centerCrop,确保动画内容在容器内居中 -->
        android:translationY="-30dp" <!-- 整体上移30dp,修正视觉偏移 -->
        app:lottie_autoPlay="false"
        app:lottie_loop="true"
        app:lottie_rawRes="@raw/title_anim"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <!-- 动画标题:定位在title_animation下方,距离为自身高度的1/3 -->
    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="336dp"
        android:layout_marginTop="112dp" <!-- 取336dp的1/3,可根据视觉调整 -->
        app:lottie_autoPlay="false"
        app:lottie_loop="false"
        app:lottie_rawRes="@raw/title_app"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title_animation" />

</androidx.constraintlayout.widget.ConstraintLayout>

关键细节说明

  1. title_animation调整

    • scaleType改为centerInside:避免centerCrop裁剪动画内容,确保动画本身居中显示在容器内
    • 添加android:translationY="-30dp":让整个视图上移30dp,直接修正视觉偏移问题
    • 双向居中约束:通过绑定parent的上下左右,确保视图在屏幕绝对居中
  2. title位置控制

    • 绑定到title_animation下方:通过layout_constraintTop_toBottomOf建立相对位置关联
    • 固定比例margin:设置112dp(自身高度的1/3),实现你需要的“下方约1/3位置”效果;如果要基于title_animation高度的比例,可替换为app:layout_constraintVertical_bias或百分比约束
  3. 适配增强建议

    • 若要适配更多屏幕尺寸,可把固定高度改为百分比约束,比如给title_animation设置app:layout_constraintHeight_percent="0.6"(占屏幕高度60%),避免小屏设备上动画被截断

若坚持使用RelativeLayout的替代方案

<RelativeLayout 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/activity_splash"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/primary_color"
    tools:context=".activities.ActivitySplash.ActivitySplash">

    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/title_animation"
        android:layout_width="match_parent"
        android:layout_height="460dp"
        android:layout_centerInParent="true"
        android:scaleType="centerInside"
        android:translationY="-30dp"
        app:lottie_autoPlay="false"
        app:lottie_loop="true"
        app:lottie_rawRes="@raw/title_anim" />

    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="336dp"
        android:layout_below="@+id/title_animation"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="112dp"
        app:lottie_autoPlay="false"
        app:lottie_loop="false"
        app:lottie_rawRes="@raw/title_app" />

</RelativeLayout>

注:该方案在极端屏幕(如折叠屏、小屏手机)下的适配稳定性不如ConstraintLayout。

内容的提问来源于stack exchange,提问作者Nick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 21:40:59