将图片从网址加载到毕加索

时间:2015-08-13 20:09:00

标签: android gridview download picasso

我为Android写了一个壁纸应用程序。我使用Picasso下载,缓存并显示来自给定URL的图像。

在mainActivity中,我有一个Gridview,可以在其中加载图像。

在ActivityTwo中,我有Imageview显示所选图像 但问题是图像加载速度非常慢(例如10秒内300Kb的图像加载)。

如何让图片加载更快?

这是我的代码:

MainActivity.Java

公共类MainActivity扩展了Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this, ActivityTwo.class);
                intent.putExtra("position", position);
                startActivity(intent);
            }
        });
    }


    //    our custom adapter
    private class ImageAdapter extends BaseAdapter {
        private Context mContext;

        public ImageAdapter(Context context) {
            mContext = context;
        }

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

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

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

        @Override
        public View getView(int position, View convertView,
                            ViewGroup parent) {
            ImageView imageView;
//            check to see if we have a view
            if (convertView == null) {
//                no view - so create a new one
                imageView = new ImageView(mContext);
            } else {
//                use the recycled view object
                imageView = (ImageView) convertView;
            }

//            Picasso.with(MainActivity.this).setDebugging(true);
            Picasso.with(MainActivity.this)
                    .load(mThumbIds[position])
                    .placeholder(R.raw.place_holder)
                    .error(R.raw.big_problem)
                    .noFade().resize(250, 250)
                    .into(imageView);

            return imageView;
        }
    }

    static String[] mThumbIds = {

            //My Links Here
    };
}

1 个答案:

答案 0 :(得分:0)

试试Glide库。它比Picasso库加载图像更快。有关详细信息,请参阅此link. 图书馆包括

dependencies { compile 'com.github.bumptech.glide:glide:3.5.2' compile 'com.android.support:support-v4:22.0.0' }

使用此方法加载图片:

Glide.with(context) .load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg") .into(ivImg);