1、新建一个XML,定义按钮点击按钮开始取图片:
<?x ml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imgview"
android:layout_width="wrap_content"
android:layout_height="80dp"/>
</LinearLayout>
2、创建一个Activty继承Activty
package com.scxh.netwangqu.gridview;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridView;
import com.scxh.netwangqu.R;
public class MainActivity extends Activity {
//定义网址
private String http[] = {
"http://www.iyi8.com/uploadfile/2014/1215/20141215113901749.jpg",
"http://www.iyi8.com/uploadfile/2014/1209/20141209112742464.jpg",
"http://www.juxia.com/uploadfile/content/2012/2/20120210163653221.jpg",
"http://www.58game.cn/static/uploads/news/20130323/514d6f9c6c327.jpg",
"http://pic.zznews.cn/0/10/09/24/10092439_941510.jpg",
"http://ww2.sinaimg.cn/mw600/a8e0a0d3tw1e2luctga7cj.jpg",
"http://ww2.sinaimg.cn/bmiddle/6ba10ddagw1dwtr9f55onj.jpg",
"http://picview01.baomihua.com/photos/20120731/m_14_634793365268750000_14191008.jpg",
"http://www.gscn.com.cn/pic/0/10/05/62/10056208_236741.jpg",
"http://www.eeyy.com/uploadfile/2012/0319/20120319104210914.jpg",
"http://images.55bbs.com/55shuoimg/pic/11/08/2c/11082cda8aac14fce6e72da19746f726.jpg",
"http://cdn.t01.pic.sogou.com/3c28af542f2d49f7-f1ff85e490dc165f-062f5376e12039640d826e8667d7feaa.jpg" };
private Button mButton;
private GridView mGridView;
MyBasaAdaper myadapter;
ArrayList mlist = new ArrayList();
Handler handle = new Handler();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.gridbtn);
mGridView = (GridView) findViewById(R.id.gridv);
myadapter = new MyBasaAdaper(this);
mGridView.setAdapter(myadapter);
// myadapter.setData(getData());
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// ThreadfZ();
myadapter.setData(http);
}
});
}
// 封装线程
public void ThreadfZ() {
Thread t = new Thread(new Runnable() {
public void run() {
InputStream inputStream = null;
URL url = null;
// 用while循环取图片
int i = 0;
while (i < http.length) {
try {
url = new URL(http[i++]);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
try {
// 打开流
inputStream = url.openStream();
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
// 把取出的图片fangr
mlist.add(bitmap);
Log.e("list", mlist + "");
}
handle.post(new Runnable() {
public void run() {
// myadapter.setData(mlist);
}
});
}
});
t.start();
}
}
---------------------------------------------------------------------------------分割线-------------------------------------------------------------------------------
3、定义适配器Adaper继承BaseAdapter
package com.scxh.netwangqu.gridview;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import com.scxh.netwangqu.R;
public class MyBasaAdaper extends BaseAdapter {
private String[] mList = new String[] {};
private LayoutInflater mInflater;
private Context mcontext;
public MyBasaAdaper(Context context) {
mcontext = context;
mInflater = LayoutInflater.from(mcontext);
}
public void setData(String[] mlist) {
mList = mlist;
notifyDataSetChanged();
}
public int getCount() {
return mList.length;
}
public Object getItem(int position) {
return (String) mList[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(R.layout.gridimgview, null);
final ImageView iconImg = (ImageView) convertView
.findViewById(R.id.imgview);
new AsyncTask<String, Void, Bitmap>() {
protected Bitmap doInBackground(String... params) {
String httpUrl = (String) params[0]; // http://www.xx/mm.jpg
Bitmap bitmap = null;
URL url = null;
InputStream is = null;
InputStream inputStream;
try {
url = new URL(httpUrl);
inputStream = url.openStream();
bitmap = BitmapFactory.decodeStream(inputStream);
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bitmap;
}
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
iconImg.setImageBitmap(bitmap);
}
}.execute(mList[position]);
return convertView;
}
class ViewHolder {
ImageView iconImg = null;
}
}