安卓開發之使用Acache類對資料進行二級緩衝(記憶體+檔案)

來源:互聯網
上載者:User

Acache類下載地址:http://pan.baidu.com/s/1gfI0A9X

1、建立Acache執行個體

Acache acache;

acache = Acache.get(this, “main”);

2、將資料儲存起來(記憶體和檔案)

acache.put(key,value,time);

前兩個無需贅述,索引值對,最後一個time,傳入int類型,單位為秒,代表緩衝存在的時間,超出這個時間,就會清除掉緩衝的資料。

3、擷取緩衝的資料

以一個例子來描述這個問題:

 代碼如下 複製代碼

public class MainActivity extends Activity {
private ImageView img;
Acache acache;
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
acache = Acache.get(this, "main");
img = (ImageView) findViewById(R.id.img);
 
if (acache.getAsBitmap("pic") != null) {
Log.e("acache","正在使用緩衝!");
img.setImageBitmap(acache.getAsBitmap("pic"));
} else {
Picasso.with(this).load("http://www.b.hsgjxt.com/image/cache/data/0-100x100.jpg").into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.e("Picasso","正在使用Picasso!");
img.setImageBitmap(bitmap);
acache.put("pic", bitmap, 10);
}
 
@Override
public void onBitmapFailed(Drawable errorDrawable) {
 
}
 
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
 
}
});
}
 
}
}

當我第一次進入的時候,控制台列印如下:

當我立即退出又進去,列印如下:

當我延遲了十秒左右再進去,又提示如下:

這樣可以證明緩衝可用,並且緩衝的時間也有效果。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.