如何从url在android中玩gif?

时间:2016-01-02 05:21:16

标签: android animated-gif

我想在Android应用中像urgur app一样从url播放动画gif。 Imagur非常棒,非常快。我正在使用webview加载gif,但它没有达到标记。

3 个答案:

答案 0 :(得分:12)

您可以使用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

答案 1 :(得分:0)

您可以将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 :(得分:0)

大家好,关于如何在android studio中显示来自网址的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"/>

最后进入您实现到此xml的mainActivity或活动: 首先声明ImageView并在onCreate中找到ID,如下所示:

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

最后一件事是使用滑行库从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