【问题标题】:How to play gif in android from url?如何从 url 在 android 中播放 gif?
【发布时间】:2016-04-06 09:31:31
【问题描述】:

我想在 android 应用程序(如 imgur 应用程序)中从 url 播放动画 gif。 Imgur 非常棒,而且速度非常快。我正在使用 webview 加载 gif,但它没有达到标准。

【问题讨论】:

标签: android animated-gif


【解决方案1】:

您可以将 imageview 设置为:

           <com.example.androidgif.GifView
            android:id="@+id/gifview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

按照本教程,您可以做到:

http://android-er.blogspot.de/2014/03/load-animated-gif-from-internet.html

【讨论】:

    【解决方案2】:

    您可以使用GlideImageView 上播放 gif。所以,让我们将它添加到您应用的 gradle 中:

    repositories {
        mavenCentral()
    }
    
    dependencies {
        compile 'com.github.bumptech.glide:glide:3.6.1'
        compile 'com.android.support:support-v4:23.1.1'
    }
    

    然后,创建一个ImageView

    <ImageView
        android:id="@+id/imageView"
        android:contentDescription="@string/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    

    在你的活动中:

    Glide  
        .with(context) // replace with 'this' if it's in activity
        .load("http://www.google.com/.../image.gif")
        .asGif()
        .error(R.drawable.error_image) // show error drawable if the image is not a gif
        .into(R.id.imageView);
    

    欲了解更多信息,请打开此帖子Glide — Displaying Gifs & Videos

    【讨论】:

    • 谢谢它的工作,但需要时间。我需要快速输出。
    • @user3606902,您可以使用WebView 代替:stackoverflow.com/a/21018730/3922207
    • 这需要时间,因为它首先会被下载到缓存中。如果您想要快速输出,请将其打包到您的资产文件夹中的 apk 中并从那里访问它。
    【解决方案3】:

    直接从 URL 到应用布局显示动画 GIF 的简单方法是使用 WebView 类。

    第 1 步:在您的布局 XML 中

    <WebView
    android:id="@+id/webView"
    android:layout_width="50dp"
    android:layout_height="50dp"
    />

    第 2 步:在您的活动中

    WebView wb;
    wb = (WebView) findViewById(R.id.webView);
    wb.loadUrl("https://.......);

    第 3 步:在您的 Manifest.XML 中设置 Internet 权限

    &lt;uses-permission android:name="android.permission.INTERNET" /&gt;

    第 4 步:如果您想让 GIF 背景透明并使 GIF 适合您的布局

    wb.setBackgroundColor(Color.TRANSPARENT);
    wb.getSettings().setLoadWithOverviewMode(true);
    wb.getSettings().setUseWideViewPort(true);

    【讨论】:

      【解决方案4】:

      大家好,请问如何在 android studio 中从 url 显示 gif 只需按照我的引导:

      首先我们要使用glide库所以我们需要实现依赖

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

      第二个同步你的 gradle 之后要做的事情是去你的 xml 并创建一个新的 imageView

       <ImageView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/image_id"
          android:layout_centerInParent="true"/>
      

      finally 转到您的 mainActivity 或与此 xml 相关的活动: 首先声明 ImageView 并像这样在 onCreate 中找到 id:

      public ImageView imageView;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          
          setContentView(R.layout.activity_main);
          imageView=findViewById(R.id.image_id);
      

      最后一件事是使用 glide 库从 Url 显示 gif 是的:

       Glide.with(this).load("https://media.giphy.com/media/xT5LMJmXRmrn3ft04w/giphy.gif").into(imageView);
      

      就是这样但是如果您想使用可绘制的 gif,请改用此行:

      Glide.with(this).load(R.drawable.imageGif).into(imageView);
      

      这是 Glide 库https://github.com/bumptech/glide

      【讨论】:

        猜你喜欢
        • 2014-12-29
        • 2011-11-10
        • 1970-01-01
        • 2016-04-28
        • 2019-08-18
        • 1970-01-01
        • 2012-03-26
        • 2013-12-23
        • 2022-11-14
        相关资源
        最近更新 更多