mirror of
https://github.com/SimpleMobileTools/Simple-Camera.git
synced 2025-06-27 09:02:59 +02:00
activate the shutter button at long pressing the preview
This commit is contained in:
@ -46,7 +46,7 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
|||||||
private Preview preview;
|
private Preview preview;
|
||||||
private int currCamera;
|
private int currCamera;
|
||||||
private boolean isFlashEnabled;
|
private boolean isFlashEnabled;
|
||||||
private boolean isPhoto;
|
private boolean isInPhotoMode;
|
||||||
private boolean isAskingPermissions;
|
private boolean isAskingPermissions;
|
||||||
private boolean isCameraAvailable;
|
private boolean isCameraAvailable;
|
||||||
private int currVideoRecTimer;
|
private int currVideoRecTimer;
|
||||||
@ -84,7 +84,7 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
|||||||
preview.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
preview.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
|
||||||
viewHolder.addView(preview);
|
viewHolder.addView(preview);
|
||||||
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
|
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
|
||||||
isPhoto = true;
|
isInPhotoMode = true;
|
||||||
timerHandler = new Handler();
|
timerHandler = new Handler();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,7 +165,11 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isPhoto) {
|
handleShutter();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleShutter() {
|
||||||
|
if (isInPhotoMode) {
|
||||||
preview.takePicture();
|
preview.takePicture();
|
||||||
} else {
|
} else {
|
||||||
final Resources res = getResources();
|
final Resources res = getResources();
|
||||||
@ -201,10 +205,10 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
|||||||
}
|
}
|
||||||
disableFlash();
|
disableFlash();
|
||||||
hideTimer();
|
hideTimer();
|
||||||
isPhoto = !isPhoto;
|
isInPhotoMode = !isInPhotoMode;
|
||||||
toggleCameraBtn.setVisibility(View.VISIBLE);
|
toggleCameraBtn.setVisibility(View.VISIBLE);
|
||||||
|
|
||||||
if (isPhoto) {
|
if (isInPhotoMode) {
|
||||||
initPhotoButtons();
|
initPhotoButtons();
|
||||||
} else {
|
} else {
|
||||||
initVideoButtons();
|
initVideoButtons();
|
||||||
@ -274,7 +278,7 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
|||||||
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
|
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isPhoto) {
|
if (!isInPhotoMode) {
|
||||||
initVideoButtons();
|
initVideoButtons();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -333,4 +337,9 @@ public class MainActivity extends AppCompatActivity implements SensorEventListen
|
|||||||
public void setIsCameraAvailable(boolean available) {
|
public void setIsCameraAvailable(boolean available) {
|
||||||
isCameraAvailable = available;
|
isCameraAvailable = available;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void activateShutter() {
|
||||||
|
handleShutter();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ import android.view.Surface;
|
|||||||
import android.view.SurfaceHolder;
|
import android.view.SurfaceHolder;
|
||||||
import android.view.SurfaceView;
|
import android.view.SurfaceView;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.view.View.OnLongClickListener;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -21,7 +22,7 @@ import java.io.IOException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.OnTouchListener {
|
public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.OnTouchListener, OnLongClickListener, View.OnClickListener{
|
||||||
private static final String TAG = Preview.class.getSimpleName();
|
private static final String TAG = Preview.class.getSimpleName();
|
||||||
private static final int FOCUS_AREA_SIZE = 200;
|
private static final int FOCUS_AREA_SIZE = 200;
|
||||||
private static final int PHOTO_PREVIEW_LENGTH = 1000;
|
private static final int PHOTO_PREVIEW_LENGTH = 1000;
|
||||||
@ -41,6 +42,8 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||||||
private static boolean isRecording;
|
private static boolean isRecording;
|
||||||
private static boolean isVideoMode;
|
private static boolean isVideoMode;
|
||||||
private static String curVideoPath;
|
private static String curVideoPath;
|
||||||
|
private static int lastClickX;
|
||||||
|
private static int lastClickY;
|
||||||
|
|
||||||
public Preview(Context context) {
|
public Preview(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
@ -57,6 +60,8 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||||||
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
|
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
|
||||||
canTakePicture = false;
|
canTakePicture = false;
|
||||||
surfaceView.setOnTouchListener(this);
|
surfaceView.setOnTouchListener(this);
|
||||||
|
surfaceView.setOnClickListener(this);
|
||||||
|
surfaceView.setOnLongClickListener(this);
|
||||||
isFlashEnabled = false;
|
isFlashEnabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,12 +179,12 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private void focusArea(MotionEvent event) {
|
private void focusArea() {
|
||||||
if (camera == null)
|
if (camera == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
camera.cancelAutoFocus();
|
camera.cancelAutoFocus();
|
||||||
final Rect focusRect = calculateFocusArea(event.getX(), event.getY());
|
final Rect focusRect = calculateFocusArea(lastClickX, lastClickY);
|
||||||
if (parameters.getMaxNumFocusAreas() > 0) {
|
if (parameters.getMaxNumFocusAreas() > 0) {
|
||||||
final List<Camera.Area> focusAreas = new ArrayList<>(1);
|
final List<Camera.Area> focusAreas = new ArrayList<>(1);
|
||||||
focusAreas.add(new Camera.Area(focusRect, 1000));
|
focusAreas.add(new Camera.Area(focusRect, 1000));
|
||||||
@ -326,7 +331,8 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onTouch(View v, MotionEvent event) {
|
public boolean onTouch(View v, MotionEvent event) {
|
||||||
focusArea(event);
|
lastClickX = (int) event.getX();
|
||||||
|
lastClickY = (int) event.getY();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -419,9 +425,22 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
|
|||||||
isRecording = false;
|
isRecording = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onLongClick(View v) {
|
||||||
|
callback.activateShutter();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
focusArea();
|
||||||
|
}
|
||||||
|
|
||||||
public interface PreviewListener {
|
public interface PreviewListener {
|
||||||
void setFlashAvailable(boolean available);
|
void setFlashAvailable(boolean available);
|
||||||
|
|
||||||
void setIsCameraAvailable(boolean available);
|
void setIsCameraAvailable(boolean available);
|
||||||
|
|
||||||
|
void activateShutter();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user