【问题标题】:Frame-by-Frame Animated Splash-Screen逐帧动画启动画面
【发布时间】:2019-12-03 04:35:54
【问题描述】:

[Android Studio]我试图让几帧一个接一个地快速显示,然后在启动时自动更改为不同的活动。

我可以让启动活动在继续之前等待适当的时间,但动画仍然没有播放。

我正在使用此人的方法 (https://www.youtube.com/watch?v=lGRWYJlS3d0) 为帧设置动画并将它们显示到 ImageView - 在示例中,动画由停止和开始按钮控制,该按钮将帧设置为无限循环(或不循环),但是我希望它在应用程序启动时被激活并显示。

我目前实现此方法的方式是在读取特定行后导致应用程序崩溃,但我可能只是做错了,因此我们将不胜感激。谢谢。 :)

AndroidManifest.xml

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

    <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/AppTheme">
        <activity android:name=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

                <category android:name="android.intent.category.MAIN" />
            </intent-filter>
        </activity>
    </application>
</manifest>

activity_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/splash_layout"
    android:background="@drawable/custom_animation">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center" />
    </FrameLayout>

</android.support.constraint.ConstraintLayout>

custom_animation.xml在drawables下

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/image0" android:duration="250"/>
    <item android:drawable="@drawable/image1" android:duration="100"/>
    <item android:drawable="@drawable/image2" android:duration="100"/>
    <item android:drawable="@drawable/image3" android:duration="100"/>
    <item android:drawable="@drawable/image4" android:duration="750"/>
    <item android:drawable="@drawable/image3" android:duration="750"/>
    <item android:drawable="@drawable/image4" android:duration="750"/>
    <item android:drawable="@drawable/image3" android:duration="500"/>
    <item android:drawable="@drawable/image0" android:duration="5000"/>
</animation-list>

SplashActivity.java

public class SplashActivity extends AppCompatActivity {

    AnimationDrawable anim;
    ImageView imageView;

    @Override

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        imageView = (ImageView) findViewById(R.id.imageView);

        imageView.setBackgroundResource(R.drawable.custom_animation);

        anim = (AnimationDrawable) imageView.getBackground();

        anim.start();

//  //  start new activity after 3.5 seconds  //  //

        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                startNextActivity();
            }
        }, 3500);
    }

    public void startNextActivity() {
        if (isFinishing())
            return;
        startActivity(new Intent(SplashActivity.this, MainActivity.class));
        finish();
    }

当我开始取消引用这些行时,往往会发生崩溃:

imageView = (ImageView) findViewById(R.id.imageView);

imageView.setBackgroundResource(R.drawable.custom_animation);

PS:如果有任何命名错误,可能是由于我试图使文件名更通用和易于理解,但请随时指出您认为可能导致问题的任何内容。 p>

【问题讨论】:

    标签: java android animation automation splash-screen


    【解决方案1】:

    您需要在活动类上设置布局,否则应用程序将不知道应该膨胀什么布局。这是当您尝试查找图像视图时应用程序崩溃的主要原因,因为您尚未设置内容布局。在 super.onCreate 之后和尝试操作任何 UI 元素之前添加此行

    setContentView(R.layout.activity_splash);
    

    此外,删除约束布局上的背景声明,您已经将动画可绘制对象加载到图像视图

    <android.support.constraint.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:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/splash_layout">  
    
        ... 
    
    </android.support.constraint.ConstraintLayout>
    

    【讨论】:

    • 知道这很简单。非常感谢你,我爱你。 :)
    猜你喜欢
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    相关资源
    最近更新 更多