ImageLoader,多條目載入
工具類
package utils;
import android.os.AsyncTask;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
- Created by Administrator on 2018/11/8 0008.
*/
public class HttpUtils {
private static final String TAG = “HttpUtils*****”;
private static HttpUtils httpUtils=null;
private HttpLoadListener httpLoadListener;
private HttpUtils(){ } public static HttpUtils getHttpUtils(){ if (httpUtils==null){ httpUtils=new HttpUtils(); } return httpUtils; } //get public void get(String url){ new MyTask().execute(url); } class MyTask extends AsyncTask<String,Void,String>{ @Override protected String doInBackground(String... strings) { String url=strings[0]; try { URL url1=new URL(url); HttpURLConnection connection= (HttpURLConnection) url1.openConnection(); // connection.setConnectTimeout(5000); if (connection.getResponseCode()==200){ InputStream inputStream=connection.getInputStream(); String json=inString(inputStream); return json; }else{ httpLoadListener.loadError("網路載入失敗"); } } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); // Log.d(TAG, "onPostExecute: "+s); httpLoadListener.loadSuccess(s); } } public interface HttpLoadListener{ void loadSuccess(String json); void loadError(String error); } public void setOnHttpLoadListener(HttpLoadListener httpLoadListener){ this.httpLoadListener=httpLoadListener; } public static String inString(InputStream inputStream) throws IOException { InputStreamReader in=new InputStreamReader(inputStream); int len=0; char[] chars=new char[1024]; StringBuffer stringBuffer=new StringBuffer(); while ((len=in.read(chars))!=-1){ String s=new String(chars,0,len); stringBuffer.append(s); } return stringBuffer.toString(); }
}
2. Imageloader的使用
package com.bwei.yuekao1.app;
import android.app.Application;
import android.content.Context;
import android.os.Environment;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;
import java.io.File;
/**
- Lru
- 演算法:
- 最近最多使用----回收的物件,是指的圖片
*/
public class MyApp extends Application {
private static Context context;
@Override
public void onCreate() {
super.onCreate();
context = this;
// 初始化ImageLoader
intImageLoader();
}
public static Context getContext() {
return context;
}
private void intImageLoader() {
ImageLoader.getInstance().init(getConifg1(this));
}
// options 配置載入圖片時候的東西
public static DisplayImageOptions getOptions() {
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheInMemory(true)// default 設定下載的圖片是否快取在記憶體中
.cacheOnDisk(true)// default 設定下載的圖片是否快取在SD卡中
.build();
return options;
}
// 自己定義ImageLoader的配置資訊
public ImageLoaderConfiguration config(Context context) {
File cacheDir = StorageUtils.getCacheDirectory(context); //快取資料夾路徑
//
// File myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/myCache");
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
//
// .threadPoolSize(3)// default 執行緒池內載入的數量
//
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))//可以通過自己的記憶體快取實現
.memoryCacheSize(2 * 1024 * 1024) // 記憶體快取的最大值
.memoryCacheSizePercentage(13)// default
.diskCache(new UnlimitedDiskCache(cacheDir)) // default 可以自定義快取路徑
.diskCacheSize(50 * 1024 * 1024)// 50 Mb sd卡(本地)快取的最大值
.diskCacheFileCount(100) // 可以快取的檔案數量
// default為使用HASHCODE對UIL進行加密命名, 還可以用MD5(new Md5FileNameGenerator())加密
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
.imageDownloader(new BaseImageDownloader(context)) // default
// .imageDecoder(new BaseImageDecoder()) // default
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())// default
.writeDebugLogs()// 列印debug log
.build();//開始構建
return config;
}
//簡化版
public ImageLoaderConfiguration getConifg1(Context context) {
File cacheDir = StorageUtils.getCacheDirectory(context); //快取資料夾路徑
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))//可以通過自己的記憶體快取實現
.memoryCacheSize(2 * 1024 * 1024) // 記憶體快取的最大值
.diskCache(new UnlimitedDiskCache(cacheDir)) // default 可以自定義快取路徑
.diskCacheSize(50 * 1024 * 1024)// 50 Mb sd卡(本地)快取的最大值
.build();//開始構建
return config;
}
}
3.多條目載入
package com.bwei.day07_multi_listview;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.bwei.day07_multi_listview.app.MyApp;
import com.bwei.day07_multi_listview.beans.SheHuiBean;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
/**
- 條目分類
-
- — type 0
- 2----------------1
- 3----------------2
*/
public class MyAdapter extends BaseAdapter {
private Context context;
private List<SheHuiBean.ResultBean.DataBean> list;
public MyAdapter(Context context, List<SheHuiBean.ResultBean.DataBean> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@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) {
// 獲取條目的型別
int type = getItemViewType(position);
// 根據不同的型別載入不同的條目
switch (type) {
case 0://一張圖片
ViewHolder1 viewHolder1;
if (convertView == null) {
convertView = View.inflate(context, R.layout.item_pic1, null);
ImageView imageView1 = convertView.findViewById(R.id.item_pic1);
TextView textView1 = convertView.findViewById(R.id.item_title1);
viewHolder1 = new ViewHolder1(imageView1, textView1);
convertView.setTag(viewHolder1);
} else {
viewHolder1 = (ViewHolder1) convertView.getTag();
}
// 賦值
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(), viewHolder1.getImageView1(), MyApp.getOptions());
viewHolder1.getTextView1().setText(list.get(position).getTitle());
break;
case 1://兩張圖片
ViewHolder2 viewHolder2;
if (convertView == null) {
convertView = View.inflate(context, R.layout.item_pic2, null);
ImageView imageView21 = convertView.findViewById(R.id.item_pic21);
ImageView imageView22 = convertView.findViewById(R.id.item_pic22);
TextView textView21 = convertView.findViewById(R.id.item_title1);
viewHolder2 = new ViewHolder2(imageView21, imageView22, textView21);
convertView.setTag(viewHolder2);
} else {
viewHolder2 = (ViewHolder2) convertView.getTag();
}
// 賦值
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(), viewHolder2.getImageView21(), MyApp.getOptions());
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s02(), viewHolder2.getImageView22(), MyApp.getOptions());
viewHolder2.getTextView21().setText("");
break;
case 2://三張圖片
ViewHolder3 viewHolder3;
if (convertView == null) {
convertView = View.inflate(context, R.layout.item_pic3, null);
ImageView imageView31 = convertView.findViewById(R.id.item_pic31);
ImageView imageView32 = convertView.findViewById(R.id.item_pic32);
ImageView imageView33 = convertView.findViewById(R.id.item_pic33);
TextView textView31 = convertView.findViewById(R.id.item_title1);
viewHolder3 = new ViewHolder3(imageView31, imageView32, imageView33, textView31);
convertView.setTag(viewHolder3);
} else {
viewHolder3 = (ViewHolder3) convertView.getTag();
}
// 賦值
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(), viewHolder3.getImageView31(), MyApp.getOptions());
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s02(), viewHolder3.getImageView32(), MyApp.getOptions());
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s03(), viewHolder3.getImageView33(), MyApp.getOptions());
viewHolder3.getTextView31().setText(list.get(position).getTitle());
break;
}
return convertView;
}
// 一張圖片的ViewHolder1
class ViewHolder1 {
private ImageView imageView1;
private TextView textView1;
public ViewHolder1(ImageView imageView1, TextView textView1) {
this.imageView1 = imageView1;
this.textView1 = textView1;
}
public ImageView getImageView1() {
return imageView1;
}
public void setImageView1(ImageView imageView1) {
this.imageView1 = imageView1;
}
public TextView getTextView1() {
return textView1;
}
public void setTextView1(TextView textView1) {
this.textView1 = textView1;
}
}
// 兩張圖片的ViewHolder
class ViewHolder2 {
private ImageView imageView21;
private ImageView imageView22;
private TextView textView21;
public ViewHolder2(ImageView imageView21, ImageView imageView22, TextView textView21) {
this.imageView21 = imageView21;
this.imageView22 = imageView22;
this.textView21 = textView21;
}
public ImageView getImageView21() {
return imageView21;
}
public void setImageView21(ImageView imageView21) {
this.imageView21 = imageView21;
}
public ImageView getImageView22() {
return imageView22;
}
public void setImageView22(ImageView imageView22) {
this.imageView22 = imageView22;
}
public TextView getTextView21() {
return textView21;
}
public void setTextView21(TextView textView21) {
this.textView21 = textView21;
}
}
public class ViewHolder3 {
private ImageView imageView31;
private ImageView imageView32;
private ImageView imageView33;
private TextView textView31;
public ViewHolder3(ImageView imageView31, ImageView imageView32, ImageView imageView33, TextView textView31) {
this.imageView31 = imageView31;
this.imageView32 = imageView32;
this.imageView33 = imageView33;
this.textView31 = textView31;
}
public ImageView getImageView31() {
return imageView31;
}
public void setImageView31(ImageView imageView31) {
this.imageView31 = imageView31;
}
public ImageView getImageView32() {
return imageView32;
}
public void setImageView32(ImageView imageView32) {
this.imageView32 = imageView32;
}
public ImageView getImageView33() {
return imageView33;
}
public void setImageView33(ImageView imageView33) {
this.imageView33 = imageView33;
}
public TextView getTextView31() {
return textView31;
}
public void setTextView31(TextView textView31) {
this.textView31 = textView31;
}
}
// 要顯示多少型別的條目
@Override
public int getViewTypeCount() {
return 3;
}
@Override
public int getItemViewType(int position) {
// 獲取條目資料
SheHuiBean.ResultBean.DataBean dataBean = list.get(position);
// 從資料裡面取出圖片
String thumbnail_pic_s = dataBean.getThumbnail_pic_s();
String thumbnail_pic_s02 = dataBean.getThumbnail_pic_s02();
String thumbnail_pic_s03 = dataBean.getThumbnail_pic_s03();
int type = 0;//區域性變數,預設是0
if (!TextUtils.isEmpty(thumbnail_pic_s)) {
type = 0;
}
if (!TextUtils.isEmpty(thumbnail_pic_s02)) {
type = 1;
}
if (!TextUtils.isEmpty(thumbnail_pic_s03)) {
type = 2;
}
return type;
}
}