Android Picasso - 占位符和错误图像样式

sur*_*udi 37 android image-loading picasso

我正在使用Picasso库从网络上下载图像.我只是想知道我是否可以使用进度对话框或GIF图像作为占位符?还有任何想法如何使占位符图像比下载的实际图像更小(并适合中心)?

我试过通过样品,但没有运气.有谁在这里工作?

Chr*_*ris 94

您可以在Picasso调用中使用*.placeholder(R.drawable.image_name)*来使用占位符图像,如:

Picasso.with(context)
       .load(imageUrl)
       .placeholder(R.drawable.image_name)
Run Code Online (Sandbox Code Playgroud)

如果要使用进度条而不是占位符图像,可以在.into函数内创建回调.做类似以下的事情:

在你看来:

//create the progressBar here and set it to GONE. This can be your adapters list item view for example
<RelativeLayout
    android:id="@+id/myContainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">


    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="gone"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp"/>

    <ImageView
        android:id="@+id/myImageView"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:paddingLeft="60dp"
        android:scaleType="centerCrop"></ImageView>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

在你的适配器/类中:

//create a new progress bar for each image to be loaded
ProgressBar progressBar = null;
if (view != null) {
   progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
   progressBar.setVisibility(View.VISIBLE);
}

//get your image view
ImageView myImage = (ImageView) view.findViewById(R.id.myImageView);

//load the image url with a callback to a callback method/class
Picasso.with(context)
            .load(imageUrl)
            .into(myImage,  new ImageLoadedCallback(progressBar) {
                    @Override
                    public void onSuccess() {
                        if (this.progressBar != null) {
                            this.progressBar.setVisibility(View.GONE);
                         }
                    }
              });
Run Code Online (Sandbox Code Playgroud)

在上面的适配器/类中,我为回调创建了一个内部类:

private class ImageLoadedCallback implements Callback {
   ProgressBar progressBar;

    public  ImageLoadedCallback(ProgressBar progBar){
        progressBar = progBar;
    }

    @Override
    public void onSuccess() {

    }

    @Override
    public void onError() {

    }
}
Run Code Online (Sandbox Code Playgroud)

希望这是有道理的!

  • 很好的答案.不知道为什么OP还没有接受它. (4认同)
  • 这只是一个示例,您可以直接使用Callback,而不是使用内部类进行回调 (2认同)

Aka*_*ore 15

我实现了进度对话框,直到图像下载,非常简单.将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

                    }
                });
            }
Run Code Online (Sandbox Code Playgroud)

希望它会帮助别人!:)


小智 12

它显示进度条非常简单.

将此代码添加到活动以使用进度条加载图像.

Picasso.with(this)
            .load(hoteImageStr)
            .error(R.drawable.loading)
            .into(img,  new com.squareup.picasso.Callback() {
                @Override
                public void onSuccess() {
                    if (progress != null) {
                        progress .setVisibility(View.GONE);
                    }
                }

                @Override
                public void onError() {

                }
            });
Run Code Online (Sandbox Code Playgroud)

将其添加到布局文件中.

 <RelativeLayout
    android:id="@+id/hoteldetails_myContainer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   >


    <ProgressBar
        android:id="@+id/hoteldetails_progressBar"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:visibility="gone"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp" />

    <ImageView
        android:id="@+id/hoteldetails_imageView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY"
        />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)


Ham*_*mid 10

首先创建一个这样的可绘制布局:
progress_animation.xml:

<?xml version="1.0" encoding="utf-8"?>

<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/loading"
    android:pivotX="50%"
    android:pivotY="50%"/>
Run Code Online (Sandbox Code Playgroud)


添加 loading.png到drawable
loading.png
用法:

Picasso.with(context).load(imageUri).placeholder(R.drawable.progress_animation).into(imgView);
Run Code Online (Sandbox Code Playgroud)


小智 5

科特林

val circularProgressDrawable = CircularProgressDrawable(context)
circularProgressDrawable.strokeWidth = 5f
circularProgressDrawable.centerRadius = 30f
circularProgressDrawable.start()
Picasso.get().load(imageUrl).placeholder(circularProgressDrawable).into(holder.imageView)
Run Code Online (Sandbox Code Playgroud)

上面的代码消除了xml代码,这很干净。


tas*_*iac 1

除了 Picasso 库之外,您还应该设置 ProgressBar,并且应该使用侦听器手动管理它的可见性。

我正在使用 UniversalImageLoader 和 Picasso 库应该非常相似。

private ImageLoadingListener photoSuccess = new ImageLoadingListener() {

    @Override
    public void onLoadingStarted(String imageUri, View view) {
    }

    @Override
    public void onLoadingFailed(String imageUri, View view,
            FailReason failReason) {
        if(progress != null)
            progress.setVisibility(View.GONE);
    }

    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        if(progress != null)
            progress.setVisibility(View.GONE);
        if(mImage != null)
            mImage.setVisibility(View.VISIBLE);
    }

    @Override
    public void onLoadingCancelled(String imageUri, View view) {
    }
};
Run Code Online (Sandbox Code Playgroud)