Android stdio手机启动页面添加动画

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

1.创建项目
2.输入代码和图片
3.展示效果


1.创建项目

新建一个empty activity项目
在这里插入图片描述
随后打开Gradle Scripts 下的build.gradle进行配制更改
dependencies中代码大致如上

implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13.2'
    implementation'com.airbnb.android:lottie:3.7.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

2.输入代码和图片

创建新的empty activitiy
在这里插入图片描述
起好名字直接选择finish。随后对layout中新生成的xml文件中更改代码
具体代码如下

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#61656b"
    tools:context=".SplashScreen">

    <TextView
        android:id="@+id/appname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="25dp"
        android:textStyle="bold"
        android:text="Coding with me"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="20dp"/>
    <com.airbnb.lottie.LottieAnimationView
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:layout_centerInParent="true"
        app:lottie_autoPlay="true"
        android:id="@+id/lottie"
        android:elevation="5dp"
        app:lottie_rawRes="@raw/aaa"/>


</RelativeLayout>

上网下载或自行准备开场动画,这里博主下的是LottieFiles软件上的免费动画。
在这里插入图片描述
选好图片后在res文件下新建一个Android Resource Directory
在这里插入图片描述
其中resource type选择为raw,点击ok完成创建。
在这里插入图片描述
随后找到自己的动图直接拖拽到raw中。为了防止图片报错,可以选择文件重命名把图片前缀的数字全部删除。
在这里插入图片描述
随后打开layout下的activity_splash_screen.xml更改部分代码
该文件全部代码如下

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#61656b"
    tools:context=".SplashScreen">

    <TextView
        android:id="@+id/appname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="25dp"
        android:textStyle="bold"
        android:text="Coding with me"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="20dp"/>
    <com.airbnb.lottie.LottieAnimationView
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:layout_centerInParent="true"
        app:lottie_autoPlay="true"
        android:id="@+id/lottie"
        android:elevation="5dp"
        app:lottie_rawRes="@raw/aaa"/>



</RelativeLayout>

打开values文件下themes.xml,将其中parent内容更改,代码如下
在这里插入图片描述

<style name="Theme.Huang" parent="Theme.MaterialComponents.DayNight.NoActionBar">

打开class在这里插入图片描述
更改其中代码,全部代码如下

package com.example.android.huang;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;

public class SplashScreen extends AppCompatActivity {

    TextView appname;
    LottieAnimationView lottie;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        appname = findViewById(R.id.appname);
        lottie = findViewById(R.id.lottie);

        appname.animate().translationY(-1400).setDuration(2700).setStartDelay(0);
        lottie.animate().translationX(2000).setDuration(2000).setStartDelay(2900);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent(getApplicationContext(),MainActivity.class);
                startActivity(i);
            }
        }, 5000);
    }
}

最后对manifests下的AndrooidManifest.xml进行更改,
其全部代码如下

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.android.huang">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Huang">
        <activity
            android:name=".SplashScreen"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

            android:exported="false" />
        <activity
            android:name=".MainActivity"
            android:exported="true">

        </activity>
    </application>

</manifest>

3.展示效果

最后运行效果如下
在这里插入图片描述
杂谈:所有的文案和款式都可以在xml中进行更改,另外开场动画导入时使用gif后缀的动画似乎不能兼容,博主采用的是json的形式,即将动图后缀更改一下。

总结

以上就是今天要讲的内容,本文仅仅简单介绍了Android stdio如何给手机启动页面添加开场动画,希望能够对你有所帮助。

博客链接:https://blog.csdn.net/2401_84706339/article/details/144608693?spm=1001.2014.3001.5502

——罗郅丽

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值