如何使用来自url的universal-image-loader加载资源

时间:2014-12-18 03:50:54

标签: android listview universal-image-loader

我使用了FragmentActivity + FragmentTabHost布局, ListView使用universal-image-loader显示图片。 但是我不知道为什么,切换时的标签会非常慢,几秒钟没有响应。 所以,我到处寻找解决方案,但没有。

主要活动类:

public class SMMainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sm_index);
        initView();
    }

    private void initView() {
        layoutInflater = LayoutInflater.from(this);
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        int count = fragmentArray.length;

        for (int i = 0; i < count; i++) {
            TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i])
                    .setIndicator(getTabItemView(i));
            mTabHost.addTab(tabSpec, fragmentArray[i], null);
        }
    }

    private View getTabItemView(int index) {
        View view = layoutInflater.inflate(R.layout.tab_indicator, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.icon);
        imageView.setImageResource(mImageViewArray[index]);
        TextView textView = (TextView) view.findViewById(R.id.title);
        textView.setText(mTextviewArray[index]);

        return view;
    }

    private FragmentTabHost mTabHost;
    private Class<?> fragmentArray[] = { SMHomeFragment.class,
            SMCollectionFragment.class, SMShopCarFragment.class,
            SMMeFragment.class };
    private LayoutInflater layoutInflater;
    private int mImageViewArray[] = { R.drawable.indexicon3,
            R.drawable.indexicon4, R.drawable.indexicon2, R.drawable.indexicon };
    public static String mTextviewArray[] = { "index", "other", "shopcar", "me" };
}

Listview adpter类:

public class ProductAdpter extends BaseAdapter {
    public ProductAdpter(Activity activity, List<StoreProduct> products) {
        this.mActivity = activity;
        this.products = products;
        inflater = LayoutInflater.from(activity);
    }

    public int getCount() {
        if(products==null)
            return 0;
        return products.size();
    }

    public StoreProduct getItem(int position) {
        return products.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder {
        public ImageView productImageview;
        public TextView productName;
        ... ...
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        final StoreProduct data = products.get(position);
        ViewHolder holder;
        View vi = convertView;
        if (convertView == null){
            vi = inflater.inflate(R.layout.listview_sm_product, null);
            holder = new ViewHolder();
            holder.productImageview = (ImageView) vi
                    .findViewById(R.id.product_imageview);
            holder.productName = (TextView) vi.findViewById(R.id.product_name);
            ... ...
            vi.setTag(holder);
        } else {
            holder = (ViewHolder) vi.getTag();
        }
        ImageLoader.getInstance().displayImage(data.getPicturePath(), holder.productImageview, animateFirstListener);
        holder.productName.setText(data.getName());
        ... ...
        return vi;
    }

    private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
        static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }


    private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
    private Activity mActivity;
    private static LayoutInflater inflater = null;
    private List<StoreProduct> products;
}

ImageLoader配置:

DisplayImageOptions options = new DisplayImageOptions.Builder()
        .showImageOnLoading(R.drawable.ic_stub)
        .showImageForEmptyUri(R.drawable.ic_empty)
        .showImageOnFail(R.drawable.ic_error)
        .cacheInMemory(true)
        .cacheOnDisk(true)
        .considerExifParams(true)
        .build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
        .threadPriority(Thread.NORM_PRIORITY - 2)
        .denyCacheImageMultipleSizesInMemory()
        .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
        .diskCacheSize(50 * 1024 * 1024) // 50 Mb
        .diskCacheFileNameGenerator(new Md5FileNameGenerator())
        .imageDownloader(new BaseImageDownloader(context,5 * 1000, 30 * 1000)) 
        .defaultDisplayImageOptions(options)
        .tasksProcessingOrder(QueueProcessingType.LIFO)
        //.writeDebugLogs()
        .build();
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config);

1 个答案:

答案 0 :(得分:0)

尝试picasso

适用于Android的强大图像下载和缓存库 http://square.github.io/picasso/