2016-12-07 102 views
0

我有一個使用畢加索加載背景圖像的BackgroundImageActivity。但是,當我回到家中(在BackgroundImageActivity中調用onStop()時),並進入該活動的另一個實例,該活動應該加載另一個背景圖像,但是在開始2秒鐘後,它仍然顯示前一個BackgroundImageActivity的圖像。這是一種緩存嗎?如何在Android上清除Picasso加載的圖像?

如何清除此圖像,以便每當我進入BackgroundImageActivity的新實例時,我都看不到前一個圖像?

public class BackgroundImageActivity extends Activity { 

    @Override 
    public void onCreate(final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     initBackground(); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     Picasso.invalidate(imageUrl, getApplicationContext()); 
    } 

    private void initBackground() { 
     ... 
    } 

    private void setBg(final String imageUrl, final int bg) { 

     this.imageUrl = imageUrl; 

     final RequestCreator picassoRequest = Picasso.load(imageUrl, bg).memoryPolicy(MemoryPolicy.NO_CACHE); 

     targetReference = new Target() { 
      @Override 
      public void onBitmapLoaded(final Bitmap bitmap, final Picasso.LoadedFrom from) { 
       ... 
      } 

      @Override 
      public void onBitmapFailed(final Drawable errorDrawable) { 
       ... 
      } 

      @Override 
      public void onPrepareLoad(final Drawable placeHolderDrawable) { 
       ... 
      } 
     }; 

     picassoRequest.placeholder(bg).error(bg).into(targetReference); 
    } 


} 

謝謝!

回答

0

加載圖像時使用這段代碼。

Picasso.with(getContext()).load(data.get(pos).getFeed_thumb_image()).memoryPolicy(MemoryPolicy.NO_CACHE).into(image); 

picasso將圖像直接設置爲ImageView,而無需在背景中緩存。這使得應用程序也變得輕盈。

相關問題