Android 毕加索中的动画加载图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24826459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:49:44  来源:igfitidea点击:

Animated loading image in picasso

androidpicasso

提问by NZJames

I have the following code to load an image in Picasso, using a drawable for the placeholder to display while the image is downloading. What I want though is an animated spinning progress bar style spinner that animates around and around while the image is loading, like I see in most professional apps. Picasso doesn't seem to support this, only static image drawables. Is there a way to get it working with Picasso or do I have to do something different?

我有以下代码在 Picasso 中加载图像,使用可绘制的占位符在图像下载时显示。不过,我想要的是一个动画旋转进度条样式的微调器,它可以在图像加载时四处动画,就像我在大多数专业应用程序中看到的那样。毕加索似乎不支持这一点,只有静态图像可绘制。有没有办法让它与毕加索一起工作,还是我必须做一些不同的事情?

Picasso.with(context).load(url)             
                    .placeholder(R.drawable.loading)
                    .error(R.drawable.image_download_error)
                    .into(view);

回答by DBragion

How to have a loading progress animation image using Picasso placeholder:

如何使用 Picasso 占位符获得加载进度动画图像:

I solved this easily using a animated-rotate xml object.

我使用动画旋转 xml 对象轻松解决了这个问题。

Steps:

脚步:

progress_image.png

progress_image.png

progress_image.png

progress_image.png

/res/drawable/progress_animation.xml

/res/drawable/progress_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:gravity="center">
    <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:drawable="@drawable/progress_image"
        android:pivotX="50%"
        android:pivotY="50%" />
    </item>
</layer-list>

Picasso loading:

毕加索加载:

Picasso.with( context )
        .load( your_path )
        .error( R.drawable.ic_error )
        .placeholder( R.drawable.progress_animation )
        .into( image_view );

回答by Akanksha Rathore

I implemented the progress dialog till the image download and its very simple. Take your ImageView in relative layout and place progress loader on same image view. Apply the below code and handle progress dialog visibility only.

我实现了进度对话框,直到图像下载并且非常简单。将您的 ImageView 置于相对布局中,并将进度加载器放置在同一图像视图上。应用以下代码并仅处理进度对话框可见性。

holder.loader.setVisibility(View.VISIBLE);
            holder.imageView.setVisibility(View.VISIBLE);
         final ProgressBar progressView = holder.loader;
            Picasso.with(context)
            .load(image_url)
            .transform(new RoundedTransformation(15, 0))
            .fit()
            .into(holder.imageView, new Callback() {
                @Override
                public void onSuccess() {
                    progressView.setVisibility(View.GONE);
                }

                @Override
                public void onError() {
                    // TODO Auto-generated method stub

                }
            });
        }

Enjoy. :)

享受。:)

回答by Michael

Picasso doesn't support animated placeholders unfortunately.

不幸的是,毕加索不支持动画占位符。

One way you could work around this is to place your ImageView in a FrameLayout with the animated drawable underneath. This way when Picasso loads the image it will load over the top of the animated placeholder, giving the user the intended effect.

您可以解决此问题的一种方法是将您的 ImageView 放置在 FrameLayout 中,下面是动画可绘制对象。这样,当毕加索加载图像时,它将加载到动画占位符的顶部,为用户提供预期的效果。

Alternatively, you could load the image into a Target. Then you'd have the progress bar showing by default, and when the onBitmapLoaded method is called you can hide it and display the image. You can see a basic implementation of this here

或者,您可以将图像加载到Target 中。然后你会默认显示进度条,当 onBitmapLoaded 方法被调用时,你可以隐藏它并显示图像。你可以在这里看到一个基本的实现

回答by Tushar Lathiya

Just add shape attribute in DBragion's answer as like below and it will work like charm. Happy coding.

只需在 DBragion 的答案中添加 shape 属性,如下所示,它会像魅力一样工作。快乐编码。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>

        <shape
            android:shape="rectangle">

            <size
                android:width="200dp"
                android:height="200dp"/>

            <solid
                android:color="#00FFFFFF"/>
        </shape>
    </item>

    <item android:gravity="center">
        <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
            android:drawable="@drawable/spinner"
            android:pivotX="50%"
            android:pivotY="50%" />
    </item>
</layer-list>

You can use Glide too:

你也可以使用 Glide:

Glide.with(activity).load(url)
                    .apply(RequestOptions.centerInsideTransform())
                    .placeholder(R.drawable.progress_animation)
                    .into(imageview);

回答by Yusuke Yonehara

I found answer to this question!

我找到了这个问题的答案!

See: https://github.com/square/picasso/issues/427#issuecomment-266276253

见:https: //github.com/square/picasso/issues/427#issuecomment-266276253

In addition to answer of @DBragion, try below.

除了@DBragion 的回答,请尝试以下。

Now we can fix height and width!

