Android Studio 如何在Java中编程添加Lottie动画

ej83mcc0  于 2022-12-23  发布在  Android
关注(0)|答案(4)|浏览(191)

我尝试将Lottie Animation以编程方式添加到Java(Android)中,但一直失败。我将在下面显示我的代码,我需要从可绘制更改为Lottie。
下面是可绘制对象的代码:

if (status.equals("connect")) {

            vpnBtn.setBackgroundResource(R.drawable.but_connect_light);
            logTv.setTextColor(getResources().getColor(R.color.font_color));

        } else if (status.equals("connecting")) {

            vpnBtn.setBackgroundResource(R.drawable.but_connecting_dark);
            logTv.setTextColor(getResources().getColor(R.color.font_color));

        } else if (status.equals("connected")) {

            vpnBtn.setBackgroundResource(R.drawable.but_disconnect_dark);
            logTv.setTextColor(getResources().getColor(R.color.purple));

        } else if (status.equals("failed")) {

            vpnBtn.setBackgroundResource(R.drawable.but_connect_dark);

        }

这是上面相关的xml

<ImageView
        android:id="@+id/vpnBtn"
        android:layout_alignParentBottom="true"
        android:layout_gravity="center_horizontal"
        android:tag="1"
        android:layout_width="140dp"
        android:layout_height="140dp"
        android:background="@drawable/but_connect_light"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="50dp"/>
cbwuti44

cbwuti441#

以上解决方案对我不起作用。它使应用程序崩溃。
试试用这个
第一个月
或者把它加到无穷大的答案上
字符串vpnsBtn;
if(检查.等于("夜间")){

switch (status) {
        case "connect":

            vpnBtn.setVisibility(View.VISIBLE);
            vpnsBtn = R.raw.disconnected;
            logTv.setTextColor(getResources().getColor(R.color.font_color));

            break;
        case "connecting":

            vpnBtn.setVisibility(View.VISIBLE);
            vpnsBtn = R.raw.connectingg;
            logTv.setTextColor(getResources().getColor(R.color.font_color));

            break;
        case "connected":

            vpnBtn.setVisibility(View.VISIBLE);
            vpnsBtn = R.raw.connected
            logTv.setTextColor(getResources().getColor(R.color.purple));

            break;
        case "failed":

            vpnBtn.setVisibility(View.VISIBLE);
            vpnsBtn = R.raw.disconnected;

            break;
    }

}else {

    switch (status) {
        case "connect":

            vpnBtn.setVisibility(View.VISIBLE);
            vpnsBtn = R.raw.disconnected;
            logTv.setTextColor(getResources().getColor(R.color.font_color));

            break;
        case "connecting":

            vpnBtn.setVisibility(View.VISIBLE);
            vpnsBtn = R.raw.connectingg;;
            logTv.setTextColor(getResources().getColor(R.color.font_color));

            break;
        case "connected":

            vpnBtn.setVisibility(View.VISIBLE);
            vpnsBtn = R.raw.connected;;
            logTv.setTextColor(getResources().getColor(R.color.purple));

            break;
        case "failed":

            vpnBtn.setVisibility(View.VISIBLE);
            vpnsBtn = R.raw.disconnected;

            break;
    }
}

LottieAnimationView animationView = findViewById(R.id.vpnBtn);
animationView.setImageAssetsFolder("vpnconnect");
animationView.setAnimation(vpnsBtn);
animationView.loop(true);
animationView.playAnimation();`
niknxzdl

niknxzdl2#

最后我用下面的解决方案实现了这个

String vpnsBtn;

if (check.equals("night")){

        switch (status) {
            case "connect":

                vpnBtn.setVisibility(View.VISIBLE);
                vpnsBtn = ("disconnected.json");
                logTv.setTextColor(getResources().getColor(R.color.font_color));

                break;
            case "connecting":

                vpnBtn.setVisibility(View.VISIBLE);
                vpnsBtn = ("connectingg.json");
                logTv.setTextColor(getResources().getColor(R.color.font_color));

                break;
            case "connected":

                vpnBtn.setVisibility(View.VISIBLE);
                vpnsBtn = ("connected.json");
                logTv.setTextColor(getResources().getColor(R.color.purple));

                break;
            case "failed":

                vpnBtn.setVisibility(View.VISIBLE);
                vpnsBtn = ("disconnected.json");

                break;
        }

    }else {

        switch (status) {
            case "connect":

                vpnBtn.setVisibility(View.VISIBLE);
                vpnsBtn = ("disconnected.json");
                logTv.setTextColor(getResources().getColor(R.color.font_color));

                break;
            case "connecting":

                vpnBtn.setVisibility(View.VISIBLE);
                vpnsBtn = ("connectingg.json");
                logTv.setTextColor(getResources().getColor(R.color.font_color));

                break;
            case "connected":

                vpnBtn.setVisibility(View.VISIBLE);
                vpnsBtn = ("connected.json");
                logTv.setTextColor(getResources().getColor(R.color.purple));

                break;
            case "failed":

                vpnBtn.setVisibility(View.VISIBLE);
                vpnsBtn = ("disconnected.json");

                break;
        }
    }

    LottieAnimationView animationView = findViewById(R.id.vpnBtn);
    animationView.setImageAssetsFolder("vpnconnect");
    animationView.setAnimation(vpnsBtn);
    animationView.loop(true);
    animationView.playAnimation();

希望这将有助于其他谁想要使用动态按钮与洛蒂在Android上。

vsikbqxv

vsikbqxv3#

首次使用此依赖项

implementation 'com.airbnb.android:lottie:3.6.1'

在xml中添加LottieAnimationView

<com.airbnb.lottie.LottieAnimationView       
        android:id="@+id/animation"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        app:lottie_autoPlay="true"
        app:lottie_fileName="loading.json"
        app:lottie_loop="true"
        app:lottie_speed="1" />

根据用途更改动画:

animation.setAnimation("loading.json");

:在asset文件夹中添加您的lottie animation json文件。

bzzcjhmw

bzzcjhmw4#

为了使用Lottie,我做了以下操作:
build.gradle(:app)中添加以下内容并同步:

implementation 'com.airbnb.android:lottie:$lottieVersion'

然后,添加assets文件。然后,在那里添加JSON文件。然后,转到activity_main.xml文件,添加:

<com.airbnb.lottie.LottieAnimationView
            android:id="@+id/lav_actionBar"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:layout_marginStart="0dp"
            android:layout_marginTop="0dp"
            android:layout_marginEnd="0dp"
            android:scaleType="centerCrop"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:lottie_autoPlay="true"
            app:lottie_fileName="circle-load.json" --> add your JSON file name 
            app:lottie_loop="true" />

相关问题