머리말
사실, 나는 일주일의 진입 후, 두 형제를 따라 안드로이드를 개발할 수 있습니다. 최근에 나는 요약 할 기회로 바빴다.
원칙
ListView 또는 GridView에 그림을로드하는 원리는 기본적으로 동일합니다.
메모리 캐시에서 가져 와서 파일 캐시에서 다음 단계를 얻을 수 없으면 메모리 캐시와 파일 캐시를 얻을 수 없으면 반환합니다.
흐름도는 다음과 같습니다.
동시에 스레드 수에주의하십시오. 일반적으로 ListView에 그림을로드하면 모든 사람이 새 스레드를 열어로드 할 수 있지만 빠르게 미끄러지면 OOM을 쉽게 만듭니다. 따라서 스레드 수를 제어해야합니다. 스레드 풀을 통해 스레드 수를 제어 할 수 있습니다.
스레드 풀을 설정하는 방법은 다음과 같습니다.
executorService executorService = executor.netfixedThreadPool (5);
파일 캐시 클래스
java.io.file; os.environment.getexternalstoragestate (). ) {cachedir.mkders ();}} public file getFile (String URL) {// URL의 해외 코드 string에 의해 이미지 식별 = string.valueof (url.hashcode ()); return f;} public void clear () {cachedir.listfiles (); 메모리 캐시 클래스
다음은 Soft Reference, Map <string, softreference <bitmap >> cache입니다
Java.Lang.softreference java.util.linkedhashmap; 컬렉션. 새로운 LinkedHashMap <softreference <bitmap >> (10, 1.5f, true)) {return null;} softreference <bitmap> ref = cache (id); ref.get ();} public void put (String id, bitmap bitmap) {cache.put (id, new softreference <bit map> (bitmap);} public void clear () {cache.clear ();}}사진 로딩 클래스
java.io.fileinputstream; Util .bitmapfactory; 이미지 Cache * / MemoryCache MemoryCache = File Image Cache * / Filecache Filecache; SynchronizedMap (New Heafhashmap <ImageView, String> ()) * STREW STREARSEREDLER (Context Context)에서 이미지를 표시합니다. {filecache = 새로운 filecache (resid_num); 메모리 캐시 이미지 imageView.SetImageBitMap (BitMap);} else {// 파일 캐시 또는 네트워크 QueuePhoto (url, imagev iew)} 개인 void queuephoto (String URL, ImageView imageView) {phototoload phototoload = new Phototoload (url , ImageView); executorService.submit (phototoload);} prive bitmap getbitmap (String URL) Lecache.getFile (url); {return bitmap = null inmindurl (inminconnection); (true)는 Conn.getInputStream (); {if (OutofMemoryError) {requoDStream IS, outputStream os) {int buffer_size = 1024; .read (bytes, 0, buffer_size); count == -1) {break;} os.wri te (te bytes, 0, count);}} catch (예외 e)} 개인 비트 맵 decodefile (파일). f) {// todo : 이미지 크기를 압축합니다. fileInputStream (f); {memorycache.clear (); url, imageview imageview) {this.url = url; imageview = im ) {this.phototoload = phototoload;} private boolean imageviewreused (phototoload photoload) {string tag = imageviews.get (phototoload.im.im.im.im.im.im ageview (tag == null); || tag.equ! BitMap BitMap = getBitMap (photoToload.url); BitMapDisplayer (Bitmap, Phototoload); @override public void run () {if (imageViewReused (phototoload)) {return;} if (bitmap! = null) oad.imageView.SetImageBitMap (bitmap);} else {photoToload.imageView.SetImagerEsource (default_bg);}}}}}}}}}}} }}호출 방법
ImageLoader imageLoader = 새로운 ImageLoader (컨텍스트);