Skip to content

Commit

Permalink
glide image loader
Browse files Browse the repository at this point in the history
  • Loading branch information
zjlong committed Aug 1, 2016
1 parent 3ae6233 commit d8f5d17
Show file tree
Hide file tree
Showing 13 changed files with 387 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void onResponse(Call<Classify> call, Response<Classify> response) {

@Override
public void onFailure(Call<Classify> call, Throwable t) {
onFaailer(t.getMessage());
onFailer(t.getMessage());
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.racofix.view.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.ImageView;

import com.android.core.adapter.RecyclerAdapter;
import com.android.core.adapter.RecyclerViewHolder;
import com.bumptech.glide.Glide;
import com.android.core.widget.glide.ImageLoader;
import com.android.core.widget.glide.ImageLoaderUtil;
import com.racofix.R;
import com.racofix.model.repo.Classify;
import com.racofix.view.activity.SwipBackActivity;
Expand All @@ -30,8 +30,14 @@ public HomeRecyclerAdapter(Context context, int layoutId, List<Classify.TngouEnt

@Override
public void convert(final RecyclerViewHolder holder, final Classify.TngouEntity item) {
Uri uri = Uri.parse(URL_Base + item.getImg());
Glide.with(mContext).load(uri).into((ImageView) holder.getView(R.id.sv_classitfy_img));
// Uri uri = Uri.parse(URL_Base + item.getImg());
String url = URL_Base + item.getImg();
ImageView view = holder.getView(R.id.sv_classitfy_img);
ImageLoaderUtil.getInstance().loadCircleImage(mContext, new ImageLoader.Builder()
.url(url)
.placeHolder(R.color.abc_theme_black_7f)
.imgView(view)
.build());
holder.setText(R.id.sv_classitfy_des, item.getTitle());
holder.setOnClickListener(R.id.sv_classitfy_img, new View.OnClickListener() {
@Override
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/item_compete_classitfy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@color/abc_theme_black_7f"
android:scaleType="centerCrop" />
android:scaleType="fitStart" />

<TextView
android:id="@+id/sv_classitfy_des"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,23 @@ public Response intercept(Chain chain) throws IOException {
request = request.newBuilder()
.cacheControl(CacheControl.FORCE_CACHE)
.build();
Log.d("com.android.core", "no network");
}

Response originalResponse = chain.proceed(request);
Response response = chain.proceed(request);

if (NetWorkHelper.isNetConnected(MainApp.getContext())) {
//有网的时候读接口上的@Headers里的配置,你可以在这里进行统一的设置
String cacheControl = request.cacheControl().toString();
return originalResponse.newBuilder()
.header("Cache-Control", cacheControl)
int maxAge = 60 * 60; // read from cache for 1 minute
response.newBuilder()
.removeHeader("Pragma")
.header("Cache-Control", "public, max-age=" + maxAge)
.build();
} else {
return originalResponse.newBuilder()
.header("Cache-Control", "public, only-if-cached, max-stale=2419200")
int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
response.newBuilder()
.removeHeader("Pragma")
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
.build();
}
return response;
}
}
4 changes: 2 additions & 2 deletions core/src/main/java/com/android/core/api/OKHttpHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public static OkHttpClient create() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

File cacheFile = new File(MainApp.getInstance().getCacheDir(), "android");
Cache cache = new Cache(cacheFile, 1024 * 1024 * 100); //100Mb
File cacheFile = new File(MainApp.getInstance().getCacheDir(), "OkHttpCache");
Cache cache = new Cache(cacheFile, 10 * 1024 * 1024); //100Mb

OkHttpClient okHttpClient = null;
try {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/com/android/core/control/Glides.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import android.widget.ImageView;

import com.android.core.R;
import com.android.core.widget.GlideCircleTransform;
import com.android.core.widget.glide.GlideCircleTransform;
import com.bumptech.glide.Glide;

/**
Expand Down
165 changes: 118 additions & 47 deletions core/src/main/java/com/android/core/control/NetWorkHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -14,62 +16,131 @@
*/
public class NetWorkHelper {

/**
* 检测网络是否连接
*/
public static boolean isNetConnected(Context context) {
//没有网络
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;


//监测网络是否连接
public static boolean isNetConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
NetworkInfo[] infos = cm.getAllNetworkInfo();
if (infos != null) {
for (NetworkInfo ni : infos) {
if (ni.isConnected()) {
return true;
}
}
}
}
return false;
}

/**
* 检测wifi是否连接
*/
public static boolean isWifiConnected(Context context) {
if (cm != null) {
NetworkInfo[] infos = cm.getAllNetworkInfo();
if (infos != null) {
for (NetworkInfo ni : infos) {
if (ni.isConnected()) {
return true;
}
}
}
}
return false;
}

// 监测wifi 连接是否可用
public static boolean isWifiConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
}
return false;
}

/**
* 检测3G是否连接
*/
public static boolean is3gConnected(Context context) {
if (cm != null) {
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
}
return false;
}

//检测3G是否连接
public static boolean is3gConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
}
}
return false;
}

/**
* 判断网址是否有效
*/
if (cm != null) {
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
}
}
return false;
}

//判断网址是否有效
public static boolean isLinkAvailable(String link) {
Pattern pattern = Pattern.compile("^(http://|https://)?((?:[A-Za-z0-9]+-[A-Za-z0-9]+|[A-Za-z0-9]+)\\.)+([A-Za-z]+)[/\\?\\:]?.*$",Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile("^(http://|https://)?((?:[A-Za-z0-9]+-[A-Za-z0-9]+|[A-Za-z0-9]+)\\.)+([A-Za-z]+)[/\\?\\:]?.*$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(link);
if (matcher.matches()) {
return true;
}
return false;
}

// 判断网络类型
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;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.android.core.widget.glide;

import android.content.Context;

/**
* Created by Anthony on 2016/3/3.
* Class Note:
* abstract class/interface defined to load image
* (Strategy Pattern used here)
*/
public interface BaseImageLoaderStrategy {
void loadImage(Context ctx, ImageLoader img);
void loadCircleImage(Context ctx, ImageLoader img);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.android.core.widget;
package com.android.core.widget.glide;

import android.content.Context;
import android.graphics.Bitmap;
Expand Down
Loading

0 comments on commit d8f5d17

Please sign in to comment.