Android 圖片異步加載
所謂圖片異步加載,意思是不用一次把圖片全部加載完,你可以叫它延遲加載,緩沖加載都行。
看看你有沒有這種需求:某篇文章圖片很多,如果在載入文章時就載入所有圖片,無疑會延緩載入速度,讓用戶等更久,所以,我想找這樣一種插件,讓網頁只加載瀏覽器視野范圍內的圖片,沒出現在范圍內的圖片就暫不加載,等用戶滑動滾動條時再逐步加載。lazyload就是用來實現這種效果。
package com.example.demo1;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import EntityBen.GoodsItem;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
Context context ;
ArrayList<GoodsItem> list ;
int index ;
public MyAdapter(Context con, ArrayList<GoodsItem> list) {
super();
this.context = con;
this.list = list;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
index = position ;
View view = convertView ;
ViewHolder vH;
if(view == null){
System.out.println("dddddddddd"+context);
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.tuangou_lv, null);
vH = new ViewHolder();
vH.name = (TextView) view.findViewById(R.id.goods_name);
vH.info = (TextView) view.findViewById(R.id.goods_infor);
vH.costPrice = (TextView) view.findViewById(R.id.cost_price);
vH.oriPrice = (TextView) view.findViewById(R.id.ori_price);
vH.moods = (TextView) view.findViewById(R.id.moods);
vH.imgtask = (ImageView) view.findViewById(R.id.goods_img);
view.setTag(vH);
}else
vH = (ViewHolder) view.getTag();
vH.name.setText(list.get(position).getName());
vH.info.setText(list.get(position).getInfo());
vH.costPrice.setText(list.get(position).getCostprice() + "");
vH.oriPrice.setText(list.get(position).getOriprice() + "");
vH.moods.setText(list.get(position).getMoods() + "人");
//當前行的圖片路徑 例如:http://192.168.0.148:9999/Music/aaa.jpg
String fileurl = list.get(position).getImageUrl();
//文件操作類實例化
FileUtils f = new FileUtils();
//通過字符串操作獲得當前的圖片名稱 ( aaa.jpg)
String fileName = fileurl.substring(fileurl.lastIndexOf("/") + 1);
// /mnt/sdcard/tt/aaa.jpg
String filePath = f.GetSDPATH() + "tt/"+fileName;
//驗證當前圖片是否已經存在 /mnt/sdcard/tt/ 中
if(f.IsFileExists("tt/"+fileName)){//如果存在
vH.imgtask.setImageBitmap(BitmapFactory.decodeFile(filePath));
}else{//如果不存在
vH.downloadImage=new DownLoadImage(vH.imgtask);
vH.downloadImage.execute(list.get(position).getImageUrl());
}
return view;
}
public static class ViewHolder{
private DownLoadImage downloadImage ;
private ImageView imgtask;
private TextView name , info , costPrice ,oriPrice , moods;
}
private class DownLoadImage extends AsyncTask<String, Void, Bitmap>{
Bitmap drawable;
ImageView imageView;
DownLoadImage (ImageView img){
this.imageView=img;
}
/**
* 此函數相當于 run函數
*/
@Override
protected Bitmap doInBackground(String... params) {
DownImg fileDownload=new DownImg();
//調用圖片下載函數,將圖片下載到本地,并返回此圖片的本地路徑
String TalkPath = fileDownload.DownLoadFile(params[0], "tt");
drawable = BitmapFactory.decodeFile(TalkPath);
return drawable; //相當于 Message
}
/**
* 當任務執行之前開始調用此方法,可以在這里顯示進度對話框
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
System.out.println("異步加載開始了");
}
/**
* 此方法在主線程執行,任務執行的結果作為此方法的參數返回。
*/
@Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
System.out.println("異步執行完畢了,返回");
if (result!=null) {
imageView.setImageBitmap(drawable);
}
}
}
public class DownImg {
HttpURLConnection httpconn = null;
public String DownLoadFile(String fileurl, String path) {
InputStream inputStream = null;
OutputStream outputStream = null;
File file = null;
String fileName = "";
String imgPath = "";
try {
fileName = fileurl.substring(fileurl.lastIndexOf("/") + 1);
FileUtils fileUtils = new FileUtils();
imgPath =fileUtils.GetSDPATH() + path + "/" + fileName;
System.out.println(fileurl + " " + imgPath);
if (fileUtils.IsFileExists(path + "/" + fileName)) {
System.out.println("已存在此圖片,無需下載");
return imgPath;
}
URL url = new URL(fileurl);// 獲得或者確定下載的路徑
HttpURLConnection httpconnCon = (HttpURLConnection) url
.openConnection();
inputStream = httpconnCon.getInputStream();// 獲得字節流
fileUtils.CreateSDDir(path);// 確定下載路徑(創建你需要的文件夾)
file = fileUtils.CreateSDFile(path + "/" + fileName);
System.out.println("ddddd---" + file);
outputStream = new FileOutputStream(file);
byte buffer[] = new byte[4 * 1024];
int length = 0;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
outputStream.flush();
} catch (Exception e) {
return null;
} finally {
try {
// inputStream.close();
// outputStream.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
return imgPath;
}
}
public class FileUtils {
private String SDPATH;
public FileUtils() {
SDPATH = Environment.getExternalStorageDirectory() + "/";
//"/mnt/sdcard/"
}
/**
* 獲得當前系統的SDPATH路徑字符
* */
public String GetSDPATH() {
return SDPATH;
}
/**
* 在SD卡上創建文件
* */
public File CreateSDFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
boolean isCreate = file.createNewFile();
return file;
}
/**
* 在SD卡上創建文件夾
* */
public File CreateSDDir(String dirName) {
File file = new File(SDPATH + dirName);
boolean isCreateDir = file.mkdir();
return file;
}
/**
* 判斷文件是否存在
* */
public boolean IsFileExists(String fileName) {
//"/mnt/sdcard/hhh/aaa.mp3"
File file = new File(SDPATH + fileName);
return file.exists();
}
}
} 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!