Explorar el Código

ComPDFKit Tools(Android) - search toolbar

libai hace 1 año
padre
commit
425e8a1345

+ 78 - 0
compdfkit-tools/src/main/java/com/compdfkit/tools/search/CSearchTextInfo.java

@@ -0,0 +1,78 @@
+package com.compdfkit.tools.search;
+
+import android.content.Context;
+import android.text.Spannable;
+import android.text.SpannableStringBuilder;
+import android.text.style.BackgroundColorSpan;
+import android.text.style.CharacterStyle;
+
+import androidx.core.content.ContextCompat;
+
+import com.compdfkit.core.page.CPDFTextPage;
+import com.compdfkit.core.page.CPDFTextRange;
+import com.compdfkit.demo.R;
+import com.compdfkit.tools.R;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * @classname:SearchTextInfo
+ * @date:2018/8/23 下午1:46
+ * @description:
+ */
+public class CSearchTextInfo {
+
+    private Context context;
+    public int page;
+    public String keyword;
+    public SpannableStringBuilder stringBuilder;
+    public boolean isHeader;
+    public int textRangeIndex;
+
+    public CSearchTextInfo(Context context, int page, String keyword, CPDFTextPage textPage, CPDFTextRange textRange, int textRangeIndex, boolean isHeader) {
+        this.context = context;
+        this.page = page;
+        this.keyword = keyword;
+        this.isHeader = isHeader;
+        this.textRangeIndex = textRangeIndex;
+
+        if (isHeader) {
+            return;
+        }
+
+        int targetStart = textRange.location - 20;
+        int length;
+        if (targetStart > 0) {
+            length = textRange.length + 40;
+        } else {
+            length = textRange.length + 40 + targetStart;
+            targetStart = 0;
+        }
+        CPDFTextRange targetTextRange = new CPDFTextRange(targetStart, length);
+        String target = textPage.getText(targetTextRange);
+
+        this.stringBuilder = highlight(target.toLowerCase(), keyword.toLowerCase());
+    }
+
+    /**
+     * 关键字高亮显示
+     *
+     * @param text   需要显示的文字
+     * @param target 需要高亮的关键字
+     * @return spannable 处理完后的结果,记得不要toString(),否则没有效果
+     */
+    private SpannableStringBuilder highlight(String text, String target) {
+        SpannableStringBuilder spannable = new SpannableStringBuilder(text);
+        CharacterStyle span;
+        Pattern p = Pattern.compile(target);
+        Matcher m = p.matcher(text);
+        int backGroundColor = ContextCompat.getColor(context, R.color.search_result_text_highlight);
+        while (m.find()) {
+            /****** 需要重复 ******/
+            span = new BackgroundColorSpan(backGroundColor);
+            spannable.setSpan(span, m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
+        }
+        return spannable;
+    }
+}

+ 84 - 0
compdfkit-tools/src/main/java/com/compdfkit/tools/search/CSearchToolbar.java

@@ -0,0 +1,84 @@
+package com.compdfkit.tools.search;
+
+import android.content.Context;
+import android.text.TextUtils;
+import android.util.AttributeSet;
+import android.view.View;
+import android.widget.Button;
+import android.widget.EditText;
+import android.widget.ImageView;
+import android.widget.RelativeLayout;
+
+import com.compdfkit.tools.R;
+
+import java.util.List;
+
+public class CSearchToolbar extends RelativeLayout implements View.OnClickListener {
+    private Button completeBt;
+    private EditText searchEdit;
+    private ImageView leftImage;
+    private ImageView rightImage;
+    private ImageView listImage;
+
+    private Context context;
+
+    OnSearchListener onSearchListener = null;
+    List<CSearchTextInfo> searchTextInfos = null;
+
+    public CSearchToolbar(Context context) {
+        super(context);
+        init(context);
+    }
+
+    public CSearchToolbar(Context context, AttributeSet attrs) {
+        super(context, attrs);
+        init(context);
+    }
+
+    public CSearchToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+        init(context);
+    }
+
+    public CSearchToolbar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
+        super(context, attrs, defStyleAttr, defStyleRes);
+        init(context);
+    }
+
+    private void init(Context context) {
+        this.context = context;
+        inflate(context, R.layout.search_toolbar, this);
+        completeBt = findViewById(R.id.complete);
+        searchEdit = findViewById(R.id.search_word);
+        leftImage = findViewById(R.id.left);
+        rightImage = findViewById(R.id.right);
+        listImage = findViewById(R.id.list);
+        completeBt.setOnClickListener(this);
+        leftImage.setOnClickListener(this);
+        rightImage.setOnClickListener(this);
+    }
+
+    @Override
+    public void onClick(View view) {
+        int id = view.getId();
+        if (id == R.id.complete) {
+            if (null != onSearchListener && (!TextUtils.isEmpty(searchEdit.getText().toString()))) {
+                searchTextInfos = onSearchListener.onSearch(searchEdit.getText().toString());
+            }
+        } else if (id == R.id.left) {
+
+        } else if (id == R.id.right) {
+
+        } else if (id == R.id.list) {
+
+        }
+    }
+
+    public void setSearchListener(OnSearchListener onSearchListener) {
+        this.onSearchListener = onSearchListener;
+    }
+
+    public interface OnSearchListener {
+        List<CSearchTextInfo> onSearch(String word);
+    }
+}

+ 26 - 0
compdfkit-tools/src/main/res/layout/search_toolbar.xml

@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+
+    <Button
+        android:id="@+id/complete"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"/>
+    <EditText
+        android:id="@+id/search_word"
+        android:layout_width="@dimen/qb_px_60"
+        android:layout_height="wrap_content"/>
+    <ImageView
+        android:id="@+id/left"
+        android:layout_width="@dimen/qb_px_30"
+        android:layout_height="@dimen/qb_px_30"/>
+    <ImageView
+        android:id="@+id/right"
+        android:layout_width="@dimen/qb_px_30"
+        android:layout_height="@dimen/qb_px_30"/>
+    <ImageView
+        android:id="@+id/list"
+        android:layout_width="@dimen/qb_px_30"
+        android:layout_height="@dimen/qb_px_30"/>
+</LinearLayout>

+ 2 - 0
compdfkit-tools/src/main/res/values/colors.xml

@@ -3,4 +3,6 @@
     <color name="upgrade_diff_line">#14000000</color>
     <color name="reader_tools_button_normal">#F5F5F5</color>
     <color name="reader_tools_button_clicked">#1E0071EE</color>
+    <color name="search_result_text_color">#333333</color>
+    <color name="search_result_text_highlight">#FFAB00</color>
 </resources>