Android Studio中使用Picasso加载gif图片作为占位符

10

如何在Picasso占位符中显示gif加载图像?

我想在代码的这个部分使用gif。

imageView = (ImageView) rootView.findViewById(R.id.imageView);
Picasso.with(getActivity()).load("http://joehamirbalabadan.com/android/android/imghome/index1.png").placeholder(R.drawable.indexloading).into(imageView);
imageView3 = (ImageView) rootView.findViewById(R.id.imageView3);
Picasso.with(getActivity()).load("http://joehamirbalabadan.com/android/android/imghome/index3.png").placeholder(R.drawable.indexloading).into(imageView3);

请检查并改进我的代码。

HomeFragment.java

package com.example.administrator.mosbeau;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.squareup.picasso.Picasso;

/**
 * Created by Administrator on 9/7/2015.
 */
public class HomeFragment extends Fragment {

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    public HomeFragment () {
    }

    Boolean InternetAvailable = false;
    Seocnd detectconnection;

    ImageView imageView, imageView3;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.homelayout, container, false);

        detectconnection = new Seocnd(getActivity());
        InternetAvailable = detectconnection.InternetConnecting();
        if (InternetAvailable) {

            imageView = (ImageView) rootView.findViewById(R.id.imageView);
            Picasso.with(getActivity()).load("http://joehamirbalabadan.com/android/android/imghome/index1.png").placeholder(R.drawable.indexloading).into(imageView);

            imageView3 = (ImageView) rootView.findViewById(R.id.imageView3);
            Picasso.with(getActivity()).load("http://joehamirbalabadan.com/android/android/imghome/index3.png").placeholder(R.drawable.indexloading).into(imageView3);


        } else {
            NointernetFragment fragment = new NointernetFragment();
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.container, fragment)
                    .commit();
        }

        return rootView;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(1);
    }

}

homelayout.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:fillViewport="false"
    android:background="#fffff1f1">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:background="#fffff1f1"
    android:padding="10dp">



    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@drawable/index1"
        android:layout_alignParentEnd="false"
        android:layout_alignParentStart="false"
        android:layout_alignParentTop="false"
        android:layout_alignParentLeft="false"
        android:layout_alignParentRight="false"
        android:layout_alignWithParentIfMissing="false"
        android:adjustViewBounds="true"
        android:layout_marginBottom="10dp"
        android:layout_centerHorizontal="true"
        android:background="#ffffffff" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView2"
        android:src="@drawable/index2"
        android:layout_below="@+id/imageView"
        android:adjustViewBounds="true"
        android:layout_marginBottom="10dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView3"
        android:src="@drawable/index3"
        android:layout_below="@+id/imageView2"
        android:layout_alignParentBottom="true"
        android:adjustViewBounds="true"
        android:background="#ffffffff"
        android:layout_centerHorizontal="true" />

</RelativeLayout>
</ScrollView>
4个回答

14

据我所知,Android没有内置支持GIF的功能,因此ImageView默认不支持GIF。

我建议您使用Glide库进行图像加载和缓存,因为它提供了对GIF的支持。Glide与Picasso类似,有时被认为比Picasso更好。使用的方法也类似于Picasso,只是它具有一个asGif()方法,可以将图像加载到ImageView中作为GIF。

Glide.with(context)
    .load(imageUrl)
    .asGif()
    .placeholder(R.drawable.loading_gif)
    .into(imageView);

如果你非常想使用Picasso本身,那么你可能需要查看这个stackoverflow帖子


1
我现在使用Glide,但问题是loading_gif在占位符中无法工作。我想要在占位符中使用GIF,这样加载图像将会先显示,然后才是URL中的图像。 - Joe
我在使用Glide加载gif时遇到了另一个问题。其中一个帧缺失,导致动画出现故障。我尝试了https://github.com/koral--/android-gif-drawable,并且它对我来说效果很好,但它不是ImageView。 - Yazon2006
@Joe 我正在使用 Glide 4.11.0,它运行良好 :-) - JWL

1

Picaso是用于ImageView的,您可以在WebView中显示GIF文件,但您肯定不能在Picaso中使用该WebView。


0

0

我使用Glide来实现,只需将带有gif扩展名的URL放入其中即可。

  Glide.with(getApplicationContext()).load("http://URL/estado2.gif").into(imageView);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接