Android - 載圖Picasso、Imageloader


Picasso:https://github.com/square/picasso

// 顯示圖片
Picasso.with(mContext)
                .load(url)
                .placeholder(R.mipmap.ic_launcher) //預設顯示
                .error(R.mipmap.ic_launcher) //錯誤顯示
                .fit() //顯示寬高為預設圖的寬高或是.resize(w, h)指定寬高
                .into(iv_photo);

// 滑動載入優化
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
      @Override
      public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
           if (newState == RecyclerView.SCROLL_STATE_IDLE) {//滑动停止的時加載圖片
                 Picasso.with(mActivity).resumeTag(mAdapter);
            } else {//滑动时候不加載圖片
                 Picasso.with(mActivity).pauseTag(mAdapter);
           }
     }
});

Imageloader:https://github.com/nostra13/Android-Universal-Image-Loader

protected ImageLoader imageLoader;
// 須在Activity先設置(onCreate)
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
                .threadPoolSize(5)
                .threadPriority(Thread.MIN_PRIORITY + 2)
                .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
                .memoryCache(new WeakMemoryCache())
                .build();
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(config);

// 設定顯示圖片
DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap. ic_launcher)
                .showImageOnFail(R.mipmap. ic_launcher)
                .showImageForEmptyUri(R.mipmap. ic_launcher)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        ImageLoader.getInstance().displayImage(url, iv_photo, options);

// 滑動載入優化
listView.setOnScrollListener(new PauseOnScrollListener(imageLoader, true, true));

留言

這個網誌中的熱門文章

Android - 輸入字串排除 使用InputFilter

Android - OkHttp3連線 post應用