内存缓存的使用

  • Post author:
  • Post category:其他



<Button

android:layout_width=”match_parent”

android:layout_height=”wrap_content”

android:id=”@+id/btn_down”

android:text=”下载”/>

<ImageView

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:id=”@+id/img”

android:layout_below=”@+id/btn_down”


android:layout_centerInParent=”true”/>




public class LruCacheUtils {


//申明内存缓存

private LruCache<String,Bitmap> mLruCache;

//在构造方法中进行初使化

public LruCacheUtils(Context context) {


//得到当前应用程序的内存

int maxMemory=(int)Runtime.getRuntime().maxMemory();

//内存缓存为当前应用程序的8分之1

int cacheMemory=maxMemory/8;

//进行初使化

mLruCache=new LruCache<String, Bitmap>(cacheMemory){


@Override

protected int sizeOf(String key, Bitmap value) {


return value.getByteCount();//自定义bitmap数据大小的计算方式

}

};

}

/**

* 保存图片到内存缓存

* @param key 图片的url

* @param bitmap 图片

*/

public void savePicToMemory(String key,Bitmap bitmap){


try {


mLruCache.put(key,bitmap);

} catch (Exception e) {


e.printStackTrace();

}

}

/**

* 通过key值得到缓存的图片

* @param key 图片的url地址

* @return Bitmap 或 null

*/

public  Bitmap getPicFromMemory(String key){

Bitmap bitmap=null;

try {


//通过key获取图片

bitmap= mLruCache.get(key);

} catch (Exception e) {


e.printStackTrace();

}

return bitmap;

}

}






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) {


//首先判断内存缓存中有没有

Bitmap bitmap=lruCacheUtils.getPicFromMemory(picUrl);

if(bitmap!=null){


Log.d(“zzz”,”从内存缓存中获取”);

imageView.setImageBitmap(bitmap);

}else {


Log.d(“zzz”,”从网络中获取”);

//进行网络下载图片

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);

//保存在内存缓存中

lruCacheUtils.savePicToMemory(picUrl,bitmap);

}

}

}





版权声明:本文为sui_yz原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。