Parcourir la source

新手引导中的图片添加居中显示与点击显示大图

zengjiebin il y a 7 ans
Parent
commit
d0ae218ea3

+ 12 - 5
app/src/main/java/com/sheep/gamegroup/util/ResImageGetter.java

@@ -20,14 +20,15 @@ import rx.functions.Action1;
 /**
  * Created by realicing on 2018/11/7.
  * realicing@sina.com
+ * 图片显示类
  */
-public class ResImageGetter implements Html.ImageGetter {
+public class DetailImageGetter implements Html.ImageGetter {
 
     private int textSize;
     private int maxWidth;
     private TextView textView;
     private String content;
-    public ResImageGetter(TextView textView, String content, int maxWidth) {
+    public DetailImageGetter(TextView textView, String content, int maxWidth) {
         this.textView = textView;
         this.content = content;
         this.maxWidth = maxWidth;
@@ -47,13 +48,19 @@ public class ResImageGetter implements Html.ImageGetter {
             if(path != null){
                 Drawable drawable = Drawable.createFromPath(path);
                 if(drawable != null){
-                    int imgWidth = G.getRealPix(drawable.getMinimumWidth());
-                    int imgHeight = G.getRealPix(drawable.getMinimumHeight());
+                    int minimumWidth = drawable.getMinimumWidth();
+                    int minimumHeight = drawable.getMinimumHeight();
+                    int imgWidth = G.getRealPix(minimumWidth);
+                    int imgHeight = G.getRealPix(minimumHeight);
+                    int imgLeft = 0;
                     if(imgWidth > maxWidth && maxWidth > 0){
                         imgHeight = (int) (maxWidth * imgHeight * 1.0f / imgWidth);
                         imgWidth = maxWidth;
+                    } else {
+                        imgLeft = (maxWidth - imgWidth) / 2;
+                        imgWidth += imgLeft;
                     }
-                    drawable.setBounds(0, 0, imgWidth, imgHeight);
+                    drawable.setBounds(imgLeft, 0, imgWidth, imgHeight);
                     return drawable;
                 }
             }

+ 59 - 0
app/src/main/java/com/sheep/gamegroup/util/DetailTagHandler.java

@@ -0,0 +1,59 @@
+package com.sheep.gamegroup.util;
+
+import android.support.annotation.NonNull;
+import android.text.Editable;
+import android.text.Html;
+import android.text.Spanned;
+import android.text.style.ClickableSpan;
+import android.text.style.ImageSpan;
+import android.view.View;
+
+import org.xml.sax.XMLReader;
+
+import java.util.ArrayList;
+import java.util.Locale;
+
+/**
+ * Created by realicing on 2018/11/20.
+ * realicing@sina.com
+ * 图片处理类
+ */
+public class DetailTagHandler implements Html.TagHandler {
+    private ArrayList<String> strings;
+
+    public DetailTagHandler() {
+        strings = new ArrayList<>();
+    }
+
+    @Override
+    public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
+        // 处理标签<img>
+        if ("img".equals(tag.toLowerCase(Locale.getDefault()))) {
+            // 获取长度
+            int len = output.length();
+            // 获取图片地址
+            ImageSpan[] images = output.getSpans(len - 1, len, ImageSpan.class);
+            String imgURL = images[0].getSource();
+            // 记录所有图片地址
+            strings.add(imgURL);
+            // 记录是第几张图片
+            int position = strings.size() - 1;
+            // 使图片可点击并监听点击事件
+            output.setSpan(new ClickableImage(position), len - 1, len,
+                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
+        }
+    }
+
+    private class ClickableImage extends ClickableSpan {
+        private int position;
+
+        private ClickableImage(int position) {
+            this.position = position;
+        }
+
+        @Override
+        public void onClick(@NonNull View widget) {
+            Jump2View.getInstance().showImgList(ActivityManager.getInstance().currentActivity(), position, strings);
+        }
+    }
+}

+ 7 - 1
app/src/main/java/com/sheep/gamegroup/util/ViewUtil.java

@@ -25,6 +25,7 @@ import android.support.v7.widget.GridLayoutManager;
 import android.support.v7.widget.RecyclerView;
 import android.text.Html;
 import android.text.TextUtils;
+import android.text.method.LinkMovementMethod;
 import android.text.method.ScrollingMovementMethod;
 import android.util.DisplayMetrics;
 import android.util.TypedValue;
@@ -969,7 +970,12 @@ public class ViewUtil {
                 textView.setText("");
                 return;
             }
-            textView.setText(Html.fromHtml(content, new ResImageGetter(textView, content, maxWidth), null));
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
+                textView.setText(Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY, new DetailImageGetter(textView, content, maxWidth), new DetailTagHandler()));
+            } else {
+                textView.setText(Html.fromHtml(content, new DetailImageGetter(textView, content, maxWidth), new DetailTagHandler()));
+            }
+            textView.setMovementMethod(LinkMovementMethod.getInstance());
         }
     }
 

+ 2 - 2
app/src/main/java/com/sheep/gamegroup/view/activity/ActTestExpression.java

@@ -11,7 +11,7 @@ import com.lqr.emoji.EmotionKeyboard;
 import com.lqr.emoji.EmotionLayout;
 import com.lqr.emoji.IEmotionSelectedListener;
 import com.sheep.gamegroup.absBase.BaseActivity;
-import com.sheep.gamegroup.util.ResImageGetter;
+import com.sheep.gamegroup.util.DetailImageGetter;
 import com.sheep.gamegroup.util.ViewUtil;
 import com.sheep.jiuyan.samllsheep.R;
 import com.sheep.jiuyan.samllsheep.utils.G;
@@ -84,7 +84,7 @@ public class ActTestExpression extends BaseActivity {
     //提交数据
     private void commitComment(String content) {
         String msg = String.format(Locale.CHINA, "%s<img src=\"%s\" />", content, "exp_666");
-        test_content.setText(Html.fromHtml(msg, new ResImageGetter(test_content, msg, G.WIDTH), null));
+        test_content.setText(Html.fromHtml(msg, new DetailImageGetter(test_content, msg, G.WIDTH), null));
     }
 
     //点击x来清空输入内容