/** * 加载图片 * * @param path * @param imageView */ public void loadImage(final String path, final ImageView imageView) { // 防止未初始化 if (init < 1) return; // set tag imageView.setTag(path); // UI线程 if (mHandler == null) { mHandler = new Handler() { @Override public void handleMessage(Message msg) { ImgBeanHolder holder = (ImgBeanHolder) msg.obj; ImageView imageView = holder.imageView; Bitmap bm = holder.bitmap; String path = holder.path; if (imageView.getTag().toString().equals(path)) { imageView.setImageBitmap(bm); } } }; } Bitmap bm = getBitmapFromLruCache(path); if (bm != null) { ImgBeanHolder holder = new ImgBeanHolder(); holder.bitmap = bm; holder.imageView = imageView; holder.path = path; Message message = Message.obtain(); message.obj = holder; mHandler.sendMessage(message); } else { addTask( new Runnable() { @Override public void run() { ImageSize imageSize = getImageViewWidth(imageView); int reqWidth = imageSize.width; int reqHeight = imageSize.height; Bitmap bm = decodeSampledBitmapFromResource(path, reqWidth, reqHeight); addBitmapToLruCache(path, bm); ImgBeanHolder holder = new ImgBeanHolder(); holder.bitmap = getBitmapFromLruCache(path); holder.imageView = imageView; holder.path = path; Message message = Message.obtain(); message.obj = holder; // Log.e("TAG", "mHandler.sendMessage(message);"); mHandler.sendMessage(message); mPoolSemaphore.release(); } }); } }
@Override public void run() { Bitmap bitmap = null; if (width != 0 && height != 0) bitmap = readImage(mImagePath, width, height); else { int[] viewSizes = new int[2]; measureSize(mImageView, viewSizes); bitmap = readImage(mImagePath, viewSizes[0], viewSizes[1]); } addImageToCache(mImagePath + width + height, bitmap); ImgBeanHolder holder = new ImgBeanHolder(); holder.bitmap = getImageFromCache(mImagePath + width + height); holder.imageView = mImageView; holder.imagePath = mImagePath; holder.imageLoadListener = imageLoadListener; getPostHandler().post(holder); }
/** * According to the specified width high loading pictures, wide high, the greater the picture * clearer, more memory * * @param imageView ImageView * @param imagePath Path from local SDCard * @param width Target width * @param height Target height */ public void loadImage( ImageView imageView, String imagePath, int width, int height, ImageLoadListener imageLoadListener) { if (imageLoadListener == null) imageView.setTag(imagePath); Bitmap bitmap = getImageFromCache(imagePath + width + height); if (bitmap != null) { ImgBeanHolder holder = new ImgBeanHolder(); holder.imageView = imageView; holder.imagePath = imagePath; holder.bitmap = bitmap; holder.imageLoadListener = imageLoadListener; getPostHandler().post(holder); } else { imageView.setImageDrawable(mDefaultDrawable); mExecutorService.execute( new TaskThread(imageView, imagePath, width, height, imageLoadListener)); } }