Prechádzať zdrojové kódy

完成切换同时切换裁剪大小

詹子聪 6 rokov pred
rodič
commit
20e6625d59

+ 64 - 3
app/src/main/java/com/miekir/ocr/ui/CameraActivity.java

@@ -36,12 +36,14 @@ import android.widget.Toast;
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 
+import com.miekir.common.utils.ViewTool;
 import com.miekir.ocr.R;
 import com.miekir.ocr.base.BaseCameraActivity;
 import com.miekir.ocr.tool.CameraPreviewTool;
 import com.miekir.ocr.view.AutoFitTextureView;
 import com.miekir.ocr.view.CropView;
 import com.miekir.ocr.view.GlideV4ImageEngine;
+import com.miekir.ocr.view.IndicatorText;
 import com.zhihu.matisse.Matisse;
 import com.zhihu.matisse.MimeType;
 
@@ -93,6 +95,13 @@ public class CameraActivity extends BaseCameraActivity implements View.OnClickLi
     private static final int MAX_PREVIEW_HEIGHT = 1080;
     private int mSensorOrientation;
 
+    /**识别不同的内容*/
+    private IndicatorText it_email;
+    private IndicatorText it_address;
+    private IndicatorText it_name;
+    private IndicatorText it_all;
+    private CropView pcv_scan;
+    private int mLastType = CropView.SCAN_TYPE_EMAIL;
 
     private TextureView.SurfaceTextureListener textureListener = new TextureView.SurfaceTextureListener() {
         @Override
@@ -148,15 +157,23 @@ public class CameraActivity extends BaseCameraActivity implements View.OnClickLi
         return R.layout.activity_camera;
     }
 
+
     @SuppressLint("CheckResult")
     @Override
     public void initViews(Bundle savedInstanceState) {
         textureView = findViewById(R.id.textureView);
         textureView.setSurfaceTextureListener(textureListener);
 
-        findViewById(R.id.fl_take).setOnClickListener(this);
-        findViewById(R.id.fl_album).setOnClickListener(this);
-        CropView pcv_scan = findViewById(R.id.pcv_scan);
+        it_email = findViewById(R.id.it_email);
+        it_address = findViewById(R.id.it_address);
+        it_name = findViewById(R.id.it_name);
+        it_all = findViewById(R.id.it_all);
+
+        ViewTool.setOnClickListener(this, new int[]{R.id.fl_take, R.id.fl_album,
+                it_email.getId(), it_address.getId(), it_name.getId(), it_all.getId()}, this);
+
+        // 裁剪View
+        pcv_scan = findViewById(R.id.pcv_scan);
         pcv_scan.setLocationListener(this);
     }
 
@@ -494,12 +511,56 @@ public class CameraActivity extends BaseCameraActivity implements View.OnClickLi
                         //.showPreview(false) // Default is `true`
                         .forResult(REQUEST_CODE_CHOOSE);
                 break;
+
+            case R.id.it_email:
+                it_email.onIndicatorClicked();
+                it_address.onIndicatorRelease();
+                it_name.onIndicatorRelease();
+                it_all.onIndicatorRelease();
+                if (mLastType != CropView.SCAN_TYPE_EMAIL) {
+                    mLastType = CropView.SCAN_TYPE_EMAIL;
+                    pcv_scan.setScanArea(mLastType);
+                }
+                break;
+            case R.id.it_address:
+                it_email.onIndicatorRelease();
+                it_address.onIndicatorClicked();
+                it_name.onIndicatorRelease();
+                it_all.onIndicatorRelease();
+                if (mLastType != CropView.SCAN_TYPE_ADDRESS_NAME) {
+                    mLastType = CropView.SCAN_TYPE_ADDRESS_NAME;
+                    pcv_scan.setScanArea(mLastType);
+                }
+                break;
+            case R.id.it_name:
+                it_email.onIndicatorRelease();
+                it_address.onIndicatorRelease();
+                it_name.onIndicatorClicked();
+                it_all.onIndicatorRelease();
+                it_all.onIndicatorRelease();
+                if (mLastType != CropView.SCAN_TYPE_ADDRESS_NAME) {
+                    mLastType = CropView.SCAN_TYPE_ADDRESS_NAME;
+                    pcv_scan.setScanArea(mLastType);
+                }
+                break;
+            case R.id.it_all:
+
+                it_email.onIndicatorRelease();
+                it_address.onIndicatorRelease();
+                it_name.onIndicatorRelease();
+                it_all.onIndicatorClicked();
+                if (mLastType != CropView.SCAN_TYPE_ALL) {
+                    mLastType = CropView.SCAN_TYPE_ALL;
+                    pcv_scan.setScanArea(mLastType);
+                }
+                break;
             default:
                 break;
         }
     }
 
     private List<Uri> mSelected;
