[英]Load Image From URL to Picasso
I wrote a wallpaper app for Android. 我为Android编写了墙纸应用程序。 I used Picasso for download, cache and show images from given URLs.
我使用毕加索下载,缓存和显示给定URL中的图像。
In mainActivity I have a Gridview which images load inside it. 在mainActivity中,我有一个Gridview,其中加载了图像。
And in ActivityTwo I have Imageview which shows the selected image but the problem is that image load very very slowly (for example a 300Kb image load in 10 secs). 在ActivityTwo中,我有Imageview显示了选定的图像,但问题是图像加载非常缓慢(例如10秒内加载了300Kb图像)。
How can I make images load faster? 如何使图像加载更快?
Here is my codes: 这是我的代码:
MainActivity.Java MainActivity.Java
public class MainActivity extends Activity { 公共类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
};
}
Try Glide library. 尝试使用Glide库。 It loads images faster than Picasso library.
它比Picasso库更快地加载图像。 For more details refer this link.
有关更多详细信息,请参阅此链接。 Libraries to include
图书馆包括
dependencies { compile 'com.github.bumptech.glide:glide:3.5.2' compile 'com.android.support:support-v4:22.0.0' }
Use this to load images : 使用它来加载图像:
Glide.with(context) .load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg") .into(ivImg);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.