1. Create a new tool class
package zhanghaijiao.bawei.com.lrucache_demo;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.util.LruCache;
/**
* Created by jane on 2017/12/20 .
*/
public class LruCacheUtils {
//Declare the memory cache
private LruCache<String,Bitmap> mLruCache;
//Initialize in the constructor
public LruCacheUtils(Context context) {
//Get the memory of the current application
int maxMemory=( int)Runtime.getRuntime().maxMemory();
//The memory cache is 1/8 of the current application
int cacheMemory=maxMemory/8;
//Initialization
mLruCache=new LruCache<String, Bitmap>(cacheMemory) {
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getByteCount();//The calculation method of custom bitmap data size
}
};
}
/**
* Save the image to the memory cache
* @param key The url of the image
* @ param bitmap image
*/
public void savePicToMemory(String key,Bitmap bitmap){
try {
mLruCache.put(key,bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Get by key value Cached image
* @param key image url address
* @return Bitmap or null
*/
public Bitmap getPicFromMemory(String key){
Bitmap bitmap=null;
try {
//Get the image by key
bitmap= mLruCache.get(key);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
/**
* clear cache
* evict: evict evict
*/
public void clearCache() {
if (mLruCache != null) {
if (mLruCache.size() > 0) {
Log.d("CacheUtils",
"mMemoryCache.size() " + mLruCache.size() );
mLruCache.evictAll();
Log.d("CacheUtils", "mMemoryCache.size()" + mLruCache.size());
}
mLruCache = null;
}
}
}
2.mainactivity call tool class
package zhanghaijiao.bawei.com.lrucache_demo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private LruCacheUtils lruCacheUtils;
private String picUrl="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1513777051557&di=a6de503f11b6aa53a60e844e13c01f0c&imgtype=jpg&src=http%3A%2F%2Fimg3.imgtn.bdimg.com%2Fit%2Fu%3D3164275898%2C1502108289%26fm%3D214%26gp%3D0.jpg";
private Button btnDown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.img);
btnDown = findViewById(R.id.btn_down);
//初使化内存缓存
lruCacheUtils = new LruCacheUtils(this);
//按钮的点击事件
btnDown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//First determine if there is a
Bitmap in the memory cache bitmap=lruCacheUtils.getPicFromMemory(picUrl);
if(bitmap!=null){
Log.d("zzz","From the memory cache Get");
imageView.setImageBitmap(bitmap);
}else {
Log.d("zzz","Get from the network");
//Download pictures from the network
MyPicTask task=new MyPicTask();
task.execute(picUrl) ;
}
}
});
}
class MyPicTask extends AsyncTask<String,Void,Bitmap>{
@Override
protected Bitmap doInBackground(String... strings) {
try {
//使用HttpUrlConnection
URL url=new URL(strings[0]);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
if(connection.getResponseCode()==200){
InputStream inputStream=connection.getInputStream();
return BitmapFactory.decodeStream(inputStream);
}
} catch (MalformedURLException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
imageView.setImageBitmap(bitmap);
// Save in memory cache
lruCacheUtils.savePicToMemory(picUrl,bitmap);
}
}
}