+
     @Override
     protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
         super.onActivityResult(requestCode, resultCode, data);

+ 6 - 1
app/src/main/java/com/miekir/ocr/view/CropView.java

@@ -77,7 +77,7 @@ public class CropView extends View {
         super(context, attrs, defStyleAttr);
 
         // 获取到宽高之后再初始化
-        post(() -> setScanArea(SCAN_TYPE_ALL));
+        post(() -> setScanArea(SCAN_TYPE_EMAIL));
     }
 
     /**
@@ -144,6 +144,11 @@ public class CropView extends View {
         mPaintCorner = new Paint();
         mPaintCorner.setColor(getContext().getResources().getColor(R.color.colorPrimary));
         mPaintCorner.setStrokeWidth(CORNER_STROKE);
+
+        invalidate();
+        // invalidate()重绘树,主线程使用,不会触发onMeasure()方法(控制大小用)。
+        // 如果是View就重绘View,如果是ViewGroup就全部重绘。postInvalidate()同,在子线程使用
+        // requestLayout()和invalidate()相反,只调用measure()和layout()过程,不会调用draw()
     }
 
     /*生成bitmap*/

+ 66 - 0
app/src/main/java/com/miekir/ocr/view/IndicatorText.java

@@ -0,0 +1,66 @@
+package com.miekir.ocr.view;
+
+import android.content.Context;
+import android.content.res.TypedArray;
+import android.util.AttributeSet;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+
+import androidx.annotation.Nullable;
+
+import com.miekir.ocr.R;
+
+/**
+ *
+ * @author 詹子聪
+ * @date 2020/7/27 1:59
+ * Description: 指示器和文字自定义View
+ */
+public class IndicatorText extends LinearLayout {
+    private View view_dot;
+    private TextView tv_title;
+
+    public IndicatorText(Context context) {
+        this(context, null);
+    }
+
+    public IndicatorText(Context context, @Nullable AttributeSet attrs) {
+        this(context, attrs, 0);
+    }
+
+    public IndicatorText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
+        super(context, attrs, defStyleAttr);
+
+        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.IndicatorText);
+        String title = typedArray.getString(R.styleable.IndicatorText_text);
+        boolean isFocused = typedArray.getBoolean(R.styleable.IndicatorText_focused, false);
+        //int id = typedArray.getInt(R.styleable, -1);
+        typedArray.recycle();
+
+        // 把布局渲染进View
+        LayoutInflater.from(context).inflate(R.layout.view_indicator, this);
+
+        view_dot = findViewById(R.id.view_dot);
+        tv_title = findViewById(R.id.tv_title);
+        tv_title.setText(title);
+
+        if (isFocused) {
+            onIndicatorClicked();
+        } else {
+            onIndicatorRelease();
+        }
+    }
+
+    public void onIndicatorClicked() {
+        view_dot.setBackgroundResource(R.drawable.shape_circle_focus);
+        tv_title.setTextColor(getResources().getColor(R.color.colorPrimary));
+    }
+
+    public void onIndicatorRelease() {
+        view_dot.setBackgroundResource(R.drawable.shape_circle_normal);
+        tv_title.setTextColor(getResources().getColor(R.color.white));
+    }
+}
+

app/src/main/res/drawable/shape_circle.xml → app/src/main/res/drawable/shape_circle_focus.xml


+ 5 - 0
app/src/main/res/drawable/shape_circle_normal.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<shape xmlns:android="http://schemas.android.com/apk/res/android"
+    android:shape="oval">
+    <solid android:color="@color/white" />
+</shape>

+ 14 - 80
app/src/main/res/layout/activity_camera.xml

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/activity_main"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
@@ -75,112 +76,45 @@
                         android:layout_height="wrap_content"
                         android:layout_weight="1" />
 
-                    <LinearLayout
+                    <com.miekir.ocr.view.IndicatorText
+                        android:id="@+id/it_email"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
-                        android:gravity="center_horizontal"
-                        android:orientation="vertical"
-                        android:paddingTop="@dimen/margin_s"
-                        android:paddingBottom="@dimen/margin_default">
-
-                        <View
-                            android:layout_width="@dimen/width_focus"
-                            android:layout_height="@dimen/width_focus"
-                            android:layout_marginTop="@dimen/margin_s"
-                            android:layout_marginBottom="@dimen/margin_s"
-                            android:background="@drawable/shape_circle" />
-
-                        <TextView
-                            android:layout_width="wrap_content"
-                            android:layout_height="wrap_content"
-                            android:text="邮政编码"
-                            android:textColor="@color/white"
-                            android:textSize="@dimen/size_text_title" />
-                    </LinearLayout>
+                        app:text="邮政编码"
+                        app:focused="true"/>
 
                     <Space
                         android:layout_width="0dp"
                         android:layout_height="wrap_content"
                         android:layout_weight="1" />
 
