2019-06-26 18:10:57 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-11-09 09:21:59 -04:00
|
|
|
package androidx.camera.view;
|
2019-10-18 17:36:00 -03:00
|
|
|
|
2019-06-26 18:10:57 -04:00
|
|
|
import android.Manifest.permission;
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
import android.content.Context;
|
2019-10-18 17:36:00 -03:00
|
|
|
import android.content.res.TypedArray;
|
2019-06-26 18:10:57 -04:00
|
|
|
import android.hardware.display.DisplayManager;
|
|
|
|
import android.hardware.display.DisplayManager.DisplayListener;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
|
|
|
import android.os.Parcelable;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.view.Display;
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
import android.view.ScaleGestureDetector;
|
|
|
|
import android.view.Surface;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewConfiguration;
|
|
|
|
import android.view.ViewGroup;
|
2020-03-25 09:30:13 -03:00
|
|
|
import android.widget.FrameLayout;
|
2019-06-26 18:10:57 -04:00
|
|
|
|
2019-10-18 17:36:00 -03:00
|
|
|
import androidx.annotation.NonNull;
|
2019-06-26 18:10:57 -04:00
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
import androidx.annotation.RequiresApi;
|
|
|
|
import androidx.annotation.RequiresPermission;
|
|
|
|
import androidx.annotation.RestrictTo;
|
|
|
|
import androidx.annotation.RestrictTo.Scope;
|
2020-03-25 09:30:13 -03:00
|
|
|
import androidx.camera.core.Camera;
|
|
|
|
import androidx.camera.core.CameraSelector;
|
2019-10-18 17:36:00 -03:00
|
|
|
import androidx.camera.core.FocusMeteringAction;
|
2020-03-25 09:30:13 -03:00
|
|
|
import androidx.camera.core.FocusMeteringResult;
|
2019-10-18 17:36:00 -03:00
|
|
|
import androidx.camera.core.ImageCapture;
|
2020-03-25 09:30:13 -03:00
|
|
|
import androidx.camera.core.ImageCapture.OnImageCapturedCallback;
|
2020-11-09 09:21:59 -04:00
|
|
|
import androidx.camera.core.ImageCapture.OnImageSavedCallback;
|
2020-03-25 09:30:13 -03:00
|
|
|
import androidx.camera.core.ImageProxy;
|
2020-11-09 09:21:59 -04:00
|
|
|
import androidx.camera.core.Logger;
|
2019-10-18 17:36:00 -03:00
|
|
|
import androidx.camera.core.MeteringPoint;
|
2020-11-09 09:21:59 -04:00
|
|
|
import androidx.camera.core.MeteringPointFactory;
|
|
|
|
import androidx.camera.core.VideoCapture;
|
|
|
|
import androidx.camera.core.VideoCapture.OnVideoSavedCallback;
|
2020-03-25 09:30:13 -03:00
|
|
|
import androidx.camera.core.impl.LensFacingConverter;
|
|
|
|
import androidx.camera.core.impl.utils.executor.CameraXExecutors;
|
|
|
|
import androidx.camera.core.impl.utils.futures.FutureCallback;
|
|
|
|
import androidx.camera.core.impl.utils.futures.Futures;
|
2019-06-26 18:10:57 -04:00
|
|
|
import androidx.lifecycle.LifecycleOwner;
|
2020-11-09 09:21:59 -04:00
|
|
|
import androidx.lifecycle.LiveData;
|
2019-06-26 18:10:57 -04:00
|
|
|
|
2020-03-25 09:30:13 -03:00
|
|
|
import com.google.common.util.concurrent.ListenableFuture;
|
|
|
|
|
2020-11-09 09:21:59 -04:00
|
|
|
import java.io.File;
|
2019-10-18 17:36:00 -03:00
|
|
|
import java.util.concurrent.Executor;
|
2019-06-26 18:10:57 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A {@link View} that displays a preview of the camera with methods {@link
|
2020-03-25 09:30:13 -03:00
|
|
|
* #takePicture(Executor, OnImageCapturedCallback)},
|
2020-11-09 09:21:59 -04:00
|
|
|
* {@link #takePicture(ImageCapture.OutputFileOptions, Executor, OnImageSavedCallback)},
|
|
|
|
* {@link #startRecording(File , Executor , OnVideoSavedCallback callback)}
|
|
|
|
* and {@link #stopRecording()}.
|
2019-06-26 18:10:57 -04:00
|
|
|
*
|
|
|
|
* <p>Because the Camera is a limited resource and consumes a high amount of power, CameraView must
|
|
|
|
* be opened/closed. CameraView will handle opening/closing automatically through use of a {@link
|
|
|
|
* LifecycleOwner}. Use {@link #bindToLifecycle(LifecycleOwner)} to start the camera.
|
|
|
|
*/
|
|
|
|
@RequiresApi(21)
|
2020-03-25 09:30:13 -03:00
|
|
|
@SuppressLint("RestrictedApi")
|
2020-11-09 09:21:59 -04:00
|
|
|
public final class SignalCameraView extends FrameLayout {
|
|
|
|
static final String TAG = SignalCameraView.class.getSimpleName();
|
2019-06-26 18:10:57 -04:00
|
|
|
|
|
|
|
static final int INDEFINITE_VIDEO_DURATION = -1;
|
|
|
|
static final int INDEFINITE_VIDEO_SIZE = -1;
|
|
|
|
|
|
|
|
private static final String EXTRA_SUPER = "super";
|
2020-03-25 09:30:13 -03:00
|
|
|
private static final String EXTRA_ZOOM_RATIO = "zoom_ratio";
|
2019-06-26 18:10:57 -04:00
|
|
|
private static final String EXTRA_PINCH_TO_ZOOM_ENABLED = "pinch_to_zoom_enabled";
|
|
|
|
private static final String EXTRA_FLASH = "flash";
|
|
|
|
private static final String EXTRA_MAX_VIDEO_DURATION = "max_video_duration";
|
|
|
|
private static final String EXTRA_MAX_VIDEO_SIZE = "max_video_size";
|
|
|
|
private static final String EXTRA_SCALE_TYPE = "scale_type";
|
|
|
|
private static final String EXTRA_CAMERA_DIRECTION = "camera_direction";
|
|
|
|
private static final String EXTRA_CAPTURE_MODE = "captureMode";
|
|
|
|
|
2019-10-18 17:36:00 -03:00
|
|
|
private static final int LENS_FACING_NONE = 0;
|
|
|
|
private static final int LENS_FACING_FRONT = 1;
|
|
|
|
private static final int LENS_FACING_BACK = 2;
|
|
|
|
private static final int FLASH_MODE_AUTO = 1;
|
|
|
|
private static final int FLASH_MODE_ON = 2;
|
|
|
|
private static final int FLASH_MODE_OFF = 4;
|
2019-06-26 18:10:57 -04:00
|
|
|
// For tap-to-focus
|
|
|
|
private long mDownEventTimestamp;
|
|
|
|
// For pinch-to-zoom
|
|
|
|
private PinchToZoomGestureDetector mPinchToZoomGestureDetector;
|
|
|
|
private boolean mIsPinchToZoomEnabled = true;
|
2020-11-09 09:21:59 -04:00
|
|
|
SignalCameraXModule mCameraModule;
|
2019-06-26 18:10:57 -04:00
|
|
|
private final DisplayManager.DisplayListener mDisplayListener =
|
|
|
|
new DisplayListener() {
|
|
|
|
@Override
|
|
|
|
public void onDisplayAdded(int displayId) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDisplayRemoved(int displayId) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDisplayChanged(int displayId) {
|
|
|
|
mCameraModule.invalidateView();
|
|
|
|
}
|
|
|
|
};
|
2020-03-25 09:30:13 -03:00
|
|
|
private PreviewView mPreviewView;
|
2019-06-26 18:10:57 -04:00
|
|
|
// For accessibility event
|
|
|
|
private MotionEvent mUpEvent;
|
|
|
|
|
2020-11-09 09:21:59 -04:00
|
|
|
public SignalCameraView(@NonNull Context context) {
|
2019-06-26 18:10:57 -04:00
|
|
|
this(context, null);
|
|
|
|
}
|
|
|
|
|
2020-11-09 09:21:59 -04:00
|
|
|
public SignalCameraView(@NonNull Context context, @Nullable AttributeSet attrs) {
|
2019-06-26 18:10:57 -04:00
|
|
|
this(context, attrs, 0);
|
|
|
|
}
|
|
|
|
|
2020-11-09 09:21:59 -04:00
|
|
|
public SignalCameraView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
|
2019-06-26 18:10:57 -04:00
|
|
|
super(context, attrs, defStyle);
|
|
|
|
init(context, attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequiresApi(21)
|
2020-11-09 09:21:59 -04:00
|
|
|
public SignalCameraView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr,
|
|
|
|
int defStyleRes) {
|
2019-06-26 18:10:57 -04:00
|
|
|
super(context, attrs, defStyleAttr, defStyleRes);
|
|
|
|
init(context, attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Binds control of the camera used by this view to the given lifecycle.
|
|
|
|
*
|
|
|
|
* <p>This links opening/closing the camera to the given lifecycle. The camera will not operate
|
|
|
|
* unless this method is called with a valid {@link LifecycleOwner} that is not in the {@link
|
|
|
|
* androidx.lifecycle.Lifecycle.State#DESTROYED} state. Call this method only once camera
|
|
|
|
* permissions have been obtained.
|
|
|
|
*
|
|
|
|
* <p>Once the provided lifecycle has transitioned to a {@link
|
|
|
|
* androidx.lifecycle.Lifecycle.State#DESTROYED} state, CameraView must be bound to a new
|
|
|
|
* lifecycle through this method in order to operate the camera.
|
|
|
|
*
|
|
|
|
* @param lifecycleOwner The lifecycle that will control this view's camera
|
|
|
|
* @throws IllegalArgumentException if provided lifecycle is in a {@link
|
|
|
|
* androidx.lifecycle.Lifecycle.State#DESTROYED} state.
|
|
|
|
* @throws IllegalStateException if camera permissions are not granted.
|
|
|
|
*/
|
|
|
|
@RequiresPermission(permission.CAMERA)
|
2020-03-25 09:30:13 -03:00
|
|
|
public void bindToLifecycle(@NonNull LifecycleOwner lifecycleOwner) {
|
2019-06-26 18:10:57 -04:00
|
|
|
mCameraModule.bindToLifecycle(lifecycleOwner);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void init(Context context, @Nullable AttributeSet attrs) {
|
2020-03-25 09:30:13 -03:00
|
|
|
addView(mPreviewView = new PreviewView(getContext()), 0 /* view position */);
|
2020-11-09 09:21:59 -04:00
|
|
|
mCameraModule = new SignalCameraXModule(this);
|
2019-06-26 18:10:57 -04:00
|
|
|
|
2019-10-18 17:36:00 -03:00
|
|
|
if (attrs != null) {
|
2020-11-09 09:21:59 -04:00
|
|
|
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CameraView);
|
2019-10-18 17:36:00 -03:00
|
|
|
setScaleType(
|
2020-11-09 09:21:59 -04:00
|
|
|
PreviewView.ScaleType.fromId(
|
|
|
|
a.getInteger(R.styleable.CameraView_scaleType,
|
2019-10-18 17:36:00 -03:00
|
|
|
getScaleType().getId())));
|
|
|
|
setPinchToZoomEnabled(
|
|
|
|
a.getBoolean(
|
2020-11-09 09:21:59 -04:00
|
|
|
R.styleable.CameraView_pinchToZoomEnabled, isPinchToZoomEnabled()));
|
2019-10-18 17:36:00 -03:00
|
|
|
setCaptureMode(
|
|
|
|
CaptureMode.fromId(
|
2020-11-09 09:21:59 -04:00
|
|
|
a.getInteger(R.styleable.CameraView_captureMode,
|
2019-10-18 17:36:00 -03:00
|
|
|
getCaptureMode().getId())));
|
|
|
|
|
2020-11-09 09:21:59 -04:00
|
|
|
int lensFacing = a.getInt(R.styleable.CameraView_lensFacing, LENS_FACING_BACK);
|
2019-10-18 17:36:00 -03:00
|
|
|
switch (lensFacing) {
|
|
|
|
case LENS_FACING_NONE:
|
|
|
|
setCameraLensFacing(null);
|
|
|
|
break;
|
|
|
|
case LENS_FACING_FRONT:
|
2020-03-25 09:30:13 -03:00
|
|
|
setCameraLensFacing(CameraSelector.LENS_FACING_FRONT);
|
2019-10-18 17:36:00 -03:00
|
|
|
break;
|
|
|
|
case LENS_FACING_BACK:
|
2020-03-25 09:30:13 -03:00
|
|
|
setCameraLensFacing(CameraSelector.LENS_FACING_BACK);
|
2019-10-18 17:36:00 -03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Unhandled event.
|
|
|
|
}
|
|
|
|
|
2020-11-09 09:21:59 -04:00
|
|
|
int flashMode = a.getInt(R.styleable.CameraView_flash, 0);
|
2019-10-18 17:36:00 -03:00
|
|
|
switch (flashMode) {
|
|
|
|
case FLASH_MODE_AUTO:
|
2020-03-25 09:30:13 -03:00
|
|
|
setFlash(ImageCapture.FLASH_MODE_AUTO);
|
2019-10-18 17:36:00 -03:00
|
|
|
break;
|
|
|
|
case FLASH_MODE_ON:
|
2020-03-25 09:30:13 -03:00
|
|
|
setFlash(ImageCapture.FLASH_MODE_ON);
|
2019-10-18 17:36:00 -03:00
|
|
|
break;
|
|
|
|
case FLASH_MODE_OFF:
|
2020-03-25 09:30:13 -03:00
|
|
|
setFlash(ImageCapture.FLASH_MODE_OFF);
|
2019-10-18 17:36:00 -03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Unhandled event.
|
|
|
|
}
|
|
|
|
|
|
|
|
a.recycle();
|
|
|
|
}
|
2019-06-26 18:10:57 -04:00
|
|
|
|
|
|
|
if (getBackground() == null) {
|
|
|
|
setBackgroundColor(0xFF111111);
|
|
|
|
}
|
|
|
|
|
|
|
|
mPinchToZoomGestureDetector = new PinchToZoomGestureDetector(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-25 09:30:13 -03:00
|
|
|
@NonNull
|
2019-06-26 18:10:57 -04:00
|
|
|
protected LayoutParams generateDefaultLayoutParams() {
|
|
|
|
return new LayoutParams(
|
|
|
|
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-25 09:30:13 -03:00
|
|
|
@NonNull
|
2019-06-26 18:10:57 -04:00
|
|
|
protected Parcelable onSaveInstanceState() {
|
|
|
|
// TODO(b/113884082): Decide what belongs here or what should be invalidated on
|
|
|
|
// configuration
|
|
|
|
// change
|
|
|
|
Bundle state = new Bundle();
|
|
|
|
state.putParcelable(EXTRA_SUPER, super.onSaveInstanceState());
|
|
|
|
state.putInt(EXTRA_SCALE_TYPE, getScaleType().getId());
|
2020-03-25 09:30:13 -03:00
|
|
|
state.putFloat(EXTRA_ZOOM_RATIO, getZoomRatio());
|
2019-06-26 18:10:57 -04:00
|
|
|
state.putBoolean(EXTRA_PINCH_TO_ZOOM_ENABLED, isPinchToZoomEnabled());
|
2020-03-25 09:30:13 -03:00
|
|
|
state.putString(EXTRA_FLASH, FlashModeConverter.nameOf(getFlash()));
|
2019-06-26 18:10:57 -04:00
|
|
|
state.putLong(EXTRA_MAX_VIDEO_DURATION, getMaxVideoDuration());
|
|
|
|
state.putLong(EXTRA_MAX_VIDEO_SIZE, getMaxVideoSize());
|
|
|
|
if (getCameraLensFacing() != null) {
|
2020-03-25 09:30:13 -03:00
|
|
|
state.putString(EXTRA_CAMERA_DIRECTION,
|
|
|
|
LensFacingConverter.nameOf(getCameraLensFacing()));
|
2019-06-26 18:10:57 -04:00
|
|
|
}
|
|
|
|
state.putInt(EXTRA_CAPTURE_MODE, getCaptureMode().getId());
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-03-25 09:30:13 -03:00
|
|
|
protected void onRestoreInstanceState(@Nullable Parcelable savedState) {
|
2019-06-26 18:10:57 -04:00
|
|
|
// TODO(b/113884082): Decide what belongs here or what should be invalidated on
|
|
|
|
// configuration
|
|
|
|
// change
|
|
|
|
if (savedState instanceof Bundle) {
|
|
|
|
Bundle state = (Bundle) savedState;
|
|
|
|
super.onRestoreInstanceState(state.getParcelable(EXTRA_SUPER));
|
2020-11-09 09:21:59 -04:00
|
|
|
setScaleType(PreviewView.ScaleType.fromId(state.getInt(EXTRA_SCALE_TYPE)));
|
2020-03-25 09:30:13 -03:00
|
|
|
setZoomRatio(state.getFloat(EXTRA_ZOOM_RATIO));
|
2019-06-26 18:10:57 -04:00
|
|
|
setPinchToZoomEnabled(state.getBoolean(EXTRA_PINCH_TO_ZOOM_ENABLED));
|
2020-03-25 09:30:13 -03:00
|
|
|
setFlash(FlashModeConverter.valueOf(state.getString(EXTRA_FLASH)));
|
2019-06-26 18:10:57 -04:00
|
|
|
setMaxVideoDuration(state.getLong(EXTRA_MAX_VIDEO_DURATION));
|
|
|
|
setMaxVideoSize(state.getLong(EXTRA_MAX_VIDEO_SIZE));
|
|
|
|
String lensFacingString = state.getString(EXTRA_CAMERA_DIRECTION);
|
|
|
|
setCameraLensFacing(
|
|
|
|
TextUtils.isEmpty(lensFacingString)
|
|
|
|
? null
|
2020-03-25 09:30:13 -03:00
|
|
|
: LensFacingConverter.valueOf(lensFacingString));
|
2019-06-26 18:10:57 -04:00
|
|
|
setCaptureMode(CaptureMode.fromId(state.getInt(EXTRA_CAPTURE_MODE)));
|
|
|
|
} else {
|
|
|
|
super.onRestoreInstanceState(savedState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onAttachedToWindow() {
|
|
|
|
super.onAttachedToWindow();
|
|
|
|
DisplayManager dpyMgr =
|
|
|
|
(DisplayManager) getContext().getSystemService(Context.DISPLAY_SERVICE);
|
|
|
|
dpyMgr.registerDisplayListener(mDisplayListener, new Handler(Looper.getMainLooper()));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onDetachedFromWindow() {
|
|
|
|
super.onDetachedFromWindow();
|
|
|
|
DisplayManager dpyMgr =
|
|
|
|
(DisplayManager) getContext().getSystemService(Context.DISPLAY_SERVICE);
|
|
|
|
dpyMgr.unregisterDisplayListener(mDisplayListener);
|
|
|
|
}
|
|
|
|
|
2020-11-09 09:21:59 -04:00
|
|
|
/**
|
|
|
|
* Gets the {@link LiveData} of the underlying {@link PreviewView}'s
|
|
|
|
* {@link PreviewView.StreamState}.
|
|
|
|
*
|
|
|
|
* @return A {@link LiveData} containing the {@link PreviewView.StreamState}. Apps can either
|
|
|
|
* get current value by {@link LiveData#getValue()} or register a observer by
|
|
|
|
* {@link LiveData#observe}.
|
|
|
|
* @see PreviewView#getPreviewStreamState()
|
|
|
|
*/
|
|
|
|
@NonNull
|
|
|
|
public LiveData<PreviewView.StreamState> getPreviewStreamState() {
|
|
|
|
return mPreviewView.getPreviewStreamState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@NonNull
|
2020-03-25 09:30:13 -03:00
|
|
|
PreviewView getPreviewView() {
|
|
|
|
return mPreviewView;
|
|
|
|
}
|
|
|
|
|
2019-06-26 18:10:57 -04:00
|
|
|
// TODO(b/124269166): Rethink how we can handle permissions here.
|
|
|
|
@SuppressLint("MissingPermission")
|
|
|
|
@Override
|
|
|
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
|
|
|
// Since bindToLifecycle will depend on the measured dimension, only call it when measured
|
|
|
|
// dimension is not 0x0
|
|
|
|
if (getMeasuredWidth() > 0 && getMeasuredHeight() > 0) {
|
|
|
|
mCameraModule.bindToLifecycleAfterViewMeasured();
|
|
|
|
}
|
2020-03-25 09:30:13 -03:00
|
|
|
|
|
|
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
2019-06-26 18:10:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(b/124269166): Rethink how we can handle permissions here.
|
|
|
|
@SuppressLint("MissingPermission")
|
|
|
|
@Override
|
|
|
|
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
|
|
|
// In case that the CameraView size is always set as 0x0, we still need to trigger to force
|
|
|
|
// binding to lifecycle
|
|
|
|
mCameraModule.bindToLifecycleAfterViewMeasured();
|
|
|
|
|
|
|
|
mCameraModule.invalidateView();
|
2020-03-25 09:30:13 -03:00
|
|
|
super.onLayout(changed, left, top, right, bottom);
|
2019-06-26 18:10:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return One of {@link Surface#ROTATION_0}, {@link Surface#ROTATION_90}, {@link
|
|
|
|
* Surface#ROTATION_180}, {@link Surface#ROTATION_270}.
|
|
|
|
*/
|
|
|
|
int getDisplaySurfaceRotation() {
|
|
|
|
Display display = getDisplay();
|
|
|
|
|
|
|
|
// Null when the View is detached. If we were in the middle of a background operation,
|
|
|
|
// better to not NPE. When the background operation finishes, it'll realize that the camera
|
|
|
|
// was closed.
|
|
|
|
if (display == null) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return display.getRotation();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the scale type used to scale the preview.
|
|
|
|
*
|
2020-11-09 09:21:59 -04:00
|
|
|
* @return The current {@link PreviewView.ScaleType}.
|
2019-06-26 18:10:57 -04:00
|
|
|
*/
|
2020-03-25 09:30:13 -03:00
|
|
|
@NonNull
|
2020-11-09 09:21:59 -04:00
|
|
|
public PreviewView.ScaleType getScaleType() {
|
|
|
|
return mPreviewView.getScaleType();
|
2019-06-26 18:10:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the view finder scale type.
|
|
|
|
*
|
|
|
|
* <p>This controls how the view finder should be scaled and positioned within the view.
|
|
|
|
*
|
2020-11-09 09:21:59 -04:00
|
|
|
* @param scaleType The desired {@link PreviewView.ScaleType}.
|
2019-06-26 18:10:57 -04:00
|
|
|
*/
|
2020-11-09 09:21:59 -04:00
|
|
|
public void setScaleType(@NonNull PreviewView.ScaleType scaleType) {
|
|
|
|
mPreviewView.setScaleType(scaleType);
|
2019-06-26 18:10:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the scale type used to scale the preview.
|
|
|
|
*
|
|
|
|
* @return The current {@link CaptureMode}.
|
|
|
|
*/
|
2020-03-25 09:30:13 -03:00
|
|
|
@NonNull
|
2019-06-26 18:10:57 -04:00
|
|
|
public CaptureMode getCaptureMode() {
|
|
|
|
return mCameraModule.getCaptureMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the CameraView capture mode
|
|
|
|
*
|
|
|
|
* <p>This controls only image or video capture function is enabled or both are enabled.
|
|
|
|
*
|
|
|
|
* @param captureMode The desired {@link CaptureMode}.
|
|
|
|
*/
|
2020-03-25 09:30:13 -03:00
|
|
|
public void setCaptureMode(@NonNull CaptureMode captureMode) {
|
2019-06-26 18:10:57 -04:00
|
|
|
mCameraModule.setCaptureMode(captureMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the maximum duration of videos, or {@link #INDEFINITE_VIDEO_DURATION} if there is no
|
|
|
|
* timeout.
|
|
|
|
*
|
|
|
|
* @hide Not currently implemented.
|
|
|
|
*/
|
|
|
|
@RestrictTo(Scope.LIBRARY_GROUP)
|
|
|
|
public long getMaxVideoDuration() {
|
|
|
|
return mCameraModule.getMaxVideoDuration();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-09 09:21:59 -04:00
|
|
|
* Sets the maximum video duration before
|
|
|
|
* {@link OnVideoSavedCallback#onVideoSaved(VideoCapture.OutputFileResults)} is called
|
|
|
|
* automatically.
|
|
|
|
* Use {@link #INDEFINITE_VIDEO_DURATION} to disable the timeout.
|
2019-06-26 18:10:57 -04:00
|
|
|
*/
|
|
|
|
private void setMaxVideoDuration(long duration) {
|
|
|
|
mCameraModule.setMaxVideoDuration(duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the maximum size of videos in bytes, or {@link #INDEFINITE_VIDEO_SIZE} if there is no
|
|
|
|
* timeout.
|
|
|
|
*/
|
|
|
|
private long getMaxVideoSize() {
|
|
|
|
return mCameraModule.getMaxVideoSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-11-09 09:21:59 -04:00
|
|
|
* Sets the maximum video size in bytes before
|
|
|
|
* {@link OnVideoSavedCallback#onVideoSaved(VideoCapture.OutputFileResults)}
|
2019-06-26 18:10:57 -04:00
|
|
|
* is called automatically. Use {@link #INDEFINITE_VIDEO_SIZE} to disable the size restriction.
|
|
|
|
*/
|
|
|
|
private void setMaxVideoSize(long size) {
|
|
|
|
mCameraModule.setMaxVideoSize(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-25 09:30:13 -03:00
|
|
|
* Takes a picture, and calls {@link OnImageCapturedCallback#onCaptureSuccess(ImageProxy)}
|
2019-06-26 18:10:57 -04:00
|
|
|
* once when done.
|
|
|
|
*
|
2020-03-25 09:30:13 -03:00
|
|
|
* @param executor The executor in which the callback methods will be run.
|
|
|
|
* @param callback Callback which will receive success or failure callbacks.
|
2019-06-26 18:10:57 -04:00
|
|
|
*/
|
2020-03-25 09:30:13 -03:00
|
|
|
public void takePicture(@NonNull Executor executor, @NonNull OnImageCapturedCallback callback) {
|
|
|
|
mCameraModule.takePicture(executor, callback);
|
2019-06-26 18:10:57 -04:00
|
|
|
}
|
|
|
|
|
2020-11-09 09:21:59 -04:00
|
|
|
/**
|
|
|
|
* Takes a picture and calls
|
|
|
|
* {@link OnImageSavedCallback#onImageSaved(ImageCapture.OutputFileResults)} when done.
|
|
|
|
*
|
|
|
|
* <p> The value of {@link ImageCapture.Metadata#isReversedHorizontal()} in the
|
|
|
|
* {@link ImageCapture.OutputFileOptions} will be overwritten based on camera direction. For
|
|
|
|
* front camera, it will be set to true; for back camera, it will be set to false.
|
|
|
|
*
|
|
|
|
* @param outputFileOptions Options to store the newly captured image.
|
|
|
|
* @param executor The executor in which the callback methods will be run.
|
|
|
|
* @param callback Callback which will receive success or failure.
|
|
|
|
*/
|
|
|
|
public void takePicture(@NonNull ImageCapture.OutputFileOptions outputFileOptions,
|
|
|
|
@NonNull Executor executor,
|
|
|
|
@NonNull OnImageSavedCallback callback) {
|
|
|
|
mCameraModule.takePicture(outputFileOptions, executor, callback);
|
|
|
|
}
|
|
|
|
|
2019-06-26 18:10:57 -04:00
|
|
|
/**
|
2020-03-25 09:30:13 -03:00
|
|
|
* Takes a video and calls the OnVideoSavedCallback when done.
|
2019-06-26 18:10:57 -04:00
|
|
|
*
|
2020-11-09 09:21:59 -04:00
|
|
|
* @param outputFileOptions Options to store the newly captured video.
|
|
|
|
* @param executor The executor in which the callback methods will be run.
|
|
|
|
* @param callback Callback which will receive success or failure.
|
2019-06-26 18:10:57 -04:00
|
|
|
*/
|
2020-11-09 09:21:59 -04:00
|
|
|
public void startRecording(@NonNull VideoCapture.OutputFileOptions outputFileOptions,
|
2020-03-25 09:30:13 -03:00
|
|
|
@NonNull Executor executor,
|
2020-11-09 09:21:59 -04:00
|
|
|
@NonNull OnVideoSavedCallback callback) {
|
|
|
|
mCameraModule.startRecording(outputFileOptions, executor, callback);
|
2019-10-18 17:36:00 -03:00
|
|
|
}
|
2019-06-26 18:10:57 -04:00
|
|
|
|
|
|
|
/** Stops an in progress video. */
|
|
|
|
public void stopRecording() {
|
|
|
|
mCameraModule.stopRecording();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @return True if currently recording. */
|
|
|
|
public boolean isRecording() {
|
|
|
|
return mCameraModule.isRecording();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queries whether the current device has a camera with the specified direction.
|
|
|
|
*
|
|
|
|
* @return True if the device supports the direction.
|
|
|
|
* @throws IllegalStateException if the CAMERA permission is not currently granted.
|
|
|
|
*/
|
|
|
|
@RequiresPermission(permission.CAMERA)
|
2020-03-25 09:30:13 -03:00
|
|
|
public boolean hasCameraWithLensFacing(@CameraSelector.LensFacing int lensFacing) {
|
2019-06-26 18:10:57 -04:00
|
|
|
return mCameraModule.hasCameraWithLensFacing(lensFacing);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggles between the primary front facing camera and the primary back facing camera.
|
|
|
|
*
|
|
|
|
* <p>This will have no effect if not already bound to a lifecycle via {@link
|
|
|
|
* #bindToLifecycle(LifecycleOwner)}.
|
|
|
|
*/
|
|
|
|
public void toggleCamera() {
|
|
|
|
mCameraModule.toggleCamera();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the desired camera by specifying desired lensFacing.
|
|
|
|
*
|
|
|
|
* <p>This will choose the primary camera with the specified camera lensFacing.
|
|
|
|
*
|
|
|
|
* <p>If called before {@link #bindToLifecycle(LifecycleOwner)}, this will set the camera to be
|
|
|
|
* used when first bound to the lifecycle. If the specified lensFacing is not supported by the
|
2020-03-25 09:30:13 -03:00
|
|
|
* device, as determined by {@link #hasCameraWithLensFacing(int)}, the first supported
|
2019-06-26 18:10:57 -04:00
|
|
|
* lensFacing will be chosen when {@link #bindToLifecycle(LifecycleOwner)} is called.
|
|
|
|
*
|
|
|
|
* <p>If called with {@code null} AFTER binding to the lifecycle, the behavior would be
|
|
|
|
* equivalent to unbind the use cases without the lifecycle having to be destroyed.
|
|
|
|
*
|
|
|
|
* @param lensFacing The desired camera lensFacing.
|
|
|
|
*/
|
2020-03-25 09:30:13 -03:00
|
|
|
public void setCameraLensFacing(@Nullable Integer lensFacing) {
|
2019-06-26 18:10:57 -04:00
|
|
|
mCameraModule.setCameraLensFacing(lensFacing);
|
|
|
|
}
|
|
|
|
|
2020-03-25 09:30:13 -03:00
|
|
|
/** Returns the currently selected lensFacing. */
|
2019-06-26 18:10:57 -04:00
|
|
|
@Nullable
|
2020-03-25 09:30:13 -03:00
|
|
|
public Integer getCameraLensFacing() {
|
2019-06-26 18:10:57 -04:00
|
|
|
return mCameraModule.getLensFacing();
|
|
|
|
}
|
|
|
|
|
2020-03-25 09:30:13 -03:00
|
|
|
/** Gets the active flash strategy. */
|
|
|
|
@ImageCapture.FlashMode
|
|
|
|
public int getFlash() {
|
|
|
|
return mCameraModule.getFlash();
|
|
|
|
}
|
|
|
|
|
2019-10-18 17:36:00 -03:00
|
|
|
// Begin Signal Custom Code Block
|
|
|
|
public boolean hasFlash() {
|
|
|
|
return mCameraModule.hasFlash();
|
2019-06-26 18:10:57 -04:00
|
|
|
}
|
2019-10-18 17:36:00 -03:00
|
|
|
// End Signal Custom Code Block
|
2019-06-26 18:10:57 -04:00
|
|
|
|
|
|
|
/** Sets the active flash strategy. */
|
2020-03-25 09:30:13 -03:00
|
|
|
public void setFlash(@ImageCapture.FlashMode int flashMode) {
|
2019-06-26 18:10:57 -04:00
|
|
|
mCameraModule.setFlash(flashMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
private long delta() {
|
|
|
|
return System.currentTimeMillis() - mDownEventTimestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-10-18 17:36:00 -03:00
|
|
|
public boolean onTouchEvent(@NonNull MotionEvent event) {
|
2019-06-26 18:10:57 -04:00
|
|
|
// Disable pinch-to-zoom and tap-to-focus while the camera module is paused.
|
|
|
|
if (mCameraModule.isPaused()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Only forward the event to the pinch-to-zoom gesture detector when pinch-to-zoom is
|
|
|
|
// enabled.
|
|
|
|
if (isPinchToZoomEnabled()) {
|
|
|
|
mPinchToZoomGestureDetector.onTouchEvent(event);
|
|
|
|
}
|
|
|
|
if (event.getPointerCount() == 2 && isPinchToZoomEnabled() && isZoomSupported()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Camera focus
|
|
|
|
switch (event.getAction()) {
|
|
|
|
case MotionEvent.ACTION_DOWN:
|
|
|
|
mDownEventTimestamp = System.currentTimeMillis();
|
|
|
|
break;
|
|
|
|
case MotionEvent.ACTION_UP:
|
2020-11-09 09:21:59 -04:00
|
|
|
if (delta() < ViewConfiguration.getLongPressTimeout()
|
|
|
|
&& mCameraModule.isBoundToLifecycle()) {
|
2019-06-26 18:10:57 -04:00
|
|
|
mUpEvent = event;
|
|
|
|
performClick();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Unhandled event.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Focus the position of the touch event, or focus the center of the preview for
|
|
|
|
* accessibility events
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public boolean performClick() {
|
|
|
|
super.performClick();
|
|
|
|
|
|
|
|
final float x = (mUpEvent != null) ? mUpEvent.getX() : getX() + getWidth() / 2f;
|
|
|
|
final float y = (mUpEvent != null) ? mUpEvent.getY() : getY() + getHeight() / 2f;
|
|
|
|
mUpEvent = null;
|
2019-10-18 17:36:00 -03:00
|
|
|
|
2020-03-25 09:30:13 -03:00
|
|
|
Camera camera = mCameraModule.getCamera();
|
|
|
|
if (camera != null) {
|
2020-11-09 09:21:59 -04:00
|
|
|
MeteringPointFactory pointFactory = mPreviewView.getMeteringPointFactory();
|
|
|
|
float afPointWidth = 1.0f / 6.0f; // 1/6 total area
|
|
|
|
float aePointWidth = afPointWidth * 1.5f;
|
|
|
|
MeteringPoint afPoint = pointFactory.createPoint(x, y, afPointWidth);
|
|
|
|
MeteringPoint aePoint = pointFactory.createPoint(x, y, aePointWidth);
|
|
|
|
|
2020-03-25 09:30:13 -03:00
|
|
|
ListenableFuture<FocusMeteringResult> future =
|
|
|
|
camera.getCameraControl().startFocusAndMetering(
|
|
|
|
new FocusMeteringAction.Builder(afPoint,
|
|
|
|
FocusMeteringAction.FLAG_AF).addPoint(aePoint,
|
|
|
|
FocusMeteringAction.FLAG_AE).build());
|
|
|
|
Futures.addCallback(future, new FutureCallback<FocusMeteringResult>() {
|
|
|
|
@Override
|
|
|
|
public void onSuccess(@Nullable FocusMeteringResult result) {
|
|
|
|
}
|
2019-06-26 18:10:57 -04:00
|
|
|
|
2020-03-25 09:30:13 -03:00
|
|
|
@Override
|
|
|
|
public void onFailure(Throwable t) {
|
|
|
|
// Throw the unexpected error.
|
|
|
|
throw new RuntimeException(t);
|
|
|
|
}
|
|
|
|
}, CameraXExecutors.directExecutor());
|
2019-06-26 18:10:57 -04:00
|
|
|
|
2020-03-25 09:30:13 -03:00
|
|
|
} else {
|
2020-11-09 09:21:59 -04:00
|
|
|
Logger.d(TAG, "cannot access camera");
|
2020-03-25 09:30:13 -03:00
|
|
|
}
|
2019-06-26 18:10:57 -04:00
|
|
|
|
2020-03-25 09:30:13 -03:00
|
|
|
return true;
|
2019-06-26 18:10:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
float rangeLimit(float val, float max, float min) {
|
|
|
|
return Math.min(Math.max(val, min), max);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the view allows pinch-to-zoom.
|
|
|
|
*
|
|
|
|
* @return True if pinch to zoom is enabled.
|
|
|
|
*/
|
|
|
|
public boolean isPinchToZoomEnabled() {
|
|
|
|
return mIsPinchToZoomEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets whether the view should allow pinch-to-zoom.
|
|
|
|
*
|
|
|
|
* <p>When enabled, the user can pinch the camera to zoom in/out. This only has an effect if the
|
|
|
|
* bound camera supports zoom.
|
|
|
|
*
|
|
|
|
* @param enabled True to enable pinch-to-zoom.
|
|
|
|
*/
|
|
|
|
public void setPinchToZoomEnabled(boolean enabled) {
|
|
|
|
mIsPinchToZoomEnabled = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-25 09:30:13 -03:00
|
|
|
* Returns the current zoom ratio.
|
2019-06-26 18:10:57 -04:00
|
|
|
*
|
2020-03-25 09:30:13 -03:00
|
|
|
* @return The current zoom ratio.
|
2019-06-26 18:10:57 -04:00
|
|
|
*/
|
2020-03-25 09:30:13 -03:00
|
|
|
public float getZoomRatio() {
|
|
|
|
return mCameraModule.getZoomRatio();
|
2019-06-26 18:10:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-25 09:30:13 -03:00
|
|
|
* Sets the current zoom ratio.
|
2019-06-26 18:10:57 -04:00
|
|
|
*
|
2020-03-25 09:30:13 -03:00
|
|
|
* <p>Valid zoom values range from {@link #getMinZoomRatio()} to {@link #getMaxZoomRatio()}.
|
2019-06-26 18:10:57 -04:00
|
|
|
*
|
2020-03-25 09:30:13 -03:00
|
|
|
* @param zoomRatio The requested zoom ratio.
|
2019-06-26 18:10:57 -04:00
|
|
|
*/
|
2020-03-25 09:30:13 -03:00
|
|
|
public void setZoomRatio(float zoomRatio) {
|
|
|
|
mCameraModule.setZoomRatio(zoomRatio);
|
2019-06-26 18:10:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-25 09:30:13 -03:00
|
|
|
* Returns the minimum zoom ratio.
|
2019-06-26 18:10:57 -04:00
|
|
|
*
|
2020-03-25 09:30:13 -03:00
|
|
|
* <p>For most cameras this should return a zoom ratio of 1. A zoom ratio of 1 corresponds to a
|
2019-06-26 18:10:57 -04:00
|
|
|
* non-zoomed image.
|
|
|
|
*
|
2020-03-25 09:30:13 -03:00
|
|
|
* @return The minimum zoom ratio.
|
2019-06-26 18:10:57 -04:00
|
|
|
*/
|
2020-03-25 09:30:13 -03:00
|
|
|
public float getMinZoomRatio() {
|
|
|
|
return mCameraModule.getMinZoomRatio();
|
2019-06-26 18:10:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-25 09:30:13 -03:00
|
|
|
* Returns the maximum zoom ratio.
|
2019-06-26 18:10:57 -04:00
|
|
|
*
|
2020-03-25 09:30:13 -03:00
|
|
|
* <p>The zoom ratio corresponds to the ratio between both the widths and heights of a
|
2019-06-26 18:10:57 -04:00
|
|
|
* non-zoomed image and a maximally zoomed image for the selected camera.
|
|
|
|
*
|
2020-03-25 09:30:13 -03:00
|
|
|
* @return The maximum zoom ratio.
|
2019-06-26 18:10:57 -04:00
|
|
|
*/
|
2020-03-25 09:30:13 -03:00
|
|
|
public float getMaxZoomRatio() {
|
|
|
|
return mCameraModule.getMaxZoomRatio();
|
2019-06-26 18:10:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the bound camera supports zooming.
|
|
|
|
*
|
|
|
|
* @return True if the camera supports zooming.
|
|
|
|
*/
|
|
|
|
public boolean isZoomSupported() {
|
|
|
|
return mCameraModule.isZoomSupported();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turns on/off torch.
|
|
|
|
*
|
|
|
|
* @param torch True to turn on torch, false to turn off torch.
|
|
|
|
*/
|
|
|
|
public void enableTorch(boolean torch) {
|
|
|
|
mCameraModule.enableTorch(torch);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns current torch status.
|
|
|
|
*
|
|
|
|
* @return true if torch is on , otherwise false
|
|
|
|
*/
|
|
|
|
public boolean isTorchOn() {
|
|
|
|
return mCameraModule.isTorchOn();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The capture mode used by CameraView.
|
|
|
|
*
|
|
|
|
* <p>This enum can be used to determine which capture mode will be enabled for {@link
|
2020-11-09 09:21:59 -04:00
|
|
|
* SignalCameraView}.
|
2019-06-26 18:10:57 -04:00
|
|
|
*/
|
|
|
|
public enum CaptureMode {
|
|
|
|
/** A mode where image capture is enabled. */
|
|
|
|
IMAGE(0),
|
|
|
|
/** A mode where video capture is enabled. */
|
|
|
|
VIDEO(1),
|
|
|
|
/**
|
|
|
|
* A mode where both image capture and video capture are simultaneously enabled. Note that
|
|
|
|
* this mode may not be available on every device.
|
|
|
|
*/
|
|
|
|
MIXED(2);
|
|
|
|
|
2020-03-25 09:30:13 -03:00
|
|
|
private final int mId;
|
2019-06-26 18:10:57 -04:00
|
|
|
|
|
|
|
int getId() {
|
|
|
|
return mId;
|
|
|
|
}
|
|
|
|
|
|
|
|
CaptureMode(int id) {
|
|
|
|
mId = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CaptureMode fromId(int id) {
|
|
|
|
for (CaptureMode f : values()) {
|
|
|
|
if (f.mId == id) {
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new IllegalArgumentException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static class S extends ScaleGestureDetector.SimpleOnScaleGestureListener {
|
|
|
|
private ScaleGestureDetector.OnScaleGestureListener mListener;
|
|
|
|
|
|
|
|
void setRealGestureDetector(ScaleGestureDetector.OnScaleGestureListener l) {
|
|
|
|
mListener = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onScale(ScaleGestureDetector detector) {
|
|
|
|
return mListener.onScale(detector);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class PinchToZoomGestureDetector extends ScaleGestureDetector
|
|
|
|
implements ScaleGestureDetector.OnScaleGestureListener {
|
|
|
|
PinchToZoomGestureDetector(Context context) {
|
|
|
|
this(context, new S());
|
|
|
|
}
|
|
|
|
|
|
|
|
PinchToZoomGestureDetector(Context context, S s) {
|
|
|
|
super(context, s);
|
|
|
|
s.setRealGestureDetector(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onScale(ScaleGestureDetector detector) {
|
2020-03-25 09:30:13 -03:00
|
|
|
float scale = detector.getScaleFactor();
|
|
|
|
|
|
|
|
// Speeding up the zoom by 2X.
|
|
|
|
if (scale > 1f) {
|
|
|
|
scale = 1.0f + (scale - 1.0f) * 2;
|
|
|
|
} else {
|
|
|
|
scale = 1.0f - (1.0f - scale) * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
float newRatio = getZoomRatio() * scale;
|
|
|
|
newRatio = rangeLimit(newRatio, getMaxZoomRatio(), getMinZoomRatio());
|
|
|
|
setZoomRatio(newRatio);
|
2019-06-26 18:10:57 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onScaleBegin(ScaleGestureDetector detector) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onScaleEnd(ScaleGestureDetector detector) {
|
|
|
|
}
|
|
|
|
}
|
2020-11-09 09:21:59 -04:00
|
|
|
}
|