1.新建AndroID工程AsyncLoadPicture
新建布局文件activity_main.xml主界面为一个GrIDVIEw,还有其子项布局文件grIDvIEw_item.xml
2.功能主界面MainActivity.java,主代码如下
package com.example.asyncloadpicture;import java.util.ArrayList;import androID.app.Activity;import androID.content.Context;import androID.graphics.Bitmap;import androID.graphics.BitmapFactory;import androID.os.AsyncTask;import androID.os.Bundle;import androID.Widget.GrIDVIEw;/*** @author ymw* @data --*/public class MainActivity extends Activity {private GrIDVIEw grIDVIEwimg;private ArrayList<ImageEntity> imageList = new ArrayList<ImageEntity>();@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentVIEw(R.layout.activity_main);grIDVIEwimg = (GrIDVIEw) findVIEwByID(R.ID.grIDVIEwimg);initData();}private voID initData() {for (int i = ; i < ; i++) {ImageEntity b = new ImageEntity();b.setimage(BitmapFactory.decodeResource(getResources(),R.drawable.nopicture));imageList.add(b);}ImageAdapter adapter = new ImageAdapter(getApplicationContext(),imageList);grIDVIEwimg.setAdapter(adapter);String[] params = {"http://images.cnitblog.com/i///.png","p" };new ImageLoadTask(getApplicationContext(),adapter).execute(params);}/** 图片异步加载类 */public class ImageLoadTask extends AsyncTask<String,VoID,VoID> {private ImageAdapter adapter;public ImageLoadTask(Context context,ImageAdapter adapter) {this.adapter = adapter;}@OverrIDeprotected VoID doInBackground(String... params) {String url = params[];// 获取传来的参数,图片uri地址String p = params[];for (int i = ; i < adapter.getCount(); i++) {ImageEntity bean = (ImageEntity) adapter.getItem(i);Bitmap bitmap = BitmapFactory.decodeStream(Request.HandlerData(url));bean.setimage(bitmap);publishProgress(); // 通知去更新UI}return null;}public voID onProgressUpdate(VoID... voIDs) {if (isCancelled())return;// 更新UIadapter.notifyDataSetChanged();}}/** 图片实体 */public class ImageEntity {private Bitmap image;public Bitmap getimage() {return image;}public voID setimage(Bitmap image) {this.image = image;}}}
3.GrIDVIEw列表图片适配器代码ImageAdapter.java
package com.example.asyncloadpicture;import java.util.ArrayList;import androID.content.Context;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.BaseAdapter;import androID.Widget.ImageVIEw;import com.example.asyncloadpicture.MainActivity.ImageEntity;/*** * @author ymw* @data -- 图片适配器*/public class ImageAdapter extends BaseAdapter {private Context _context;private ArrayList<ImageEntity> _List;public ImageAdapter(Context context,ArrayList<ImageEntity> imageList) {this._context = context;this._List = imageList;}@OverrIDepublic int getCount() {return _List.size();}@OverrIDepublic Object getItem(int position) {return _List.get(position);}@OverrIDepublic long getItemID(int position) {return position;}@OverrIDepublic VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {LayoutInflater inflater = LayoutInflater.from(_context);convertVIEw = inflater.inflate(R.layout.grIDvIEw_item,null);ImageVIEw imgVIEw = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.imgVIEw);ImageEntity b = _List.get(position);// 如果有图片则读取,没有则跳过if (b.getimage() != null) {imgVIEw.setimageBitmap(b.getimage());}return convertVIEw;}}
4.请求网络读取图片的类Request.java
package com.example.asyncloadpicture;import java.io.IOException;import java.io.inputStream;import java.net.URL;import java.net.URLConnection;import androID.graphics.drawable.Drawable;import androID.util.Log;/*** 工具类,主要根据url读取图片返回流的方法* * @author ymw* */public class Request {public static inputStream HandlerData(String url) {inputStream inStream = null;try {URL FeedUrl = new URL(url);URLConnection conn = FeedUrl.openConnection();conn.setConnectTimeout( * );inStream = conn.getinputStream();} catch (Exception e) {e.printstacktrace();}return inStream;}/** 直接返回Drawable对象设置图片 */public static Drawable loadImageFromNetwork(String imageUrl) {Drawable drawable = null;try {// 可以在这里通过文件名来判断,是否本地有此图片drawable = Drawable.createFromStream(new URL(imageUrl).openStream(),"image.jpg");} catch (IOException e) {Log.d("test",e.getMessage());}if (drawable == null) {Log.d("test","null drawable");} else {Log.d("test","not null drawable");}return drawable;}}
以上内容给大家介绍了AndroID 中使用 AsyncTask 异步读取网络图片的相关知识,希望对大家有所帮助!
总结以上是内存溢出为你收集整理的Android 中使用 AsyncTask 异步读取网络图片全部内容,希望文章能够帮你解决Android 中使用 AsyncTask 异步读取网络图片所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)