Animated loading image in picasso

后端 未结 10 1561
醉酒成梦
醉酒成梦 2020-11-28 19:10

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 spin

相关标签:
10条回答
  • 2020-11-28 19:35

    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.

    compile 'com.squareup.picasso:picasso:2.5.2'
    
    0 讨论(0)
  • 2020-11-28 19:35

    In this link, Jake Wharton said :

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

    Then you can't

    0 讨论(0)
  • 2020-11-28 19:39

    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.

    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. :)

    0 讨论(0)
  • 2020-11-28 19:44

    @DBragion's answer was great. The problem with it (like many mentioned in the comments) was it didn't provide a way to specify the height / width of the progress indicator. Following is the modified version of his answer with combination of @AkankshaRathod's answer.

    loading_indicator.png

    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
                android:drawable="@drawable/loading_indicator"
                android:pivotX="50%"
                android:pivotY="50%" />
        </item>
    </layer-list>
    

    CardView (or any container Layout) containing your ImageView

    <androidx.cardview.widget.CardView
        android:id="@+id/vCategoryItem"
        android:layout_width="wrap_content"
        android:layout_height="@dimen/height_category_image"
        android:layout_marginLeft="@dimen/smaller"
        app:cardCornerRadius="@dimen/small">
    
        <ImageView
            android:id="@+id/imgCategoryImage"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:src="@drawable/demo" />
    
        <ImageView
            android:id="@+id/loadingIndicator"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_gravity="center"
            android:layout_centerInParent="true"
            android:src="@drawable/progress_animation" />
    
        <TextView
            android:id="@+id/lblCategoryName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_gravity="bottom"
            android:layout_marginBottom="@dimen/big"
            android:ellipsize="marquee"
            android:gravity="center"
            android:paddingLeft="@dimen/big"
            android:paddingRight="@dimen/big"
            android:singleLine="true"
            android:text="Category Name"
            android:textColor="@android:color/white"
            android:textSize="15sp"
            android:textStyle="bold" />
    
    </androidx.cardview.widget.CardView>
    

    Picasso loading:

    Picasso.get().load(yourImageURL).into(imgCategoryImage, new Callback() {
        @Override
        public void onSuccess() {
            loadingIndicator.setVisibility(View.GONE);
        }
    
        @Override
        public void onError(Exception e) {
    
        }
    });
    
    0 讨论(0)
提交回复
热议问题