package com.scxh.android1503.asynctask;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import com.scxh.android1503.R;
import com.squareup.picasso.Picasso;
public class GridViewAsyncTaskActivity extends Activity {
private GridView mGridView;
private String[] imageThumbUrls = Constances.imageThumbUrls;
private GridViewAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.asynctask_gridview);
mGridView = (GridView) findViewById(R.id.asynctask_gridview);
mAdapter = new GridViewAdapter(this);
mGridView.setAdapter(mAdapter);
}
public class GridViewAdapter extends BaseAdapter {
private LayoutInflater infalter;
private Context context;
public GridViewAdapter(Context context) {
this.context = context;
infalter = LayoutInflater.from(context);
}
@Override
public int getCount() {
return imageThumbUrls.length;
}
@Override
public Object getItem(int position) {
return imageThumbUrls[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageview;
if (convertView == null) {
convertView = infalter.inflate(
R.layout.asynctask_gri_layout, null);
imageview = (ImageView) convertView
.findViewById(R.id.asy_image);
convertView.setTag(imageview);
} else {
imageview = (ImageView) convertView.getTag();
}
String httpUrl = (String) getItem(position);
Picasso.with(context).load(httpUrl).into(imageview);
此为关键代码:调用Picasso第三方包, picasso是Square公司开源的一个Android图形缓存库,地址http://square.github.io/picasso/,可以实现图片下载和缓存功能。仅仅只需要一行代码就能完全实现图片的异步加载:
return convertView;
}
}
}
//所得图片效果如下图:
//定义了一个工具类Constances
package com.scxh.android1503.asynctask;
public class Constances {
public interface ParameterType{
public static final int TYPE_BASEDATE = 1; //基本数据类型
public static final int TYPE_BUNDLE = 2; //Bundle类型
public static final int TYPE_Serializable = 3; //Serializable类型
public static final int TYPE_Parcelable = 4; //Parcelable类型
}
public static final String TYPE = "TYPE";
public static final String PARAMETER = "PARAMETER";
public static final String USER_NAME = "USER_NAME";
public interface ACTION_INTENT{
public static final String INTENT_DETAIL = "com.scxh.android1503.intent.action.DETAILACTIVITY";
}
public final static String[] imageThumbUrls = new String[] {
"https://img-my.csdn.net/uploads/201407/26/1406383299_1976.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383291_6518.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383291_8239.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383290_9329.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383290_1042.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383275_3977.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383265_8550.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383264_3954.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383264_4787.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383264_8243.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383248_3693.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383243_5120.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383242_3127.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383242_9576.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383242_1721.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383219_5806.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383214_7794.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383213_4418.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383213_3557.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383210_8779.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383172_4577.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383166_3407.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383166_2224.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383166_7301.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383165_7197.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383150_8410.jpg",
"https://img-my.csdn.net/uploads/201407/26/1406383131_3736.jpg",
};
}