在自定义网格视图上实现Picasso库

时间:2014-03-20 13:32:54

标签: android eclipse gridview picasso

我有网格视图的片段,我想将我的图像保存在应用程序中。但表现糟糕而缓慢。我听说毕加索图书馆可以提高性能。我尝试下载Picasso_sample文件夹并在我的eclipse中运行它,每次我将Picasso.jar文件添加到我的项目时,我的项目崩溃并显示几个错误。我试图清理它并再次构建它,但是徒劳无功。任何人都可以帮我建议一种方法将Picasso库添加到我的项目中吗?

以下是我有网格视图的活动代码:

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);
                }
            });
        }

这是图像适配器的代码:

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;
    }
}

那我怎么能在这里使用Picasso库?

1 个答案:

答案 0 :(得分:1)

将您的picasso jar添加到libs文件夹中,然后尝试下面的代码来显示

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

链接: http://square.github.io/picasso/