【问题标题】:Android Studio Picasso gif loading image for placeholder占位符的 Android Studio Picasso gif 加载图像
【发布时间】:2015-09-30 08:03:36
【问题描述】:

如何在我的毕加索占位符中显示 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>

【问题讨论】:

    标签: android picasso


    【解决方案1】:

    据我所知,Android 没有内置对 GIF 的支持。所以ImageView默认不支持GIF

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

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

    如果您如此热衷于使用毕加索本身,那么您可能需要查看stackoverflow post

    【讨论】:

    • 我现在使用 Glide,但问题是 loading_gif 在占位符中不起作用..我希望 GIF 在占位符中,因此加载图像将首先显示在 url 中的图像之前..
    • 我在 Glide 加载 gif 时遇到了另一个问题。缺少一帧并且动画出现故障。我试过github.com/koral--/android-gif-drawable。它对我有用,但它不是 ImageView。
    • @Joe 正在使用 Glide 4.11.0,它运行良好 :-)
    【解决方案2】:

    picaso 用于图像视图,您可以在 webView 中显示 GIF 文件,但您不能在 picaso 中使用该 webview。

    【讨论】:

      【解决方案3】:

      我不确定毕加索。但是 ION 库具有内置的 GIF 支持。 https://github.com/koush/ion

      【讨论】:

        【解决方案4】:

        我用 Glide 做的,你把 url 加上扩展名 gif 就可以了

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-06-27
          • 2020-12-08
          • 2019-02-03
          • 2017-05-08
          • 1970-01-01
          • 1970-01-01
          • 2016-03-14
          • 1970-01-01
          相关资源
          最近更新 更多