1 답변
-
LazyList는 sd카드나 서버로부터 이미지를 천천히 불러옵니다. 이미지를 그때그때 불러오는 방식으로 보면 됩니다.
이미지는 로컬 sd카드나 휴대폰 메모리에 캐싱될 수 있습니다. Url이 그 키로 간주되죠. 만약 키가 sd카드를 가리키고 있으면, 이미지를 sd카드로부터 불러오고, 그렇지 않을 경우에는 서버로부터 불러와 선택된 장소에 캐싱하죠. 캐시는 제한될 수 있고, 캐시 이미지가 저장될 장소 또한 사용자가 설정할 수 있죠. 또한 캐시를 초기화할 수도 있습니다.
사용자가 크기가 큰 이미지가 전부 다운로드되어 화면에 표시될 때까지 기다리는 대신, 그때그때 이미지를 불러오는 것이죠. 불러진 이미지는 캐시로 남기때문에 오프라인에서도 이미지를 볼 수 있습니다.
https://github.com/thest1/LazyList
getview에 아래와 같이 사용하면 됩니다.
imageLoader.DisplayImage(imageurl, imageview);
ImageLoader Display 함수 :
public void DisplayImage(String url, ImageView imageView) //url and imageview as parameters { imageViews.put(imageView, url); Bitmap bitmap=memoryCache.get(url); //get image from cache using url as key if(bitmap!=null) //if image exists imageView.setImageBitmap(bitmap); //dispaly iamge else //downlaod image and dispaly. add to cache. { queuePhoto(url, imageView); imageView.setImageResource(stub_id); } }
LazyList의 대안으로는 Universal Image Loader가 있습니다.
https://github.com/nostra13/Android-Universal-Image-Loader. 이는 LazyList를 기반으로 (같은 원리로) 작동합니다. 하지만 많은 설정을 지원하죠. 따라서 저는 Universal Image Loader의 사용을 더 권장합니다. 이를테면, 다운로드에 실패했을 경우 에러 이미지를 보여주거나, 이미지를 둥근 모서리를 적용시켜 보여주거나, 메모리나 디스크에 캐싱을 할 수도 있고, 이미지를 압축할 수도 있습니다.
custom adapter 생성자에 아래와 같은 코드를 작성하고
File cacheDir = StorageUtils.getOwnCacheDirectory(a, "your folder"); // Get singletone instance of ImageLoader imageLoader = ImageLoader.getInstance(); // Create configuration for ImageLoader (all options are optional) ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a) // You can pass your own memory cache implementation .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) .enableLogging() .build(); // Initialize ImageLoader with created configuration. Do it once. imageLoader.init(config); options = new DisplayImageOptions.Builder() .showStubImage(R.drawable.stub_id)//display stub image .cacheInMemory() .cacheOnDisc() .displayer(new RoundedBitmapDisplayer(20)) .build();
getView()에는 아래의 코드를 넣어 사용하면 됩니다.
ImageView image=(ImageView)vi.findViewById(R.id.imageview); imageLoader.displayImage(imageurl, image,options);//provide imageurl, imageview and options
물론 필요에 따라 다른 옵션으로 설정할 수 있습니다.
lazy loading / Universal Image Loader와 함께 부드러운 스크롤과 성능을 위해 view holder를 사용할 수 있습니다. https://developer.android.com/training/improving-layouts/smooth-scrolling.html
댓글 입력