现在我们可以固定高度和宽度了!

image_view.scaleType = ImageView.ScaleType.CENTER_INSIDE
picasso.load(your_path)
        .fit()
        .centerCrop()
        .noFade()
        .placeholder(R.drawable. progress_animation)
        .into(image_view)

I think there are two key points.

我认为有两个关键点。

  1. use noFade()

  2. set image_view's scaleType to "CENTER_INSIDE"

  1. 使用 noFade()

  2. 将 image_view 的 scaleType 设置为“CENTER_INSIDE”

回答by Exel Staderlin

Just use Picasso PlaceHolder

只需使用毕加索占位符

  Picasso.get()
        .load(datas[position].artist?.image)
        .placeholder(R.drawable.music_image)
        .error(R.drawable.music_image)
        .into(holder.cardViewImage)

回答by Dmytro Karataiev

I made it working in following way:

我使它按以下方式工作:

  1. Created a drawable (found somewhere on the Internet) and put it in the res/drawable:

    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="90"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360">
    
    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:thicknessRatio="7.0">
    
        <gradient
            android:angle="0"
            android:centerColor="#007DD6"
            android:endColor="#007DD6"
            android:startColor="#007DD6"
            android:type="sweep"
            android:useLevel="false" />
    </shape>
    

  2. In my item for the GridView added ProgressBar element:

    <ProgressBar
        android:id="@+id/movie_item_spinner"
        android:layout_width="@dimen/poster_width"
        android:layout_height="@dimen/poster_height"
        android:progressDrawable="@drawable/circular_progress_bar"/>
    
  3. In Adapter added Target Object with following parameters, where poster and spinner are references to ImageView and ProgressBar:

    // Target to show/hide ProgressBar on ImageView

    final Target target = new Target() {
    
            @Override
            public void onPrepareLoad(Drawable drawable) {
                poster.setBackgroundResource(R.color.white);
                spinner.setVisibility(View.VISIBLE);
            }
    
            @Override
            public void onBitmapLoaded(Bitmap photo, Picasso.LoadedFrom from) {
                poster.setBackgroundDrawable(new BitmapDrawable(photo));
                spinner.setVisibility(View.GONE);
            }
    
            @Override
            public void onBitmapFailed(Drawable drawable) {
                poster.setBackgroundResource(R.color.white);
            }
        };
    
  4. Results will be like that on loading - circle is rotating (sorry for this screenshot, but images are appearing too fast): ScreenShot

  1. 创建了一个 drawable(在 Internet 某处找到)并将其放入 res/drawable:

    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="90"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360">
    
    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:thicknessRatio="7.0">
    
        <gradient
            android:angle="0"
            android:centerColor="#007DD6"
            android:endColor="#007DD6"
            android:startColor="#007DD6"
            android:type="sweep"
            android:useLevel="false" />
    </shape>
    

  2. 在我为 GridView 添加的 ProgressBar 元素的项目中:

    <ProgressBar
        android:id="@+id/movie_item_spinner"
        android:layout_width="@dimen/poster_width"
        android:layout_height="@dimen/poster_height"
        android:progressDrawable="@drawable/circular_progress_bar"/>
    
  3. 在 Adapter 中添加了具有以下参数的 Target Object,其中 poster 和 spinner 是对 ImageView 和 ProgressBar 的引用:

    // 目标在 ImageView 上显示/隐藏 ProgressBar

    final Target target = new Target() {
    
            @Override
            public void onPrepareLoad(Drawable drawable) {
                poster.setBackgroundResource(R.color.white);
                spinner.setVisibility(View.VISIBLE);
            }
    
            @Override
            public void onBitmapLoaded(Bitmap photo, Picasso.LoadedFrom from) {
                poster.setBackgroundDrawable(new BitmapDrawable(photo));
                spinner.setVisibility(View.GONE);
            }
    
            @Override
            public void onBitmapFailed(Drawable drawable) {
                poster.setBackgroundResource(R.color.white);
            }
        };
    
  4. 结果将与加载时类似 - 圆圈正在旋转(抱歉此屏幕截图,但图像显示太快): ScreenShot

回答by Jesse Abruzzo

For anyone trying to use DBragion's technique: Make sure you have the latest version of Picasso or else it wont spin. Mine didn't work until I used version 2.5.2.

对于任何尝试使用 DBragion 技术的人:确保您拥有最新版本的 Picasso,否则它不会旋转。直到我使用 2.5.2 版,我的才工作。

compile 'com.squareup.picasso:picasso:2.5.2'

回答by Radesh

In this link, Jake Wharton said :

在这个链接中,Hyman沃顿说:

Nope. We use the Android BitmapFactory for all image decoding and it has no animated GIF support (sadly).

不。我们使用 Android BitmapFactory 进行所有图像解码,它没有动画 GIF 支持(遗憾的是)。

Then you can't

那你不能