ImageLoader

public class ImageLoader {

private ImageView mImageView;
private String mUrl;

// 创建缓存机制 Cache
private LruCache<String, Bitmap> mCaches;



public ImageLoader() {
	
	// 获得当前最大可用内存
	int maxMemroy = (int) Runtime.getRuntime().maxMemory();
	// 设置最大缓存大小
	int cacheSize = maxMemroy / 4;
	
	
	mCaches = new LruCache<String, Bitmap>(cacheSize) {
		// 在每次加入内存缓存时候调用
		protected int sizeOf(String key, Bitmap value) {
			// 在每次存入缓存的时候
			return value.getByteCount();
		};
	};
}


/**
 * 增加缓存
 * 
 * @param url
 * @param bitmap
 */
public void addBitmapToCache(String url, Bitmap bitmap) {
	if (getBitmapToCache(url) == null) {
		mCaches.put(url, bitmap);
	}
}

public Bitmap getBitmapToCache(String url) {
	return mCaches.get(url);
}




private Handler handler = new Handler() {
	public void handleMessage(android.os.Message msg) {
		if (mImageView.getTag().equals(mUrl)) {
			mImageView.setImageBitmap((Bitmap) msg.obj);
		}
	};
};

/**
 * 使用多线程加载图片
 * 
 * @param image
 * @param url
 */
public void ShowImageByThread(ImageView imageView, final String url) {
	mImageView = imageView;
	mUrl = url;
	new Thread() {
		@Override
		public void run() {
			super.run();
			Bitmap bitmap = getBitMapFromURL(url);
			Message message = Message.obtain();
			message.obj = bitmap;
			handler.sendMessage(message);
		}
	}.start();

}
//没问题!!

/**
 * 使用httpUrlConnection下载Bitmap
 * 
 * @param urlString
 * @return
 */
public Bitmap getBitMapFromURL(String urlString) {
	Bitmap bitmap;
	InputStream is = null;
	try {
		URL url = new URL(urlString);
		HttpURLConnection connection = (HttpURLConnection) url
				.openConnection();
		is = new BufferedInputStream(connection.getInputStream());
		bitmap = BitmapFactory.decodeStream(is);
		connection.disconnect();
		return bitmap;
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	return null;
}



/**
 * 使用异步任务加载Bitmap
 * 
 * @param imageView
 * @param url
 */
public void ShowImageByAsyncTask(ImageView imageView, String url) {
	Bitmap bitmap = getBitmapToCache(url);
	// 在这里使用loadImages来加载图片,bitmap为空时不再调用AsynTask而是加在一张默认图片
	if (bitmap == null) {
		new NewsAsyncTask(imageView,url).execute(url);
	} else {
		imageView.setImageBitmap(bitmap);
	}
	

}

private class NewsAsyncTask extends AsyncTask<String, Void, Bitmap> {
	 private ImageView mImageView;
	private String mUrl;

	public NewsAsyncTask(ImageView imageView,String url) {
		mImageView = imageView;
		mUrl = url;
	}

	@Override
	protected Bitmap doInBackground(String... params) {
		// 从网络获取图片or添加缓存
		String url = params[0];
		// 从网络中获取中取出图片
		Bitmap bitmap = getBitMapFromURL(url);
		if (bitmap != null) {
			addBitmapToCache(url, bitmap);
		}

		return bitmap;
	}

	@Override
     protected void onPostExecute(Bitmap bitmap) {
		            super.onPostExecute(bitmap);
		            if(mImageView.getTag().equals(mUrl)) {
		                mImageView.setImageBitmap(bitmap);
		            }
		
		  }

}

}

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页