使用Universal Image Loader加载可绘制图像

时间:2014-08-10 18:28:33

标签: android

我已经浏览了Universal Image Loader库的GitHub page并试图显示以下代码中给出的drawable文件夹中的图像,但只显示了存根图像。

我初始化了imageLoader,如下所示

 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
            .threadPoolSize(3)
            .threadPriority(Thread.NORM_PRIORITY - 2)
            .memoryCacheSize(1500000) // 1.5 Mb
            .denyCacheImageMultipleSizesInMemory()
            .discCacheFileNameGenerator(new Md5FileNameGenerator())
           // .enableLogging() // Not necessary in common
            .build();
        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config);

BaseActivity:

public abstract class BaseActivity extends Activity {

        protected ImageLoader imageLoader = ImageLoader.getInstance();          

}

我的活动代码,

public class MultiStickerSelectorActivity extends BaseActivity {

    private ArrayList<String> imageUrls;
    private DisplayImageOptions options;
    private ImageAdapter imageAdapter;
    private static final int SELECTION_LIMIT = 5;
    private int mSelectionCount = 0;

 // Keep all Images in array
    private static Integer[] stickerResIds = {R.drawable.men_sticker_0,R.drawable.men_sticker_1,.....,R.drawable.men_sticker_16};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_image_grid);

        this.imageUrls = new ArrayList<String>();

        // get image urls
        for (int i = 0; i < stickerResIds.length; i++) {
           imageUrls.add("drawable://"+stickerResIds[i]);
        }

        options = new DisplayImageOptions.Builder()
            .showStubImage(R.drawable.stub_image)
            .showImageForEmptyUri(R.drawable.image_for_empty_url)
            .cacheInMemory()
            .cacheOnDisc()
            .build();

        imageAdapter = new ImageAdapter(this, imageUrls);

        GridView gridView = (GridView) findViewById(R.id.gridview);
        gridView.setAdapter(imageAdapter);        
    }

    @Override
    protected void onStop() {
        imageLoader.stop();
        super.onStop();
    }

    public void btnChoosePhotosClick(View v){
        GlobalClass.imagePaths = imageAdapter.getCheckedItems(); //saved urls of checked images 
        Log.d("MultiStickerSelectorActivity", "Selected Items: " + GlobalClass.imagePaths.toString());
        Intent intent=new Intent();  // return to previous activity
        setResult(1,intent);  
        finish();//finishing activity  
    }

    public class ImageAdapter extends BaseAdapter {

        ArrayList<String> mList;
        LayoutInflater mInflater;
        Context mContext;
        SparseBooleanArray mSparseBooleanArray;

        public ImageAdapter(Context context, ArrayList<String> imageList) {
            // TODO Auto-generated constructor stub
            mContext = context;
            mInflater = LayoutInflater.from(mContext);
            mSparseBooleanArray = new SparseBooleanArray();
            mList = new ArrayList<String>();
            this.mList = imageList; 
        }

        public ArrayList<String> getCheckedItems() {
            ArrayList<String> mTempArry = new ArrayList<String>();

           // logic to get checked images goes here

            return mTempArry;
        }

        @Override
        public int getCount() {
            return imageUrls.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            if(convertView == null) {
                convertView = mInflater.inflate(R.layout.row_multiphoto_item, null);
            }

            CheckBox mCheckBox = (CheckBox) convertView.findViewById(R.id.checkBox1);
            final ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);

            imageLoader.displayImage(imageUrls.get(position), imageView, options, new SimpleImageLoadingListener() {
                @Override
                public void onLoadingComplete(Bitmap loadedImage) {
                    Animation anim = AnimationUtils.loadAnimation(MultiStickerSelectorActivity.this, R.anim.fade_in);
                    imageView.setAnimation(anim);
                    anim.start();
                }
            });

            mCheckBox.setTag(position);
            mCheckBox.setChecked(mSparseBooleanArray.get(position));
            mCheckBox.setOnCheckedChangeListener(mCheckedChangeListener);

            return convertView;
        }

        OnCheckedChangeListener mCheckedChangeListener = new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //logic related to check - uncheck image goes here

            }
        };
    }

}

我无法得到我错的地方,我们将非常感谢任何帮助。提前谢谢!

0 个答案:

没有答案