Search code examples
androideclipsegridviewpicasso

Implementing Picasso library on Custom grid view


I have fragments with grid view and I want to keep my images in the application. But the performance is bad and slow. And I heard that Picasso library can increase the performance a lot. I tried to download the Picasso_sample folder and run it in my eclipse, where each time I add Picasso.jar file to my project, my project crashes and displays several errors. I tried to clean it and build it again, but in vain. Can anyone help me in suggesting a way to add Picasso library to my project?

Here is the code of the activity that i has the grid view:

public class AndroidGridLayoutActivity extends Activity {




    public Integer[] mThumbIds = { R.drawable.rom_1, R.drawable.rom_2,
                R.drawable.rom_3, R.drawable.rom_4, R.drawable.rom_5,
                R.drawable.rom_6, R.drawable.rom_7, R.drawable.rom_8,
                R.drawable.rom_9, R.drawable.rom_10, R.drawable.rom_11,
                R.drawable.rom_12, R.drawable.rom_13, R.drawable.rom_14,
                R.drawable.rom_15, R.drawable.rom_16 };

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



            GridView gridView = (GridView) findViewById(R.id.grid_view);
            try {
                // Instance of ImageAdapter Class
                gridView.setAdapter(new ImageAdapter(this, mThumbIds));
            } catch (OutOfMemoryError E) {
                E.printStackTrace();
            }

            /**
             * On Click event for Single Gridview Item
             * */
            gridView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {

                    // Sending image id to FullScreenActivity
                    Intent i = new Intent(getApplicationContext(),
                            FullImageActivity2.class);
                    // passing array index
                    // i.putExtra("id", position);
                     i.putExtra("id", mThumbIds[position]);
                    Log.d("ID", "" + mThumbIds[position]);
                     startActivity(i);
                }
            });
        }

and here is the code of the image adapter :

public class ImageAdapter extends BaseAdapter {
    private Context mContext;


    // Keep all Images in array
    public Integer[] mThumbIds = {};


    public ImageAdapter(Context c,Integer[] mThumbIds2){
        mContext = c;
        this.mThumbIds=mThumbIds2;
    }


    @Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {         
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(100, 100));

        return imageView;
    }
}

so how can i use Picasso library here?


Solution

  • add your picasso jar into libs folder and try below code to display

    Picasso.with(getApplicationContext()).load(R.drawable.ic_launcher).into(target);
    

    link: http://square.github.io/picasso/