<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.moliying.a59_bitmaps.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="加载图片"
android:onClick="loadImageClick"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:id="@+id/imageView"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加载图片"
android:onClick="loadImageClick2"
android:id="@+id/button"
android:layout_gravity="center_horizontal" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:id="@+id/imageView2"
android:layout_gravity="center_horizontal" />
</LinearLayout>
loadImageClick2()中是二级缓存的例子
package com.moliying.a59_bitmaps;
import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.LruCache;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView imageView,imageView2;
//LinkedHashMap实现
private LruCache<String,Bitmap> lruCache;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
imageView2 = (ImageView) findViewById(R.id.imageView2);
//初始化缓存
initCache();
}
//初始化缓存
private void initCache() {
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
// am.getLargeMemoryClass();
int memorySize = am.getMemoryClass();
int cacheSize = memorySize / 8 * 1024 * 1024; //使用Activity内存的8/1作为缓存(字节)
Log.i("memorySize",memorySize+"");
lruCache = new LruCache<>(1); //缓存的大小(个数)
//通过重写sizeOf方法实现缓存图片的字节大小(每个图片的字节)
// lruCache = new LruCache<String,Bitmap>(cacheSize){
// @Override
// protected int sizeOf(String key, Bitmap value) {
// return value.getByteCount();
// }
// };
Log.i("LruCache",lruCache.toString());
}
//把图片添加到缓存中
public void addBitmapToCache(String key,Bitmap bitmap){
if(key!=null && bitmap!=null){
lruCache.put(key,bitmap);
}
}
//从缓存中获取图片
public Bitmap getBitmapFromCache(String key){
if(key!=null){
Log.i("getBitmap","getBitmapFromCache");
return lruCache.get(key);
}
return null;
}
public void loadImageClick(View view){
Bitmap bitmap = getBitmap(R.mipmap.bbb);
imageView.setImageBitmap(bitmap);
}
public void loadImageClick2(View v){
Bitmap bitmap = getBitmap(R.mipmap.aaa);
imageView2.setImageBitmap(bitmap);
}
//获取图片:先从缓存中获取,如果没有,再从资源文件中获取,再把图片添加到缓存中
private Bitmap getBitmap(int resId) {
String key = String.valueOf(resId);
Bitmap bitmap = getBitmapFromCache(key);
if(bitmap==null) {
Log.i("getBitmap","loadImageFromResource");
bitmap = loadImageFromResource(this, resId, 100, 100);
addBitmapToCache(key,bitmap);
}
return bitmap;
}
//加载图片
private Bitmap loadImageFromResource(Context context,int resId, int reqWidth, int reqHeight) {
//创建一个图片解码设置选项对象
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;//设置只解码图片的边界
BitmapFactory.decodeResource(context.getResources(),resId,options);//解码图片
options.inSampleSize = calculateSampleSize(options,reqWidth,reqHeight);
options.inJustDecodeBounds = false;//设置为需要解码图片
Bitmap b = BitmapFactory.decodeResource(getResources(),R.mipmap.bbb,options);
return b;
}
//计算采样比例
private int calculateSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight){
BitmapFactory.Options opt = options;
int width = opt.outWidth;
int height = opt.outHeight;
int inSampleSize = 1;
if(width>reqWidth || height>reqHeight){
if(width>height){
inSampleSize = Math.round((float)height / (float) reqHeight);
}else{
inSampleSize = Math.round((float)width / (float) reqWidth);
}
}
return inSampleSize;
}
}