温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android二级缓存加载图片实现照片墙功能

发布时间:2020-09-18 07:54:00 来源:脚本之家 阅读:140 作者:qq_20801369 栏目:移动开发

实现二级缓存加载图片的功能,在使用DiskLruCache时,需先在工程中添加名为libcore.io的包,并将DiskLruCache.Java文件放进去。DiskLruCache直接百度下载即可。

在GridView的适配器中,为ImageView添加图片时,先从内存缓存中加载,内存中无缓存的话则在磁盘缓存中加载,磁盘缓存也没有的话开启线程下载,然后将下载的图片缓存到磁盘,内存中。下载的图片最好先进行压缩,文章最后给出了压缩代码,但本例中并未实现压缩。

/*二级缓存实现图片墙功能,先在内存中加载缓存,内存中无缓存的话到磁盘缓存中加载,仍然没有的话开启线程下载图片,下载后缓存到磁盘中,然后缓存到内存中*/

public class ErJiHuanCun extends ArrayAdapter<String> {
  /**
   * 记录所有正在下载或等待下载的任务。
   */
  private Set<BitmapWorkerTask> taskCollection;

  /**
   * 图片缓存技术的核心类,用于缓存所有下载好的图片,在程序内存达到设定值时会将最少最近使用的图片移除掉。
   */
  private LruCache<String, Bitmap> mMemoryCache;

  /**
   * 图片硬盘缓存核心类。
   */
  private DiskLruCache mDiskLruCache;

  /**
   * GridView的实例
   */
  private GridView mPhotoWall;

  /**
   * 记录每个子项的高度。
   */
  private int mItemHeight = 0;

  public ErJiHuanCun(Context context, int textViewResourceId, String[] objects,
      GridView photoWall) {
    super(context, textViewResourceId, objects);
    mPhotoWall = photoWall;
    taskCollection = new HashSet<BitmapWorkerTask>();
    // 获取应用程序最大可用内存
    int maxMemory = (int) Runtime.getRuntime().maxMemory();
    int cacheSize = maxMemory / 8;
    // 设置图片缓存大小为程序最大可用内存的1/8
    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
      @Override
      protected int sizeOf(String key, Bitmap bitmap) {
        return bitmap.getByteCount();
      }
    };
    try {
      // 获取图片缓存路径
      File cacheDir = getDiskCacheDir(context, "thumb");
      if (!cacheDir.exists()) {
        cacheDir.mkdirs();
      }
      // 创建DiskLruCache实例,初始化缓存数据
      mDiskLruCache = DiskLruCache
          .open(cacheDir, getAppVersion(context), 1, 10 * 1024 * 1024);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    final String url = getItem(position);
    View view;
    if (convertView == null) {
      view = LayoutInflater.from(getContext()).inflate(R.layout.photo_layout, null);
    } else {
      view = convertView;
    }
    final ImageView imageView = (ImageView) view.findViewById(R.id.photo);
    if (imageView.getLayoutParams().height != mItemHeight) {
      imageView.getLayoutParams().height = mItemHeight;
    }
    // 给ImageView设置一个Tag,保证异步加载图片时不会乱序
    imageView.setTag(url); 
    imageView.setImageResource(R.drawable.ic_launcher);
    loadBitmaps(imageView, url);
    return view;
  }

