Imageloader框架

package adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView; import com.example.examday08.R;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
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.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader; import java.io.File;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List; import bean0.Result; /**
* Created by lenovo on 2017/12/6.
*/ public class MyAdapter extends BaseAdapter {
private List<Result.NewslistBean>list;
private Context context; public MyAdapter(List<Result.NewslistBean> list, Context context) {
this.list = list;
this.context = context;
File cacheDir= new File(Environment.getExternalStorageDirectory().getPath()+"/aaa");//自定义 sd卡的缓存路径 //第一步:对ImageLoader进行初使化
ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(context)
.memoryCacheExtraOptions(320, 450) // default = device screen dimensions 内存缓存文件的最大长宽
.diskCacheExtraOptions(480, 800, null) // 本地缓存的详细信息(缓存的最大长宽),最好不要设置这个
.threadPriority(Thread.NORM_PRIORITY - 2) // default 设置当前线程的优先级
.tasksProcessingOrder(QueueProcessingType.FIFO) // default
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new LruMemoryCache(10 * 1024 * 1024)) //可以通过自己的内存缓存实现
.memoryCacheSize(2 * 1024 * 1024) // 内存缓存的最大值
.memoryCacheSizePercentage(13) // default .diskCache(new UnlimitedDiscCache(cacheDir)) // 磁盘缓存 --default 可以自定义缓存路径 .diskCacheSize(20 * 1024 * 1024) // 50 Mb sd卡(本地)缓存的最大值 .diskCacheFileCount(100) // 可以缓存的文件数量
// default为使用HASHCODE对UIL进行加密命名, 还可以用MD5(new Md5FileNameGenerator())加密
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
.imageDownloader(new BaseImageDownloader(context)) // default
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.writeDebugLogs() // 打印debug log
.build(); ImageLoader.getInstance().init(configuration); } @Override
public int getCount() {
return list.size();
} @Override
public Object getItem(int i) {
return list.get(i);
} @Override
public long getItemId(int i) {
return i;
} @Override
public View getView(int i, View view, ViewGroup viewGroup) { view =View.inflate(context, R.layout.item,null);
TextView text = view.findViewById(R.id.text1);
ImageView img1 = view.findViewById(R.id.img); text.setText(list.get(i).getTitle());
//第二步:进行图片的显示配置
DisplayImageOptions options=new DisplayImageOptions.Builder()
.cacheInMemory(true)//使用内存缓存
.cacheOnDisk(true)//使用磁盘缓存 .showImageOnLoading(R.mipmap.ic_launcher)//设置正在下载的图片
.showImageForEmptyUri(R.mipmap.ic_launcher)//url为空或请求的资源不存在时
.showImageOnFail(R.mipmap.ic_launcher)//下载失败时显示的图片 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片色彩模式
.imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放模式===imageView,,ScaleType
.displayer(new RoundedBitmapDisplayer(100))//设置图片圆角显示 弧度
.build(); //第三步:下载图片
ImageLoader.getInstance().displayImage(list.get(i).getPicUrl(), img1, options);
// new MyTask(img1).execute(list.get(i).getPicUrl());
return view;
}
class MyTask extends AsyncTask<String,Void,Bitmap> {
private ImageView img; public MyTask(ImageView img) {
this.img = img;
} @Override
protected Bitmap doInBackground(String... strings) {
Bitmap bitmap = null;
StringBuilder builder =new StringBuilder();
try {
URL url = new URL(strings[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
if (connection.getResponseCode()==200){
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream); } } catch (Exception e) {
e.printStackTrace();
}
return bitmap;
} @Override
protected void onPostExecute(Bitmap s) {
img.setImageBitmap(s); }
}
}
上一篇:VMware中linux配置1-安装VMware tool 共享文件夹


下一篇:Influxdb+Grafana+Telegraf及docker中运行