Explorar el Código

使用二维码预下载来优化二维码加载速度

zengjiebin hace 7 años
padre
commit
2725bb2c97

+ 6 - 1
app/src/main/java/com/sheep/gamegroup/util/AppUtil.java

@@ -10,6 +10,7 @@ import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 import android.view.WindowManager;
 
+import java.io.UnsupportedEncodingException;
 import java.net.URLEncoder;
 import java.util.Locale;
 
@@ -24,7 +25,11 @@ public class AppUtil {
     }
 
     public static String getQRLink(String share_link,int w) {
-        share_link = URLEncoder.encode(share_link);
+        try {
+            share_link = URLEncoder.encode(share_link,"GBK");
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        }
 //        String link = BASE_QR + share_link +"&w="+w;
 //        LogUtil.println("link", link);
         return getQRLink(share_link, w, w);

+ 34 - 3
app/src/main/java/com/sheep/gamegroup/util/GlideImageLoader.java

@@ -1,12 +1,15 @@
 package com.sheep.gamegroup.util;
 
+import android.content.Intent;
 import android.graphics.Color;
+import android.net.Uri;
 import android.text.TextUtils;
 import android.widget.ImageView;
 
 import com.bumptech.glide.Glide;
 import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
 import com.bumptech.glide.request.RequestOptions;
+import com.sheep.gamegroup.model.api.BaseMessageConverter;
 import com.sheep.gamegroup.util.glide.GlideCircleTransformWithBorder;
 import com.sheep.jiuyan.samllsheep.R;
 import com.sheep.jiuyan.samllsheep.SheepApp;
@@ -16,6 +19,7 @@ import com.zhy.http.okhttp.callback.FileCallBack;
 
 import java.io.File;
 import java.util.HashMap;
+import java.util.Locale;
 import java.util.Map;
 
 import okhttp3.Call;
@@ -88,9 +92,31 @@ public class GlideImageLoader {
                 .into(imageView);
     }
 
+
+    public static void downloadAndSetImage(final ImageView imageView, final String link, final int w) {
+        downLoadImage(String.format(Locale.CHINA, "%d", link.hashCode()), link, new Action1<File>() {
+            @Override
+            public void call(File file) {
+                if(imageView != null) {
+                    if (file == null)
+                        Glide.with(SheepApp.getInstance())
+                                .load(link)
+                                .apply(new RequestOptions().override(w, w))
+                                .into(imageView);
+                    else
+                        Glide.with(SheepApp.getInstance())
+                                .load(file)
+                                .apply(new RequestOptions().override(w, w))
+                                .into(imageView);
+                }
+
+            }
+        });
+
+    }
     public static void downLoadImage(String name, String link, final Action1<File> action1) {
         String fileName = name + ".png";
-        final File file = new File(ClassFileHelper.DIR, fileName);
+        File file = new File(ClassFileHelper.DIR, fileName);
         if(file.exists()){
             if(action1 != null){
                 action1.call(file);
@@ -107,10 +133,15 @@ public class GlideImageLoader {
 
             @Override
             public void onResponse(File response, int id) {
-                if(response.exists())
-                    if(action1 != null){
+                if(response.exists()) {
+                    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
+                    Uri uri = Uri.fromFile(response);
+                    intent.setData(uri);
+                    SheepApp.getInstance().sendBroadcast(intent); // 发送广播通知相册
+                    if (action1 != null) {
                         action1.call(response);
                     }
+                }
             }
         });
     }

+ 28 - 45
app/src/main/java/com/sheep/gamegroup/util/Jump2View.java

@@ -247,20 +247,17 @@ public class Jump2View {
     }
 
     /**
-     * 进入主页前需要检查协议或者用户标签
+     * 正常进入
+     * 进入主页前需要检查是否需要绑定手机号
      * @param activity
      */
     public void checkPhoneOrGoHomePage(final Activity activity) {
         CommonUtil.getInstance().updateUserInfo(new Action1<UserEntity>() {
             @Override
             public void call(UserEntity userEntity) {
-                if(userEntity != null && !TextUtils.isEmpty(userEntity.getShareLink("other"))) {
-//                                GlideImageLoader.downLoadImage(userEntity.getShare_link().hashCode(), AppUtil.getQRLink(userEntity.getShare_link(), 800), null);
-//                                //PersionInfoAct.java与AskGetMoneyAct.java中分享二维码时的图片预下载
-                    Glide.with(activity)
-                            .load(AppUtil.getQRLink(userEntity.getShareLink("other"), 800))
-                            .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.RESOURCE))
-                            .preload(800, 800);
+                if(userEntity != null && !TextUtils.isEmpty(userEntity.getShare_link())) {
+                    //PersionInfoAct.java与AskGetMoneyAct.java中分享二维码时的图片预下载
+                    GlideImageLoader.downloadAndSetImage(null, AppUtil.getQRLink(userEntity.getShareLink(DialogShare.FACE_TO_FACE), 800), 800);
                 }
 
                 if(userEntity != null && userEntity.canBindMobile()
@@ -276,48 +273,34 @@ public class Jump2View {
         });
     }
     /**
+     * 通过引导页面或者登录成功后进入
      * 进入主页前需要检查是否需要绑定手机号、用户标签是否设置
      * @param activity
      */
     private void checkOrGoHomePage(final Activity activity) {
-        //是否展示协议
-//        tryShowAgreement(activity, new Action1<String>() {
-//            @Override
-//            public void call(String msg) {
-//                if(msg == null || TextUtils.equals(msg, "404")) {
-                    CommonUtil.getInstance().updateUserInfo(new Action1<UserEntity>() {
-                        @Override
-                        public void call(UserEntity userEntity) {
-                            if(userEntity != null && !TextUtils.isEmpty(userEntity.getShareLink("other"))) {
-//                                GlideImageLoader.downLoadImage(userEntity.getShare_link().hashCode(), AppUtil.getQRLink(userEntity.getShare_link(), 800), null);
-//                                //PersionInfoAct.java与AskGetMoneyAct.java中分享二维码时的图片预下载
-                                Glide.with(activity)
-                                        .load(AppUtil.getQRLink(userEntity.getShareLink("other"), 800))
-                                        .apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.RESOURCE))
-                                        .preload(800, 800);
-                            }
+        CommonUtil.getInstance().updateUserInfo(new Action1<UserEntity>() {
+            @Override
+            public void call(UserEntity userEntity) {
+                if(userEntity != null && !TextUtils.isEmpty(userEntity.getShare_link())) {
+                    //PersionInfoAct.java与AskGetMoneyAct.java中分享二维码时的图片预下载
+                    GlideImageLoader.downloadAndSetImage(null, AppUtil.getQRLink(userEntity.getShareLink(DialogShare.FACE_TO_FACE), 800), 800);
+                }
 
-                            if(userEntity != null && userEntity.canBindMobile()
-                                    && !BuildConfig.DEBUG
-                                    ){//老用户没有绑定手机号
-                                Jump2View.getInstance().goBindPhone(activity, 1);
-                                return;
-                            }
-                            checkLabel(activity, new Action1<BaseMessage>() {
-                                @Override
-                                public void call(BaseMessage baseMessage) {
-                                    goHomePageView(activity, null);
-                                    activity.finish();
-                                }
-                            });
-                        }
-                    });
-//                }else {
-//                    G.showToast(msg);
-//                    SheepApp.getInstance().quit();
-//                }
-//            }
-//        });
+                if(userEntity != null && userEntity.canBindMobile()
+//                        && !BuildConfig.DEBUG
+                        ){//老用户没有绑定手机号
+                    Jump2View.getInstance().goBindPhone(activity, 1);
+                    return;
+                }
+                checkLabel(activity, new Action1<BaseMessage>() {
+                    @Override
+                    public void call(BaseMessage baseMessage) {
+                        goHomePageView(activity, null);
+                        activity.finish();
+                    }
+                });
+            }
+        });
     }
 
 

+ 2 - 15
app/src/main/java/com/sheep/gamegroup/util/ViewUtil.java

@@ -95,17 +95,7 @@ public class ViewUtil {
         final ImageView iv_qr = view.findViewById(R.id.iv_qr);
         View save_pic = view.findViewById(R.id.save_pic);
         ImageView iv_qr_small = view.findViewById(R.id.iv_qr_small);
-        Glide.with(activity)
-                .load(AppUtil.getQRLink(link, 800))
-                .apply(new RequestOptions().override(800, 800))
-                .into(iv_qr);
-//        GlideImageLoader.downLoadImage(link.hashCode() + "", AppUtil.getQRLink(link, 800), new Action1<File>() {
-//            @Override
-//            public void call(File file) {
-//                if(file != null)
-//                    Glide.with(SheepApp.getInstance()).load(file).into(iv_qr);
-//            }
-//        });
+        GlideImageLoader.downloadAndSetImage(iv_qr, AppUtil.getQRLink(link, 800), 800);
         Glide.with(activity)
                 .load(R.drawable.icon)
                 .apply(new RequestOptions().override(G.WIDTH / 8))
@@ -166,10 +156,7 @@ public class ViewUtil {
         if (!TextUtils.isEmpty(link))
             tv_link.setText(link);
 
-        Glide.with(activity)
-                .load(AppUtil.getQRLink(link, 800))
-                .apply(new RequestOptions().override(800, 800))
-                .into(iv_qr);
+        GlideImageLoader.downloadAndSetImage(iv_qr, AppUtil.getQRLink(link, 800), 800);
         dialog_close.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {

+ 1 - 3
app/src/main/java/com/sheep/gamegroup/view/activity/PersonalInfoAct.java

@@ -200,9 +200,7 @@ public class PersonalInfoAct extends AbsChooseImageActivity {
         personal_info_name.setText(userEntity.getNickname());
         personal_info_code.setText(userEntity.getInvitation_code());
 
-        Glide.with(PersonalInfoAct.this)
-                .load(AppUtil.getQRLink(userEntity.getShareLink("other"), 90))
-                .into(personal_info_qr);
+        GlideImageLoader.downloadAndSetImage(personal_info_qr, AppUtil.getQRLink(userEntity.getShareLink(DialogShare.FACE_TO_FACE), 90), 90);
         GlideImageLoader.setAvatar(personal_info_avatar, userEntity.getAvatar());
         //账号与安全