修改图片加载工具类
This commit is contained in:
parent
b02a5e9b96
commit
028213e973
|
@ -10,11 +10,10 @@ import android.widget.ImageView;
|
||||||
import android.widget.ListView;
|
import android.widget.ListView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
import com.nostra13.universalimageloader.core.DisplayImageOptions;
|
|
||||||
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
|
|
||||||
import com.zftlive.android.R;
|
import com.zftlive.android.R;
|
||||||
import com.zftlive.android.base.BaseActivity;
|
import com.zftlive.android.base.BaseActivity;
|
||||||
import com.zftlive.android.base.BaseAdapter;
|
import com.zftlive.android.base.BaseAdapter;
|
||||||
|
import com.zftlive.android.tools.ToolImage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 异步加载图片示例DEMO,防止图片错位
|
* 异步加载图片示例DEMO,防止图片错位
|
||||||
|
@ -70,7 +69,6 @@ public class ImageListviewActivity extends BaseActivity {
|
||||||
"多途网络科技 15K 招聘前端开发工程师",
|
"多途网络科技 15K 招聘前端开发工程师",
|
||||||
"携程无线前端团队招聘 直接内部推荐(携程上海总部)"
|
"携程无线前端团队招聘 直接内部推荐(携程上海总部)"
|
||||||
};
|
};
|
||||||
private DisplayImageOptions mDisplayImageOptions;
|
|
||||||
private com.nostra13.universalimageloader.core.ImageLoader universalimageloader;
|
private com.nostra13.universalimageloader.core.ImageLoader universalimageloader;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -84,14 +82,8 @@ public class ImageListviewActivity extends BaseActivity {
|
||||||
mMyListViewAdapter = new MyListViewAdapter();
|
mMyListViewAdapter = new MyListViewAdapter();
|
||||||
mListView = (ListView)findViewById(R.id.lv_list);
|
mListView = (ListView)findViewById(R.id.lv_list);
|
||||||
|
|
||||||
mDisplayImageOptions = new DisplayImageOptions.Builder()
|
//图片异步加载器
|
||||||
.showStubImage(R.drawable.ic_launcher).cacheInMemory()
|
universalimageloader = ToolImage.initImageLoader(getApplicationContext());
|
||||||
.cacheOnDisc().build();
|
|
||||||
|
|
||||||
ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(this).enableLogging().build();
|
|
||||||
universalimageloader = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
|
|
||||||
universalimageloader.init(imageLoaderConfiguration);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -142,8 +134,7 @@ public class ImageListviewActivity extends BaseActivity {
|
||||||
// mImageLoader.get((String)rowData.get("imageUrl"), mImageListener);
|
// mImageLoader.get((String)rowData.get("imageUrl"), mImageListener);
|
||||||
|
|
||||||
//异步加载图片防止错位方法二:com.nostra13.universalimageloader.core.ImageLoader
|
//异步加载图片防止错位方法二:com.nostra13.universalimageloader.core.ImageLoader
|
||||||
universalimageloader.displayImage((String)rowData.get("imageUrl"), mViewHolder.iv_icon, mDisplayImageOptions);
|
universalimageloader.displayImage((String)rowData.get("imageUrl"), mViewHolder.iv_icon, ToolImage.getFadeOptions(R.drawable.default_icon,R.drawable.ic_launcher,R.drawable.ic_launcher));
|
||||||
|
|
||||||
mViewHolder.tv_title.setText((String)rowData.get("title"));
|
mViewHolder.tv_title.setText((String)rowData.get("title"));
|
||||||
return convertView;
|
return convertView;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,165 +1,138 @@
|
||||||
package com.zftlive.android.tools;
|
package com.zftlive.android.tools;
|
||||||
|
|
||||||
import android.content.Context;
|
import java.io.File;
|
||||||
import android.widget.ImageView;
|
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
|
||||||
|
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
|
||||||
|
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
|
||||||
|
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
|
||||||
import com.nostra13.universalimageloader.core.DisplayImageOptions;
|
import com.nostra13.universalimageloader.core.DisplayImageOptions;
|
||||||
import com.nostra13.universalimageloader.core.ImageLoader;
|
import com.nostra13.universalimageloader.core.ImageLoader;
|
||||||
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
|
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.FadeInBitmapDisplayer;
|
||||||
|
import com.nostra13.universalimageloader.utils.StorageUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图片加载工具类
|
||||||
|
* @author 曾繁添
|
||||||
|
* @version 1.0
|
||||||
|
*
|
||||||
|
*/
|
||||||
public abstract class ToolImage {
|
public abstract class ToolImage {
|
||||||
|
|
||||||
private static ImageLoader imageLoader;
|
private static ImageLoader imageLoader;
|
||||||
private static DisplayImageOptions displayImageOptions;
|
|
||||||
private static DisplayImageOptions displayImageOptions1;
|
|
||||||
private static DisplayImageOptions displayImageOptions2;
|
|
||||||
private static DisplayImageOptions displayImageOptions3;
|
|
||||||
private static DisplayImageOptions displayImageOptions4;
|
|
||||||
private static DisplayImageOptions displayImageOptions5;
|
|
||||||
|
|
||||||
public static void init(Context context, int stubImage,
|
/**
|
||||||
int imageForEmptyUri, int imageOnFail) {
|
* 初始化ImageLoader
|
||||||
ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(
|
*/
|
||||||
context)
|
public static ImageLoader initImageLoader(Context context) {
|
||||||
// 如果图片尺寸大于了这个参数,那么就会这按照这个参数对图片大小进行限制并缓存
|
// 获取到缓存的目录地址
|
||||||
// .memoryCacheExtraOptions(480, 800) // default=device screen
|
File cacheDir = StorageUtils.getOwnCacheDirectory(context,"ImageLoader/Cache");
|
||||||
// dimensions
|
// 创建配置ImageLoader,可以设定在Application,设置为全局的配置参数
|
||||||
// .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75)
|
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
|
||||||
// .taskExecutor(AsyncTask.THREAD_POOL_EXECUTOR)
|
.memoryCacheExtraOptions(480, 800)//缓存文件的最大长宽
|
||||||
// .taskExecutorForCachedImages(AsyncTask.THREAD_POOL_EXECUTOR)
|
// Can slow ImageLoader, use it carefully (Better don't use it)设置缓存的详细信息,最好不要设置这个
|
||||||
// .threadPoolSize(3) // default
|
// .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75,null)
|
||||||
// .threadPriority(Thread.NORM_PRIORITY - 1) // default
|
.threadPoolSize(3)// 线程池内加载的数量
|
||||||
// .tasksProcessingOrder(QueueProcessingType.FIFO) // default
|
.threadPriority(Thread.NORM_PRIORITY - 2)// 线程优先级
|
||||||
|
/*
|
||||||
|
* When you display an image in a small ImageView and later you
|
||||||
|
* try to display this image (from identical URI) in a larger
|
||||||
|
* ImageView so decoded image of bigger size will be cached in
|
||||||
|
* memory as a previous decoded image of smaller size. So the
|
||||||
|
* default behavior is to allow to cache multiple sizes of one
|
||||||
|
* image in memory. You can deny it by calling this method: so
|
||||||
|
* when some image will be cached in memory then previous cached
|
||||||
|
* size of this image (if it exists) will be removed from memory
|
||||||
|
* cache before.
|
||||||
|
*/
|
||||||
// .denyCacheImageMultipleSizesInMemory()
|
// .denyCacheImageMultipleSizesInMemory()
|
||||||
// .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
|
// You can pass your own memory cache implementation
|
||||||
// .memoryCacheSize(2 * 1024 * 1024)
|
.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 *1024))
|
||||||
// .discCache(
|
.memoryCacheSize(2 * 1024 * 1024)
|
||||||
// new
|
.discCacheSize(20 * 1024 * 1024)// 硬盘缓存50MB
|
||||||
// LimitedAgeDiscCache(Environment.getExternalStorageDirectory(),
|
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())// 将保存的时候的URI名称用MD5
|
||||||
// 5 * 60))
|
.tasksProcessingOrder(QueueProcessingType.LIFO)
|
||||||
// .discCache(new
|
.discCacheFileCount(100)// 缓存的File数量
|
||||||
// UnlimitedDiscCache(Environment.getExternalStorageDirectory()))
|
.discCache(new UnlimitedDiscCache(cacheDir))// 自定义缓存路径
|
||||||
// .discCacheSize(50 * 1024 * 1024)
|
//.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
|
||||||
// .discCacheFileCount(50)
|
//.imageDownloader(new BaseImageDownloader(context, 5 * 1000,30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
|
||||||
// .discCacheFileNameGenerator( new HashCodeFileNameGenerator())
|
.build();
|
||||||
// // default
|
|
||||||
// .imageDownloader(new BaseImageDownloader(context)) // default
|
|
||||||
// .imageDecoder(new BaseImageDecoder()) // default
|
|
||||||
// .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
|
|
||||||
// // default
|
|
||||||
.enableLogging().build();
|
|
||||||
ImageLoader.getInstance().init(imageLoaderConfiguration);
|
|
||||||
imageLoader = ImageLoader.getInstance();
|
|
||||||
|
|
||||||
displayImageOptions = new DisplayImageOptions.Builder()
|
// 全局初始化此配置
|
||||||
// 设置图片在下载期间显示的图片
|
imageLoader = ImageLoader.getInstance();
|
||||||
.showStubImage(stubImage) // 在显示真正的图片前,会加载这个资源
|
imageLoader.init(config);
|
||||||
// 设置图片Uri为空或是错误的时候显示的图片
|
|
||||||
.showImageForEmptyUri(imageForEmptyUri) // 空的Url时
|
return imageLoader;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取渐现显示选项
|
||||||
|
* @param loadingImageResId 加载期间显示的图片
|
||||||
|
* @param errorImageResid 加载错误时显示的图片
|
||||||
|
* @param emptyImageResId 空图片或者解析图片出错时显示的图片
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static DisplayImageOptions getFadeOptions(int loadingImageResId,int errorImageResid,int emptyImageResId ) {
|
||||||
|
DisplayImageOptions options = new DisplayImageOptions.Builder()
|
||||||
|
//设置图片在下载期间显示的图片
|
||||||
|
.showStubImage(loadingImageResId)
|
||||||
// 设置图片加载/解码过程中错误时候显示的图片
|
// 设置图片加载/解码过程中错误时候显示的图片
|
||||||
.showImageOnFail(imageOnFail)
|
.showImageOnFail(errorImageResid)
|
||||||
// .resetViewBeforeLoading() //
|
// 设置图片Uri为空或是错误的时候显示的图片
|
||||||
// .delayBeforeLoading(1000) // 延长1000ms 加载图片 (想不出来用在什么场景下)
|
.showImageForEmptyUri(emptyImageResId)
|
||||||
// 设置下载的图片是否缓存在内存中
|
// 设置下载的图片是否缓存在内存中
|
||||||
.cacheInMemory()
|
.cacheInMemory()
|
||||||
// 设置下载的图片是否缓存在SD卡中
|
// 设置下载的图片是否缓存在SD卡中
|
||||||
// .cacheOnDisc()
|
.cacheOnDisc()
|
||||||
// .preProcessor(...)
|
/**设置图片缩放方式:
|
||||||
// .postProcessor(...)
|
EXACTLY :图像将完全按比例缩小到目标大小
|
||||||
// .extraForDownloader(...) //可以向加载器携带一些参数
|
EXACTLY_STRETCHED:图片会缩放到目标大小完全
|
||||||
// .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) //
|
IN_SAMPLE_INT:图像将被二次采样的整数倍
|
||||||
// default
|
IN_SAMPLE_POWER_OF_2:图片将降低2倍,直到下一减少步骤,使图像更小的目标大小
|
||||||
// .bitmapConfig(Bitmap.Config.ARGB_8888) // default
|
NONE:图片不会调整
|
||||||
// .decodingOptions(...)
|
***/
|
||||||
// .displayer(new SimpleBitmapDisplayer()) // default
|
.imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
|
||||||
// .handler(new Handler()) // default
|
// 设置图片的解码类型
|
||||||
|
.bitmapConfig(Bitmap.Config.RGB_565)
|
||||||
|
// 设置图片下载前的延迟
|
||||||
|
.delayBeforeLoading(100)
|
||||||
|
// delayInMillis为你设置的延迟时间
|
||||||
|
// 设置图片加入缓存前,对bitmap进行设置
|
||||||
|
// .preProcessor(BitmapProcessor preProcessor)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图片显示方式:
|
||||||
|
* RoundedBitmapDisplayer(int roundPixels)设置圆角图片
|
||||||
|
* FakeBitmapDisplayer()这个类什么都没做
|
||||||
|
* FadeInBitmapDisplayer(int durationMillis)设置图片渐显的时间
|
||||||
|
* SimpleBitmapDisplayer()正常显示一张图片
|
||||||
|
**/
|
||||||
|
.displayer(new FadeInBitmapDisplayer(100))// 渐显--设置图片渐显的时间
|
||||||
.build();
|
.build();
|
||||||
|
return options;
|
||||||
// //设置图片在下载前是否重置,复位
|
|
||||||
// resetViewBeforeLoading()
|
|
||||||
// //设置图片的解码类型
|
|
||||||
// bitmapConfig(Bitmap.Config.RGB_565)
|
|
||||||
// //设置图片的解码配置
|
|
||||||
// decodingOptions(android.graphics.BitmapFactory.Options
|
|
||||||
// decodingOptions)
|
|
||||||
// //设置图片下载前的延迟
|
|
||||||
// delayBeforeLoading(int delayInMillis)
|
|
||||||
// //设置额外的内容给ImageDownloader
|
|
||||||
// extraForDownloader(Object extra)
|
|
||||||
// //设置图片加入缓存前,对bitmap进行设置
|
|
||||||
// preProcessor(BitmapProcessor preProcessor)
|
|
||||||
// //设置显示前的图片,显示后这个图片一直保留在缓存中
|
|
||||||
// postProcessor(BitmapProcessor postProcessor)
|
|
||||||
// //设置图片以如何的编码方式显示
|
|
||||||
// imageScaleType(ImageScaleType imageScaleType)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void settingInit1(int stubImage, int imageForEmptyUri,
|
/**
|
||||||
int imageOnFail) {
|
* 获取默认显示配置选项
|
||||||
displayImageOptions1 = new DisplayImageOptions.Builder()
|
*/
|
||||||
.showStubImage(stubImage)
|
public static DisplayImageOptions getDefaultOptions(){
|
||||||
.showImageForEmptyUri(imageForEmptyUri)
|
return DisplayImageOptions.createSimple();
|
||||||
.showImageOnFail(imageOnFail).cacheInMemory().build();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void settingInit2(int stubImage, int imageForEmptyUri,
|
|
||||||
int imageOnFail) {
|
|
||||||
displayImageOptions2 = new DisplayImageOptions.Builder()
|
|
||||||
.showStubImage(stubImage)
|
|
||||||
.showImageForEmptyUri(imageForEmptyUri)
|
|
||||||
.showImageOnFail(imageOnFail).cacheInMemory().build();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void settingInit3(int stubImage, int imageForEmptyUri,
|
|
||||||
int imageOnFail) {
|
|
||||||
displayImageOptions3 = new DisplayImageOptions.Builder()
|
|
||||||
.showStubImage(stubImage)
|
|
||||||
.showImageForEmptyUri(imageForEmptyUri)
|
|
||||||
.showImageOnFail(imageOnFail).cacheInMemory().build();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void settingInit4(int stubImage, int imageForEmptyUri,
|
|
||||||
int imageOnFail) {
|
|
||||||
displayImageOptions4 = new DisplayImageOptions.Builder()
|
|
||||||
.showStubImage(stubImage)
|
|
||||||
.showImageForEmptyUri(imageForEmptyUri)
|
|
||||||
.showImageOnFail(imageOnFail).cacheInMemory().build();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void settingInit5(int stubImage, int imageForEmptyUri,
|
|
||||||
int imageOnFail) {
|
|
||||||
displayImageOptions5 = new DisplayImageOptions.Builder()
|
|
||||||
.showStubImage(stubImage)
|
|
||||||
.showImageForEmptyUri(imageForEmptyUri)
|
|
||||||
.showImageOnFail(imageOnFail).cacheInMemory().build();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void displayImage(String url, ImageView imageView) {
|
|
||||||
imageLoader.displayImage(url, imageView, displayImageOptions);
|
|
||||||
}
|
|
||||||
public static void displayImage1(String url, ImageView imageView) {
|
|
||||||
imageLoader.displayImage(url, imageView, displayImageOptions1);
|
|
||||||
}
|
|
||||||
public static void displayImage2(String url, ImageView imageView) {
|
|
||||||
imageLoader.displayImage(url, imageView, displayImageOptions2);
|
|
||||||
}
|
|
||||||
public static void displayImage3(String url, ImageView imageView) {
|
|
||||||
imageLoader.displayImage(url, imageView, displayImageOptions3);
|
|
||||||
}
|
|
||||||
public static void displayImage4(String url, ImageView imageView) {
|
|
||||||
imageLoader.displayImage(url, imageView, displayImageOptions4);
|
|
||||||
}
|
|
||||||
public static void displayImage5(String url, ImageView imageView) {
|
|
||||||
imageLoader.displayImage(url, imageView, displayImageOptions5);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除缓存
|
||||||
|
*/
|
||||||
public static void clearCache() {
|
public static void clearCache() {
|
||||||
imageLoader.clearMemoryCache();
|
imageLoader.clearMemoryCache();
|
||||||
|
imageLoader.clearDiscCache();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ImageLoader getImageLoader(){
|
||||||
|
return imageLoader;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue