【问题标题】:GIF doesn't appear when Android app starts在Android应用程序开始时,GIF不会出现
【发布时间】:2019-10-01 09:13:06
【问题描述】:

我在尝试在我的应用中使用 GIF 时遇到问题。当我运行我的应用程序时,它根本不会出现。我遵循了有关如何使其工作的教程,所以我不知道为什么它不工作。我也没有收到任何错误消息。所以,我的问题是,有人对为什么会发生这种情况有任何建议吗? 提前感谢您的帮助!

我的代码:

buildscript {

    repositories {
        mavenCentral()
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'



    }
}

allprojects 
{
    repositories
 {
        mavenCentral()
        google()
        jcenter()


    }
}


App build script:

dependencies {


    implementation  'pl.droidsonroids.gif:android-gif-drawable:1.2.16'
}

XML:
    <pl.droidsonroids.gif.GifImageView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        app:srcCompat="@drawable/gif" />

【问题讨论】:

  • 也许将vectorDrawables.useSupportLibrary = true 添加到您的默认配置中?无论如何,您可以添加您尝试过的代码吗?否则无法帮助你
  • 我看了多个视频,它们都有相同的程序。添加 mavenCentral() 和 'pl.droidsonroids.gif:android-gif-drawable:1.2.16' 库,然后更改 xml。那里的一切都是我所做的。
  • 您应该正确显示您的代码,xml 代码没有进入您的 Gradle 代码,对吗?而且xml代码不完整?您的活动代码在哪里?

标签: java android gif


【解决方案1】:

试试这个:

 XML:

   <pl.droidsonroids.gif.GifImageView
                android:id="@+id/gif_view_offer"
                android:layout_gravity="center"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_marginTop="20dp"
                android:src="@drawable/checkmark"
                android:layout_marginBottom="10dp"/>




 JAVA:

       GifImageView gifView;
                GifDrawable gifFromResource = null;

                gifView = (GifImageView) v.findViewById(R.id.gif_view_offer);
                try {
                    gifFromResource = new GifDrawable(activity.getResources(), R.drawable.lockgif);
                    gifView.setImageDrawable(gifFromResource);
                } catch (IOException e) {
                    gifView.setVisibility(View.GONE);
                    e.printStackTrace();
                }

【讨论】:

  • 成功了!只需将我的 xml 更改为您共享的 xml 即可。很奇怪为什么它以前不起作用。感谢您抽出宝贵时间回复您!
  • app:srcCompat="@drawable/gif" 这是罪魁祸首...如果我的回答对您有帮助,那么您可以接受,谢谢。
【解决方案2】:

我强烈建议您改用Lottie。它还支持不同类型的格式和自动播放功能。

https://github.com/airbnb/lottie-android

<com.airbnb.lottie.LottieAnimationView
            android:id="@+id/lavSplash"
            app:lottie_rawRes="@raw/splash"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            style="@style/Widget.Custom.Lottie"/>

<style name="LottieAppearance">
        <item name="lottie_loop">true</item>
        <item name="lottie_autoPlay">true</item>
        <item name="android:layout_width">@dimen/wrap_content</item>
        <item name="android:layout_height">@dimen/wrap_content</item>
</style>

<style name="Widget.Custom.Lottie" parent="LottieAppearance" />

【讨论】:

    猜你喜欢
    • 2012-09-18
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-28
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多