-                    <LinearLayout
+                    <com.miekir.ocr.view.IndicatorText
+                        android:id="@+id/it_address"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
-                        android:gravity="center_horizontal"
-                        android:orientation="vertical"
-                        android:paddingTop="@dimen/margin_s"
-                        android:paddingBottom="@dimen/margin_default">
-
-                        <View
-                            android:layout_width="@dimen/width_focus"
-                            android:layout_height="@dimen/width_focus"
-                            android:layout_marginTop="@dimen/margin_s"
-                            android:layout_marginBottom="@dimen/margin_s"
-                            android:background="@drawable/shape_circle" />
-
-                        <TextView
-                            android:layout_width="wrap_content"
-                            android:layout_height="wrap_content"
-                            android:text="住址"
-                            android:textColor="@color/white"
-                            android:textSize="@dimen/size_text_title" />
-                    </LinearLayout>
+                        app:text="住址"/>
 
                     <Space
                         android:layout_width="0dp"
                         android:layout_height="wrap_content"
                         android:layout_weight="1" />
 
-                    <LinearLayout
+                    <com.miekir.ocr.view.IndicatorText
+                        android:id="@+id/it_name"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
-                        android:gravity="center_horizontal"
-                        android:orientation="vertical"
-                        android:paddingTop="@dimen/margin_s"
-                        android:paddingBottom="@dimen/margin_default">
-
-                        <View
-                            android:layout_width="@dimen/width_focus"
-                            android:layout_height="@dimen/width_focus"
-                            android:layout_marginTop="@dimen/margin_s"
-                            android:layout_marginBottom="@dimen/margin_s"
-                            android:background="@drawable/shape_circle" />
-
-                        <TextView
-                            android:layout_width="wrap_content"
-                            android:layout_height="wrap_content"
-                            android:text="姓名"
-                            android:textColor="@color/white"
-                            android:textSize="@dimen/size_text_title" />
-                    </LinearLayout>
+                        app:text="姓名"/>
 
                     <Space
                         android:layout_width="0dp"
                         android:layout_height="wrap_content"
                         android:layout_weight="1" />
 
-                    <LinearLayout
+                    <com.miekir.ocr.view.IndicatorText
+                        android:id="@+id/it_all"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
-                        android:gravity="center_horizontal"
-                        android:orientation="vertical"
-                        android:paddingTop="@dimen/margin_s"
-                        android:paddingBottom="@dimen/margin_default">
-
-                        <View
-                            android:layout_width="@dimen/width_focus"
-                            android:layout_height="@dimen/width_focus"
-                            android:layout_marginTop="@dimen/margin_s"
-                            android:layout_marginBottom="@dimen/margin_s"
-                            android:background="@drawable/shape_circle" />
-
-                        <TextView
-                            android:layout_width="wrap_content"
-                            android:layout_height="wrap_content"
-                            android:text="整单识别"
-                            android:textColor="@color/white"
-                            android:textSize="@dimen/size_text_title" />
-                    </LinearLayout>
+                        app:text="整单识别"/>
 
                     <Space
                         android:layout_width="0dp"

+ 24 - 0
app/src/main/res/layout/view_indicator.xml

@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="wrap_content"
+    android:layout_height="wrap_content"
+    android:gravity="center_horizontal"
+    android:orientation="vertical"
+    android:paddingTop="@dimen/margin_s"
+    android:paddingBottom="@dimen/margin_default">
+
+    <View
+        android:id="@+id/view_dot"
+        android:layout_width="@dimen/width_focus"
+        android:layout_height="@dimen/width_focus"
+        android:layout_marginTop="@dimen/margin_s"
+        android:layout_marginBottom="@dimen/margin_s"
+        android:background="@drawable/shape_circle_normal" />
+
+    <TextView
+        android:id="@+id/tv_title"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:textColor="@color/white"
+        android:textSize="@dimen/size_text_title" />
+</LinearLayout>

+ 5 - 0
app/src/main/res/values/attrs.xml

@@ -1,5 +1,10 @@
 <resources>
 
+    <declare-styleable name="IndicatorText">
+        <attr name="text" format="string"/>
+        <attr name="focused" format="boolean"/>
+    </declare-styleable>
+
     <declare-styleable name="CropImageView">
         <attr name="cropGuidelines">
             <enum name="off" value="0"/>