  /**
   * 将一张图片存储到LruCache中。
   * 
   * @param key
   *      LruCache的键,这里传入图片的URL地址。
   * @param bitmap
   *      LruCache的键,这里传入从网络上下载的Bitmap对象。
   */
  public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
    if (getBitmapFromMemoryCache(key) == null) {
      mMemoryCache.put(key, bitmap);
    }
  }

  /**
   * 从LruCache中获取一张图片,如果不存在就返回null。
   * 
   * @param key
   *      LruCache的键,这里传入图片的URL地址。
   * @return 对应传入键的Bitmap对象,或者null。
   */
  public Bitmap getBitmapFromMemoryCache(String key) {
    return mMemoryCache.get(key);
  }

  /**
   * 加载Bitmap对象。此方法会在LruCache中检查所有屏幕中可见的ImageView的Bitmap对象,
   * 如果发现任何一个ImageView的Bitmap对象不在缓存中,就会开启异步线程去下载图片。
   */
  public void loadBitmaps(ImageView imageView, String imageUrl) {
    try {
      Bitmap bitmap = getBitmapFromMemoryCache(imageUrl);
      if (bitmap == null) {
        BitmapWorkerTask task = new BitmapWorkerTask();
        taskCollection.add(task);
        task.execute(imageUrl);
      } else {
        if (imageView != null && bitmap != null) {
          imageView.setImageBitmap(bitmap);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 取消所有正在下载或等待下载的任务。
   */
  public void cancelAllTasks() {
    if (taskCollection != null) {
      for (BitmapWorkerTask task : taskCollection) {
        task.cancel(false);
      }
    }
  }

  /**
   * 根据传入的uniqueName获取硬盘缓存的路径地址。
   */
  public File getDiskCacheDir(Context context, String uniqueName) {
    String cachePath;
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
        || !Environment.isExternalStorageRemovable()) {
      cachePath = context.getExternalCacheDir().getPath();
    } else {
      cachePath = context.getCacheDir().getPath();
    }
    return new File(cachePath + File.separator + uniqueName);
  }

  /**
   * 获取当前应用程序的版本号。
   */
  public int getAppVersion(Context context) {
    try {
      PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(),
          0);
      return info.versionCode;
    } catch (NameNotFoundException e) {
      e.printStackTrace();
    }
    return 1;
  }

  /**
   * 设置item子项的高度。
   */
  public void setItemHeight(int height) {
    if (height == mItemHeight) {
      return;
    }
    mItemHeight = height;
    notifyDataSetChanged();
  }

  /**
   * 使用MD5算法对传入的key进行加密并返回。
   */
  public String hashKeyForDisk(String key) {
    String cacheKey;
    try {
      final MessageDigest mDigest = MessageDigest.getInstance("MD5");
      mDigest.update(key.getBytes());
      cacheKey = bytesToHexString(mDigest.digest());
    } catch (NoSuchAlgorithmException e) {
      cacheKey = String.valueOf(key.hashCode());
    }
    return cacheKey;
  }

  /**
   * 将缓存记录同步到journal文件中。
   */
  public void flushCache() {
    if (mDiskLruCache != null) {
      try {
        mDiskLruCache.flush();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }

  private String bytesToHexString(byte[] bytes) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < bytes.length; i++) {
      String hex = Integer.toHexString(0xFF & bytes[i]);
      if (hex.length() == 1) {
        sb.append('0');
      }
      sb.append(hex);
    }
    return sb.toString();
  }

  /**
   * 异步下载图片的任务。
   * 
   *
   */
  class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {

    /**
     * 图片的URL地址
     */
    private String imageUrl;

    @Override
    protected Bitmap doInBackground(String... params) {
      imageUrl = params[0];
      FileDescriptor fileDescriptor = null;
      FileInputStream fileInputStream = null;
      Snapshot snapShot = null;
      try {
        // 生成图片URL对应的key
        final String key = hashKeyForDisk(imageUrl);
        // 查找key对应的缓存
        snapShot = mDiskLruCache.get(key);
        if (snapShot == null) {
          // 如果没有找到对应的缓存,则准备从网络上请求数据,并写入缓存
          DiskLruCache.Editor editor = mDiskLruCache.edit(key);
          if (editor != null) {
            OutputStream outputStream = editor.newOutputStream(0);
            if (downloadUrlToStream(imageUrl, outputStream)) {
              editor.commit();
            } else {
              editor.abort();
            }
          }
          // 缓存被写入后,再次查找key对应的缓存
          snapShot = mDiskLruCache.get(key);
        }
        if (snapShot != null) {
          fileInputStream = (FileInputStream) snapShot.getInputStream(0);
          fileDescriptor = fileInputStream.getFD();
        }
        // 将缓存数据解析成Bitmap对象
        Bitmap bitmap = null;
        if (fileDescriptor != null) {
        // bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
         WindowManager wm= (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);
         int width=wm.getDefaultDisplay().getWidth();
         bitmap= ImageResizer.decodeSampleBithmapFromFileDescriptor(fileDescriptor,width/3,width/3);
        }
        if (bitmap != null) {
          // 将Bitmap对象添加到内存缓存当中
            addBitmapToMemoryCache(params[0], bitmap);
        }
        return bitmap;
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        if (fileDescriptor == null && fileInputStream != null) {
          try {
            fileInputStream.close();
          } catch (IOException e) {
          }
        }
      }
      return null;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
      super.onPostExecute(bitmap);
      // 根据Tag找到相应的ImageView控件,将下载好的图片显示出来。
      ImageView imageView = (ImageView) mPhotoWall.findViewWithTag(imageUrl);
      if (imageView != null && bitmap != null) {
        imageView.setImageBitmap(bitmap);
      }
      taskCollection.remove(this);
    }

    /**
     * 建立HTTP请求,并获取Bitmap对象。
     * 
     * @param imageUrl
     *      图片的URL地址
     * @return 解析后的Bitmap对象
     */
    private boolean downloadUrlToStream(String urlString, OutputStream outputStream) {
      HttpURLConnection urlConnection = null;
      BufferedOutputStream out = null;
      BufferedInputStream in = null;
      try {
        final URL url = new URL(urlString);
        urlConnection = (HttpURLConnection) url.openConnection();
        in = new BufferedInputStream(urlConnection.getInputStream(), 8 * 1024);
        out = new BufferedOutputStream(outputStream, 8 * 1024);
        int b;
        while ((b = in.read()) != -1) {
          out.write(b);
        }
        return true;
      } catch (final IOException e) {
        e.printStackTrace();
      } finally {
        if (urlConnection != null) {
          urlConnection.disconnect();
        }
        try {
          if (out != null) {
            out.close();
          }
          if (in != null) {
            in.close();
          }
        } catch (final IOException e) {
          e.printStackTrace();
        }
      }
      return false;
    }

  }

}

MainActivity

/**
 * 照片墙主活动,使用GridView展示照片墙。
 * 
 * 
 */
public class MainActivity extends Activity {

  /**
   * 用于展示照片墙的GridView
   */
  private GridView mPhotoWall;

  /**
   * GridView的适配器
   */
  private ErJiHuanCun mAdapter;

  private int mImageThumbSize;
  private int mImageThumbSpacing;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //mImageThumbSize = getResources().getDimensionPixelSize(
  //     R.dimen.image_thumbnail_size);
    //mImageThumbSpacing = getResources().getDimensionPixelSize(
  //     R.dimen.image_thumbnail_spacing);
    mPhotoWall = (GridView) findViewById(R.id.photo_wall);
    mAdapter = new ErJiHuanCun(this, 0, Images.imageThumbUrls,
        mPhotoWall);
    mPhotoWall.setAdapter(mAdapter);
/*   mPhotoWall.getViewTreeObserver().addOnGlobalLayoutListener(
        new ViewTreeObserver.OnGlobalLayoutListener() {

          @Override
          public void onGlobalLayout() {
            final int numColumns = (int) Math.floor(mPhotoWall
                .getWidth()
                / (mImageThumbSize + mImageThumbSpacing));
            if (numColumns > 0) {
              int columnWidth = (mPhotoWall.getWidth() / numColumns)
                  - mImageThumbSpacing;
              mAdapter.setItemHeight(columnWidth);
              mPhotoWall.getViewTreeObserver()
                  .removeGlobalOnLayoutListener(this);
            }
          }
        });*/
  }

  @Override
  protected void onPause() {
    super.onPause();
    //将缓存记录同步到journal文件中
    mAdapter.flushCache();
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    // // 退出程序时结束所有的下载任务
    mAdapter.cancelAllTasks();
  }

}
/**
 * 自定义正方形的ImageView
 * 
 */
