Fade in animation while loading image Using Picasso

前端 未结 3 1075
失恋的感觉
失恋的感觉 2021-01-07 19:26

I want to show a fade effect when image is loading on Imageview. I am using picasso to cache image and display in image view. I have searched alot for this but couldnt find

相关标签:
3条回答
  • 2021-01-07 19:47

    You can simply do

    Picasso.with(context).load(url).fetch(new Callback(){
                @Override
                public void onSuccess() {
                    imageView.setAlpha(0f);
                    Picasso.with(context).load(url).into(imageView);
                    imageView.animate().setDuration(300).alpha(1f).start();
                }
    
                @Override
                public void onError() {
    
                }
            });
    
    0 讨论(0)
  • 2021-01-07 19:54

    I do this:

    Picasso.get().load(url).fit().noFade().centerInside().into(imageView, new Callback() {
    
                    @Override
                    public void onSuccess() {
                        imageView.setAlpha(0f);
                        imageView.animate().setDuration(200).alpha(1f).start();
                    }
    
                    @Override
                    public void onError(Exception e) {
                    }
                });
    
    0 讨论(0)
  • 2021-01-07 20:10

    Quoting Jake Wharton's answer here:

    If the image comes from anywhere except the memory cache the fade should be automatically applied.

    If you check the PicassoDrawable class

    boolean fade = loadedFrom != MEMORY && !noFade;
    if (fade) {
      this.placeholder = placeholder;
      animating = true;
      startTimeMillis = SystemClock.uptimeMillis();
    }
    .
    .
    .
    @Override public void draw(Canvas canvas) {
    if (!animating) {
      super.draw(canvas);
    } else {
    .
    .
    .
    

    fade effect is already applied for images loaded from n/w and not memory/cache and FADE_DURATION = 200f; //ms

    To force fade, again quoting jake wharton's answer here:

    You can specify noFade() and then always play an animation in the image loaded callback. You can also rely on the callback being called synchronously to determine if an animation needs played.

    final AtomicBoolean playAnimation = new AtomicBoolean(true);
    Picasso.with(context).load(..).into(imageView, new Callback() {
      @Override public void onLoad() {
        if (playAnimation.get()) {
          //play fade
          Animation fadeOut = new AlphaAnimation(0, 1);
          fadeOut.setInterpolator(new AccelerateInterpolator());
          fadeOut.setDuration(1000);
          imageView.startAnimation(fadeOut);
    
          Animation fadeOutPlaceholder = new AlphaAnimation(1, 0);
          fadeOutPlaceholder.setInterpolator(new AccelerateInterpolator());
          fadeOutPlaceholder.setDuration(1000);
          placeHolderImageView.startAnimation(fadeOutPlaceholder);
        }
      }
      //..
    });
    playAnimation.set(false);
    
    0 讨论(0)
提交回复
热议问题