首页 > 解决方案 > 启动画面中的 GIF 不流畅

问题描述

我在我的启动画面(630kb,230 x 480)中实现了一个 Gif,它不是很流畅,在 MainActivity 启动前一秒钟它完全冻结了。

我正在使用 Glide 4.10

implementation 'com.github.bumptech.glide:glide:4.10.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'

我的 SplashScreen.class

public class SplashScreen extends Activity {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_splash);


        ImageView imageView = (ImageView) findViewById(R.id.splashgif);
        Glide.with(this)
                .asGif()
                .load(R.drawable.mygif)
                .into(imageView);




    Thread myThread = new Thread() {
        @Override
        public void run() {
            try {
                sleep(3000);
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);
                finish();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
      myThread.start();


    }
}

这是我的activity_splash.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/splash"
        android:scaleType="centerCrop"/>



    <ImageView
        android:id="@+id/splashgif"
        android:layout_width="wrap_content"
        android:layout_height="300dp"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        />

</RelativeLayout>

我的应用程序包含许多小图像,这些需要一些时间才能加载,这可能是我的启动画面运行不流畅的原因吗?我能做些什么或者我必须接受它?

标签: javaandroidsplash-screengifandroid-glide

解决方案


最终 ImageView imageView = (ImageView) findViewById(R.id.splashgif);

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Glide.with(getApplicationContext())
                    .asGif()
                    .load(R.drawable.mygif)
                    .into(imageView);
        }
    });

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(intent);
            finish();
        }
    }, 3000);

推荐阅读