public class MyImageView extends ImageView {


  public MyImageView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    // TODO Auto-generated constructor stub
  }

  public MyImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
  }

  public MyImageView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
  }
  @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  // TODO Auto-generated method stub
    //将高度信息改成宽度即可
  super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
}

主Activity的layout

<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:orientation="vertical"
  android:padding="5dp"
 >

<GridView
  android:id="@+id/photo_wall"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center"
  android:horizontalSpacing="5dp"
  android:verticalSpacing="5dp"
  android:numColumns="3"
  android:stretchMode="columnWidth"
  />
</LinearLayout>

GridView中的Item ImageView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

<com.example.imageloader.MyImageView
  android:layout_width="match_parent"
  android:layout_height="0dp"
   android:id="@+id/photo"
  />
</LinearLayout>

图片压缩实现

public class ImageResizer {
 private static final String TAG="ImageResizer";

 public static Bitmap decodeSampledBitmapFromResource(Resources res,
     int resId,int reqWidth,int reqHeight){
   final BitmapFactory.Options options=new BitmapFactory.Options();
   options.inJustDecodeBounds=true;
   BitmapFactory.decodeResource(res,resId,options);
   options.inSampleSize=calculateInSampleSize(options,reqWidth,reqHeight);
   options.inJustDecodeBounds=false;
   return BitmapFactory.decodeResource(res, resId, options);

 }
 public static Bitmap decodeSampleBithmapFromFileDescriptor(FileDescriptor fd,
     int reqWidth,int reqHeight){
   final BitmapFactory.Options options=new BitmapFactory.Options();
   options.inJustDecodeBounds=true;
   BitmapFactory.decodeFileDescriptor(fd, null,options);
   options.inSampleSize=calculateInSampleSize(options,reqWidth,reqHeight);
   options.inJustDecodeBounds=false;
   return BitmapFactory.decodeFileDescriptor(fd, null,options);
 }
 public static int calculateInSampleSize(BitmapFactory.Options options,
     int reqWidth,int reqHeight){
   if(reqWidth==0||reqHeight==0)
     return 1;
   final int width=options.outWidth;
   final int height=options.outHeight;
   int inSampleSize=1;
   if(height>reqHeight||width>reqWidth  ){
     final int halfHeight=height/2;
     final int halfWidth=width/2;
     //尽最大限度的压缩图片,不能让图片的宽高比ImageView的宽高小,否则在将
     //图片显示到ImageView时,图片会放大导致图片失真
   while(halfHeight/inSampleSize>reqHeight&&halfWidth/inSampleSize>reqWidth){
      inSampleSize*=2;
    }
   }
   return inSampleSize;
 }
}


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI