/**
* abstract class/interface defined to load image
* (Strategy Pattern used here)
*/
public interface BaseImageLoaderStrategy {
void loadImage (Context context , ImageLoader imageLoader );
}
/**
* using {@link Glide} to load image
* 参考:https://github.com/CameloeAnthony/Ant
*/
public class GlideImageLoaderStrategy implements BaseImageLoaderStrategy {
@ Override
public void loadImage (Context context , ImageLoader imageLoader ) {
//if currently not under wifi
if (!ImageLoaderUtil .wifiFlag ) {
loadNormal (context ,imageLoader );
return ;
}
int strategy = imageLoader .getWifiStrategy ();
if (strategy == ImageLoaderUtil .LOAD_STRATEGY_ONLY_WIFI ) {
int netType = ImageLoaderUtil .getNetWorkType (context );
//if wifi ,load pic
if (netType == ImageLoaderUtil .NETWORKTYPE_WIFI ) {
loadNormal (context ,imageLoader );
} else {
//if not wifi ,load cache
loadCache (context ,imageLoader );
}
} else {
//如果不是在wifi下才加载图片
loadNormal (context ,imageLoader );
}
}
/**
* load image with Glide
*/
private void loadNormal (Context context , ImageLoader imageLoader ) {
Glide .with (context ).load (imageLoader .getUrl ()).placeholder (imageLoader .getPlaceHolder ()).into (imageLoader .getImgView ());
}
/**
* load cache image with Glide
*/
private void loadCache (Context context , ImageLoader imageLoader ) {
//缓存策略解说:
//all:缓存源资源和转换后的资源
//none:不作任何磁盘缓存
//source:缓存源资源
//result:缓存转换后的资源
Glide .with (context ).load (imageLoader .getUrl ()).placeholder (imageLoader .getPlaceHolder ()).diskCacheStrategy (DiskCacheStrategy .ALL ).into (imageLoader .getImgView ());
}
}
PicassoImageLoaderStrategy
/**
* using Picasso to load image
*/
public class PicassoImageLoaderStrategy implements BaseImageLoaderStrategy {
@ Override
public void loadImage (Context context , ImageLoader imageLoader ) {
//Todo
}
}
/**
* use this class to load image,single instance
*/
public class ImageLoaderUtil {
public static final int PIC_LARGE = 0 ;
public static final int PIC_MEDIUM = 1 ;
public static final int PIC_SMALL = 2 ;
public static final int LOAD_STRATEGY_NORMAL = 0 ;
public static final int LOAD_STRATEGY_ONLY_WIFI = 1 ;
public static boolean wifiFlag = false ;
private BaseImageLoaderStrategy mStrategy ;
public ImageLoaderUtil () {
mStrategy = new GlideImageLoaderStrategy ();
}
public void loadImage (Context context , ImageLoader img ) {
mStrategy .loadImage (context , img );
}
public void setLoadImgStrategy (BaseImageLoaderStrategy strategy ) {
mStrategy = strategy ;
}
/**
* 没有网络
*/
public static final int NETWORKTYPE_INVALID = 0 ;
/**
* wap网络
*/
public static final int NETWORKTYPE_WAP = 1 ;
/**
* 2G网络
*/
public static final int NETWORKTYPE_2G = 2 ;
/**
* 3G和3G以上网络,或统称为快速网络
*/
public static final int NETWORKTYPE_3G = 3 ;
/**
* wifi网络
*/
public static final int NETWORKTYPE_WIFI = 4 ;
/**
* get the network type (wifi,wap,2g,3g)
*
* @param context
* @return
*/
public static int getNetWorkType (Context context ) {
int mNetWorkType = NETWORKTYPE_INVALID ;
ConnectivityManager manager = (ConnectivityManager ) context .getSystemService (Context .CONNECTIVITY_SERVICE );
NetworkInfo networkInfo = manager .getActiveNetworkInfo ();
if (networkInfo != null && networkInfo .isConnected ()) {
String type = networkInfo .getTypeName ();
if (type .equalsIgnoreCase ("WIFI" )) {
mNetWorkType = NETWORKTYPE_WIFI ;
} else if (type .equalsIgnoreCase ("MOBILE" )) {
String proxyHost = android .net .Proxy .getDefaultHost ();
mNetWorkType = TextUtils .isEmpty (proxyHost )
? (isFastMobileNetwork (context ) ? NETWORKTYPE_3G : NETWORKTYPE_2G )
: NETWORKTYPE_WAP ;
}
} else {
mNetWorkType = NETWORKTYPE_INVALID ;
}
return mNetWorkType ;
}
private static boolean isFastMobileNetwork (Context context ) {
TelephonyManager telephonyManager = (TelephonyManager ) context .getSystemService (Context .TELEPHONY_SERVICE );
switch (telephonyManager .getNetworkType ()) {
case TelephonyManager .NETWORK_TYPE_1xRTT :
return false ; // ~ 50-100 kbps
case TelephonyManager .NETWORK_TYPE_CDMA :
return false ; // ~ 14-64 kbps
case TelephonyManager .NETWORK_TYPE_EDGE :
return false ; // ~ 50-100 kbps
case TelephonyManager .NETWORK_TYPE_EVDO_0 :
return true ; // ~ 400-1000 kbps
case TelephonyManager .NETWORK_TYPE_EVDO_A :
return true ; // ~ 600-1400 kbps
case TelephonyManager .NETWORK_TYPE_GPRS :
return false ; // ~ 100 kbps
case TelephonyManager .NETWORK_TYPE_HSDPA :
return true ; // ~ 2-14 Mbps
case TelephonyManager .NETWORK_TYPE_HSPA :
return true ; // ~ 700-1700 kbps
case TelephonyManager .NETWORK_TYPE_HSUPA :
return true ; // ~ 1-23 Mbps
case TelephonyManager .NETWORK_TYPE_UMTS :
return true ; // ~ 400-7000 kbps
case TelephonyManager .NETWORK_TYPE_EHRPD :
return true ; // ~ 1-2 Mbps
case TelephonyManager .NETWORK_TYPE_EVDO_B :
return true ; // ~ 5 Mbps
case TelephonyManager .NETWORK_TYPE_HSPAP :
return true ; // ~ 10-20 Mbps
case TelephonyManager .NETWORK_TYPE_IDEN :
return false ; // ~25 kbps
case TelephonyManager .NETWORK_TYPE_LTE :
return true ; // ~ 10+ Mbps
case TelephonyManager .NETWORK_TYPE_UNKNOWN :
return false ;
default :
return false ;
}
}
}
public class ImageLoader {
private int type ; // (Big,Medium,small)
private Object url ; //url
private int placeHolder ; //placeholder when fail to load pics
private ImageView imgView ; //ImageView instantce
private int wifiStrategy ;//load strategy ,wheather under wifi
private ImageLoader (Builder builder ) {
this .type = builder .type ;
this .url = builder .url ;
this .placeHolder = builder .placeHolder ;
this .imgView = builder .imgView ;
this .wifiStrategy = builder .wifiStrategy ;
}
public int getType () {
return type ;
}
public Object getUrl () {
return url ;
}
public int getPlaceHolder () {
return placeHolder ;
}
public ImageView getImgView () {
return imgView ;
}
public int getWifiStrategy () {
return wifiStrategy ;
}
public static class Builder {
private int type ;
private Object url ;//url或者本地资源文件
private int placeHolder ;
private ImageView imgView ;
private int wifiStrategy ;
public Builder () {
this .type = ImageLoaderUtil .PIC_SMALL ;
this .url = "" ;
this .placeHolder = R .drawable .ic_photo_default ;
this .imgView = null ;
this .wifiStrategy = ImageLoaderUtil .LOAD_STRATEGY_NORMAL ;
}
public Builder type (int type ) {
this .type = type ;
return this ;
}
public Builder url (Object url ) {
this .url = url ;
return this ;
}
public Builder placeHolder (int placeHolder ) {
this .placeHolder = placeHolder ;
return this ;
}
public Builder imgView (ImageView imgView ) {
this .imgView = imgView ;
return this ;
}
public Builder strategy (int strategy ) {
this .wifiStrategy = strategy ;
return this ;
}
public ImageLoader build () {
return new ImageLoader (this );
}
}
}
@ Override
public void onBindViewHolder (RelatedGoodsViewHolder holder , int position ) {
ImageLoaderUtil imageLoaderUtil = new ImageLoaderUtil ();
ImageLoader imageLoader = new ImageLoader .Builder ()
.imgView (holder .thumbnail )
.url ("http://avatar.csdn.net/2/9/C/1_jdsjlzx.jpg" )
//.strategy(ImageLoaderUtil.LOAD_STRATEGY_ONLY_WIFI) 可以不写
.build ();
//imageLoaderUtil.setLoadImgStrategy(new GlideImageLoaderStrategy()); //这里可以更改图片加载框架
imageLoaderUtil .loadImage (mContext , imageLoader );
}