Android(安卓)ListView异步加载图片
16lz
2021-01-26
/** * 图片加载工具 * Created by lezg on 2014/7/24. */public class ImagesLoader { private static ImagesLoader mIamesLoader; private LruCache<Integer, Bitmap> mMemoryCache; private ImagesLoader() { int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); // 设置图片缓存大小为程序最大可用内存的1/4 int cacheSize = maxMemory / 4; mMemoryCache = new LruCache<Integer, Bitmap>(cacheSize) { @Override protected int sizeOf(Integer key, Bitmap bitmap) { return bitmap.getByteCount() / 1024; } }; } /** * 获取 ImagesLoader实例 * * @return ImagesLoader实例 */ public static ImagesLoader getInstance() { if (mIamesLoader == null) { mIamesLoader = new ImagesLoader(); } return mIamesLoader; } /** * 将一张图片存储到LruCache中 * * @param key LruCache的键,这里传人图片资源的ID * @param bitmap LruCache的键bitmap */ public void addBitmapToMemoryCache(int key, Bitmap bitmap) { if (getBitmapFromMemCache(key) == null) { mMemoryCache.put(key, bitmap); } } /** * 从LruCache中获取一张图片,如果不存在就返回null。 * * @param key LruCache的键,这里传人图片资源的ID * @return 对应传入键的Bitmap对象,或者null。 */ public Bitmap getBitmapFromMemCache(int key) { return mMemoryCache.get(key); } /** * 获取压缩比率 * * @param options * @param reqWidth 目标宽度 * @param reqHeight 目标高度 * @return 压缩比率 */ private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { int height = options.outHeight; int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { int heightRatio = Math.round((float) height / (float) reqHeight); int widthRatio = Math.round((float) width / (float) reqWidth); inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } /** * 对图片进行压缩 * * @param mResources Resources * @param imageId 图片资源ID * @param reqWidth 目标宽度 * @param reqHeight 目标高度 * @return bitmap */ public Bitmap decodeSampleBitmapFromResource(Resources mResources, int imageId, int reqWidth, int reqHeight) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(mResources, imageId, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory.decodeResource(mResources, imageId, options); addBitmapToMemoryCache(imageId, bitmap); return bitmap; }}
/** * GoogleCardsAdapter * Created by lezg on 2014/7/23. */public class GoogleCardsAdapter extends BaseAdapter { private Context mContext; private ImagesLoader mImagesLoader; private ExecutorService threadPools = Executors.newFixedThreadPool(3); static class ListViewItem { public ImageView imageView; } public GoogleCardsAdapter(Context context) { this.mContext = context; this.mImagesLoader = ImagesLoader.getInstance(); } @Override public int getCount() { return AppHelper.imagesId.length; } @Override public Object getItem(int position) { return AppHelper.imagesId[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ListViewItem lvItem; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_item, parent, false); lvItem = new ListViewItem(); lvItem.imageView = (ImageView) convertView.findViewById(R.id.image); convertView.setTag(lvItem); } else { lvItem = (ListViewItem) convertView.getTag(); } setImages(lvItem, position); lvItem.imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AppHelper.showPhotos(mContext,position); } }); return convertView; } private void setImages(final ListViewItem lvItem, int position) { final int imageId = AppHelper.imagesId[position]; final Handler myHandler = new Handler() { @Override public void handleMessage(Message msg) { Bitmap bp = (Bitmap) msg.obj; lvItem.imageView.setImageBitmap(bp); } }; Bitmap bitmap = mImagesLoader.getBitmapFromMemCache(imageId); if (bitmap == null) { threadPools.submit(new Runnable() { @Override public void run() { Bitmap bitmap = mImagesLoader.decodeSampleBitmapFromResource(mContext.getResources(), imageId, 600, 400); Message msg = new Message(); msg.obj = bitmap; msg.what = 1; myHandler.sendMessage(msg); } }); } else { lvItem.imageView.setImageBitmap(bitmap); } }}
更多相关文章
- listview使用BaseAdapter显示图片和文字
- absolutelayout 实现图片重叠
- android 图片遮罩,水印
- Android拆分Bitmap完整示例
- android bitmap oom
- 缩放图片 : matrix.setScale(0.5f,0.5f);
- ImageCompressUtils
- Android(安卓)将bitmap保存为本地png图片
- 使用Toast显示提示信息框