Skip to content

Commit 179bb3a

Browse files
committed
优化手势操作
1 parent 036e9fd commit 179bb3a

File tree

8 files changed

+93
-78
lines changed

8 files changed

+93
-78
lines changed

app/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ android {
1717
applicationId "com.android.NBZxing"
1818
minSdkVersion 19
1919
targetSdkVersion 28
20-
versionCode 6
21-
versionName "1.5"
20+
versionCode 7
21+
versionName "1.6"
2222
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
2323
signingConfig signingConfigs.release
2424
compileOptions {

module_camera/src/main/api14/com/google/android/cameraview/Camera1.java

+1
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ void adjustCameraParameters() {
358358
SortedSet<Size> sizes = mPreviewSizes.sizes(mAspectRatio);
359359
if (sizes == null) { // Not supported
360360
mAspectRatio = chooseAspectRatio();
361+
mPreview.updateAspectRatio(mAspectRatio);
361362
sizes = mPreviewSizes.sizes(mAspectRatio);
362363
}
363364
if (mPictureSizes.sizes(mAspectRatio) == null)

module_camera/src/main/api21/com/google/android/cameraview/Camera2.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class Camera2 extends CameraViewImpl {
7373

7474
@Override
7575
public void onOpened(@NonNull CameraDevice camera) {
76+
Log.e("currentThread-Open", Thread.currentThread().getName());
7677
mCamera = camera;
7778
mCallback.onCameraOpened();
7879
startCaptureSession();
@@ -204,12 +205,7 @@ public void onImageAvailable(ImageReader reader) {
204205
Camera2(Callback callback, PreviewImpl preview, Context context) {
205206
super(callback, preview);
206207
mCameraManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
207-
mPreview.setCallback(new PreviewImpl.Callback() {
208-
@Override
209-
public void onSurfaceChanged() {
210-
startCaptureSession();
211-
}
212-
});
208+
mPreview.setCallback(() -> startCaptureSession());
213209
}
214210

215211
@Override
@@ -470,9 +466,6 @@ private void collectCameraInfo() {
470466
}
471467
}
472468

473-
if (!mPreviewSizes.ratios().contains(mAspectRatio))
474-
mAspectRatio = Constants.DEFAULT_ASPECT_RATIO;
475-
476469
mPictureSizes.clear();
477470
collectPictureSizes(mPictureSizes, map);
478471
for (AspectRatio ratio : mPreviewSizes.ratios()) {
@@ -483,7 +476,9 @@ private void collectCameraInfo() {
483476

484477
if (!mPreviewSizes.ratios().contains(mAspectRatio)) {
485478
mAspectRatio = mPreviewSizes.ratios().iterator().next();
479+
mPreview.updateAspectRatio(mPreviewSizes.ratios().iterator().next());
486480
}
481+
487482
}
488483

489484
protected void collectPictureSizes(SizeMap sizes, StreamConfigurationMap map) {

module_camera/src/main/base/com/google/android/cameraview/PreviewImpl.java

+10
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,14 @@ int getWidth() {
8585
int getHeight() {
8686
return mHeight;
8787
}
88+
89+
/***
90+
* 当相机测量出的不支持设定的, 则提供一个支持的参数
91+
* @param aspectRatio
92+
*/
93+
public void updateAspectRatio(AspectRatio aspectRatio) {
94+
if (getView().getParent() instanceof CameraView) {
95+
((CameraView) getView().getParent()).setAspectRatio(aspectRatio);
96+
}
97+
}
8898
}

module_camera/src/main/java/com/ailiwean/core/OnGestureListener.java

+11-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Context;
44
import android.graphics.PointF;
5+
import android.view.GestureDetector;
56
import android.view.MotionEvent;
67
import android.view.View;
78

@@ -13,14 +14,14 @@
1314
*/
1415
public abstract class OnGestureListener implements View.OnTouchListener {
1516

16-
private int count = 0;//点击次数
17-
private long firstClick = 0;//第一次点击时间
18-
private long secondClick = 0;//第二次点击时间
1917
/**
2018
* 两次点击时间间隔,单位毫秒
2119
*/
2220
private final int totalTime = 500;
2321

22+
private long lastClickTime = 0L;
23+
24+
2425
Context mContext;
2526

2627
//记录第一根手指xy
@@ -54,6 +55,7 @@ public OnGestureListener(Context context) {
5455
*/
5556
@Override
5657
public boolean onTouch(View v, MotionEvent event) {
58+
5759
if (event.getActionMasked() == MotionEvent.ACTION_UP) {
5860
distance = 0;
5961
total = 0;
@@ -88,22 +90,13 @@ public boolean onTouch(View v, MotionEvent event) {
8890
* 监听双击
8991
*/
9092
if (MotionEvent.ACTION_DOWN == event.getAction()) {
91-
count++;
92-
if (1 == count) {
93-
firstClick = System.currentTimeMillis();
94-
} else if (2 == count) {
95-
secondClick = System.currentTimeMillis();
96-
if (secondClick - firstClick < totalTime) {
97-
onDoubleClick();
98-
}
99-
count = 0;
100-
firstClick = 0;
101-
} else {
102-
firstClick = secondClick;
103-
count = 1;
104-
}
105-
secondClick = 0;
93+
if (System.currentTimeMillis() - lastClickTime < totalTime) {
94+
onDoubleClick();
95+
lastClickTime = 0L;
96+
} else
97+
lastClickTime = System.currentTimeMillis();
10698
}
99+
107100
return true;
108101
}
109102

module_camera/src/main/java/com/ailiwean/core/view/ZxingCameraView.kt

+23-7
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import android.graphics.Bitmap
88
import android.graphics.BitmapFactory
99
import android.graphics.PointF
1010
import android.os.Handler
11+
import android.os.HandlerThread
1112
import android.os.Message
1213
import android.util.AttributeSet
14+
import android.util.Log
1315
import android.view.LayoutInflater
1416
import android.view.View
1517
import android.view.ViewGroup
@@ -108,6 +110,7 @@ abstract class ZxingCameraView @JvmOverloads constructor(context: Context, attri
108110
override fun onDestroy() {
109111
super.onDestroy()
110112
ableCollect?.release()
113+
busHandle?.looper?.quit()
111114
}
112115

113116
/***
@@ -183,33 +186,46 @@ abstract class ZxingCameraView @JvmOverloads constructor(context: Context, attri
183186
return ScanTypeConfig.HIGH_FREQUENCY
184187
}
185188

189+
var busHandle: Handler? = null
190+
186191
protected fun parseFile(filePath: String) {
187-
onComPause()
188192
val file = File(filePath)
189193
if (!file.exists()) {
190194
return
191195
}
192-
cameraHandler.post {
196+
197+
if (busHandle == null)
198+
initBusHandle()
199+
200+
busHandle?.post {
193201
val bitmap = BitmapFactory.decodeFile(filePath)
194202
parseBitmap(bitmap)
195203
}
196204
}
197205

206+
private fun initBusHandle() {
207+
val handlerThread = HandlerThread(System.currentTimeMillis().toString())
208+
handlerThread.start()
209+
busHandle = Handler(handlerThread.looper)
210+
}
211+
198212
protected fun parseBitmap(bitmap: Bitmap) {
199-
onComPause()
200-
cameraHandler.post {
213+
214+
if (busHandle == null)
215+
initBusHandle()
216+
217+
busHandle?.post {
201218
val source = BitmapLuminanceSource(bitmap)
202219
val result = CustomMultiFormatReader.getInstance()
203220
.decode(BinaryBitmap(HybridBinarizer(source)))
204221
if (result != null) {
205-
hand.post {
222+
mainHand.post {
206223
resultBackFile(result.text)
207224
scanSucHelper()
208225
}
209226
} else {
210-
hand.post {
227+
mainHand.post {
211228
resultBackFile("")
212-
onComResume()
213229
}
214230
}
215231
}

module_camera/src/main/java/com/google/android/cameraview/BaseCameraView.kt

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ abstract class BaseCameraView @JvmOverloads constructor(context: Context, attrib
3939
var hasFloorView = false
4040

4141
override fun onCameraOpened(cameraView: CameraView) {
42-
hand.post {
42+
mainHand.post {
4343
if (!hasFloorView) {
4444
provideFloorView()?.let {
4545
this@BaseCameraView.addView(it, ViewGroup.LayoutParams(-1, -1))
@@ -51,13 +51,13 @@ abstract class BaseCameraView @JvmOverloads constructor(context: Context, attrib
5151
}
5252

5353
override fun onCameraClosed(cameraView: CameraView) {
54-
hand.post {
54+
mainHand.post {
5555
onCameraClose(cameraView)
5656
}
5757
}
5858

5959
override fun onPictureTaken(cameraView: CameraView, data: ByteArray) {
60-
hand.post {
60+
mainHand.post {
6161
onPictureTake(cameraView, data)
6262
}
6363
}

module_camera/src/main/java/com/google/android/cameraview/CameraView.java

+39-39
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public class CameraView extends FrameLayout {
9494
public @interface Flash {
9595
}
9696

97-
protected Handler hand = new Handler(Looper.getMainLooper());
97+
protected Handler mainHand = new Handler(Looper.getMainLooper());
9898

9999
CameraViewImpl mImpl;
100100

@@ -185,46 +185,46 @@ protected void onDetachedFromWindow() {
185185

186186
RectF r = new RectF();
187187

188-
189188
@Override
190189
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
191-
if (isInEditMode()) {
192-
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
193-
return;
194-
}
190+
// if (isInEditMode()) {
191+
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
192+
// return;
193+
// }
195194
// Handle android:adjustViewBounds
196-
if (mAdjustViewBounds) {
197-
if (!isCameraOpened()) {
198-
mCallbacks.reserveRequestLayoutOnOpen();
199-
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
200-
return;
201-
}
202-
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
203-
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
204-
if (widthMode == MeasureSpec.EXACTLY && heightMode != MeasureSpec.EXACTLY) {
205-
final AspectRatio ratio = getAspectRatio();
206-
assert ratio != null;
207-
int height = (int) (MeasureSpec.getSize(widthMeasureSpec) * ratio.toFloat());
208-
if (heightMode == MeasureSpec.AT_MOST) {
209-
height = Math.min(height, MeasureSpec.getSize(heightMeasureSpec));
210-
}
211-
super.onMeasure(widthMeasureSpec,
212-
MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
213-
} else if (widthMode != MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY) {
214-
final AspectRatio ratio = getAspectRatio();
215-
assert ratio != null;
216-
int width = (int) (MeasureSpec.getSize(heightMeasureSpec) * ratio.toFloat());
217-
if (widthMode == MeasureSpec.AT_MOST) {
218-
width = Math.min(width, MeasureSpec.getSize(widthMeasureSpec));
219-
}
220-
super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
221-
heightMeasureSpec);
222-
} else {
223-
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
224-
}
225-
} else {
226-
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
227-
}
195+
// if (mAdjustViewBounds) {
196+
// if (!isCameraOpened()) {
197+
// mCallbacks.reserveRequestLayoutOnOpen();
198+
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
199+
// return;
200+
// }
201+
// final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
202+
// final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
203+
// if (widthMode == MeasureSpec.EXACTLY && heightMode != MeasureSpec.EXACTLY) {
204+
// final AspectRatio ratio = getAspectRatio();
205+
// assert ratio != null;
206+
// int height = (int) (MeasureSpec.getSize(widthMeasureSpec) * ratio.toFloat());
207+
// if (heightMode == MeasureSpec.AT_MOST) {
208+
// height = Math.min(height, MeasureSpec.getSize(heightMeasureSpec));
209+
// }
210+
// super.onMeasure(widthMeasureSpec,
211+
// MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
212+
// } else if (widthMode != MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY) {
213+
// final AspectRatio ratio = getAspectRatio();
214+
// assert ratio != null;
215+
// int width = (int) (MeasureSpec.getSize(heightMeasureSpec) * ratio.toFloat());
216+
// if (widthMode == MeasureSpec.AT_MOST) {
217+
// width = Math.min(width, MeasureSpec.getSize(widthMeasureSpec));
218+
// }
219+
// super.onMeasure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
220+
// heightMeasureSpec);
221+
// } else {
222+
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
223+
// }
224+
// } else {
225+
// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
226+
// }
227+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
228228
// Measure the TextureView
229229
int width = getMeasuredWidth();
230230
int height = getMeasuredHeight();
@@ -415,7 +415,7 @@ public Set<AspectRatio> getSupportedAspectRatios() {
415415
*/
416416
public void setAspectRatio(@NonNull AspectRatio ratio) {
417417
if (mImpl.setAspectRatio(ratio)) {
418-
hand.post(this::requestLayout);
418+
mainHand.post(this::requestLayout);
419419
}
420420
}
421421

0 commit comments

Comments
 (0)