Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加录制和拍照的放缩手势 #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/src/main/java/com/cjt2325/cameraview/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import android.widget.Toast;

import com.cjt2325.cameralibrary.JCameraView;
import com.cjt2325.cameralibrary.lisenter.JCameraLisenter;
import com.cjt2325.cameralibrary.listener.JCameraListener;

import java.io.File;

Expand All @@ -35,7 +35,7 @@ protected void onCreate(Bundle savedInstanceState) {
jCameraView.setSaveVideoPath(Environment.getExternalStorageDirectory().getPath() + File.separator + "JCamera");

//JCameraView监听
jCameraView.setJCameraLisenter(new JCameraLisenter() {
jCameraView.setJCameraLisenter(new JCameraListener() {
@Override
public void captureSuccess(Bitmap bitmap) {
//获取图片bitmap
Expand Down
75 changes: 40 additions & 35 deletions camera/src/main/java/com/cjt2325/cameralibrary/CameraInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -206,34 +206,35 @@ public void setZoom(float zoom, int type) {
if (mCamera == null) {
return;
}
if (mParams == null) {
mParams = mCamera.getParameters();
}
if (!mParams.isZoomSupported() || !mParams.isSmoothZoomSupported()) {
if (!mParams.isZoomSupported()) {
return;
}
int scaleRate = 1;
switch (type) {
case TYPE_RECORDER:
//如果不是录制视频中,上滑不会缩放
if (!isRecorder) {
return;
}
if (zoom >= 0) {
//每移动50个像素缩放一个级别
int scaleRate = (int) (zoom / 40);
if (scaleRate <= mParams.getMaxZoom() && scaleRate >= nowScaleRate && recordScleRate != scaleRate) {
mParams.setZoom(scaleRate);
mCamera.setParameters(mParams);
recordScleRate = scaleRate;
//每移动50个像素缩放一个级别
scaleRate = (int) zoom;
if (scaleRate < mParams.getMaxZoom()) {
nowScaleRate += scaleRate;
if (nowScaleRate < 0) {
nowScaleRate = 0;
} else if (nowScaleRate > mParams.getMaxZoom()) {
nowScaleRate = mParams.getMaxZoom();
}
mParams.setZoom(nowScaleRate);
mCamera.setParameters(mParams);
}
break;
case TYPE_CAPTURE:
if (isRecorder) {
return;
}
//每移动50个像素缩放一个级别
int scaleRate = (int) (zoom / 50);
scaleRate = (int) zoom;
if (scaleRate < mParams.getMaxZoom()) {
nowScaleRate += scaleRate;
if (nowScaleRate < 0) {
Expand Down Expand Up @@ -345,29 +346,7 @@ public void doStartPreview(SurfaceHolder holder, float screenProp) {
this.mHolder = holder;
if (mCamera != null) {
try {
mParams = mCamera.getParameters();
Camera.Size previewSize = CameraParamUtil.getInstance().getPreviewSize(mParams
.getSupportedPreviewSizes(), 1000, screenProp);
Camera.Size pictureSize = CameraParamUtil.getInstance().getPictureSize(mParams
.getSupportedPictureSizes(), 1200, screenProp);

mParams.setPreviewSize(previewSize.width, previewSize.height);

preview_width = previewSize.width;
preview_height = previewSize.height;

mParams.setPictureSize(pictureSize.width, pictureSize.height);

if (CameraParamUtil.getInstance().isSupportedFocusMode(
mParams.getSupportedFocusModes(),
Camera.Parameters.FOCUS_MODE_AUTO)) {
mParams.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
}
if (CameraParamUtil.getInstance().isSupportedPictureFormats(mParams.getSupportedPictureFormats(),
ImageFormat.JPEG)) {
mParams.setPictureFormat(ImageFormat.JPEG);
mParams.setJpegQuality(100);
}
setShootParams(screenProp);
mCamera.setParameters(mParams);
mParams = mCamera.getParameters();
mCamera.setPreviewDisplay(holder); //SurfaceView
Expand All @@ -382,6 +361,32 @@ public void doStartPreview(SurfaceHolder holder, float screenProp) {
}
}

private void setShootParams(float screenProp) {
mParams = mCamera.getParameters();
Camera.Size previewSize = CameraParamUtil.getInstance().getPreviewSize(mParams
.getSupportedPreviewSizes(), 1000, screenProp);
Camera.Size pictureSize = CameraParamUtil.getInstance().getPictureSize(mParams
.getSupportedPictureSizes(), 1200, screenProp);

mParams.setPreviewSize(previewSize.width, previewSize.height);

preview_width = previewSize.width;
preview_height = previewSize.height;

mParams.setPictureSize(pictureSize.width, pictureSize.height);

if (CameraParamUtil.getInstance().isSupportedFocusMode(
mParams.getSupportedFocusModes(),
Camera.Parameters.FOCUS_MODE_AUTO)) {
mParams.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
}
if (CameraParamUtil.getInstance().isSupportedPictureFormats(mParams.getSupportedPictureFormats(),
ImageFormat.JPEG)) {
mParams.setPictureFormat(ImageFormat.JPEG);
mParams.setJpegQuality(100);
}
}

/**
* 停止预览
*/
Expand Down
40 changes: 33 additions & 7 deletions camera/src/main/java/com/cjt2325/cameralibrary/CaptureButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

Expand Down Expand Up @@ -44,10 +45,6 @@ public class CaptureButton extends View {
private int outside_color = 0xEEDCDCDC; //外圆背景色
private int inside_color = 0xFFFFFFFF; //内圆背景色


private float event_Y; //Touch_Event_Down时候记录的Y值


private Paint mPaint;

private float strokeWidth; //进度条宽度
Expand Down Expand Up @@ -141,6 +138,7 @@ protected void onDraw(Canvas canvas) {
}
}

float lastLeght = -1;

@Override
public boolean onTouchEvent(MotionEvent event) {
Expand All @@ -149,24 +147,52 @@ public boolean onTouchEvent(MotionEvent event) {
LogUtil.i("state = " + state);
if (event.getPointerCount() > 1 || state != STATE_IDLE)
break;
event_Y = event.getY(); //记录Y值
state = STATE_PRESS; //修改当前状态为点击按下

//判断按钮状态是否为可录制状态
if ((button_state == BUTTON_STATE_ONLY_RECORDER || button_state == BUTTON_STATE_BOTH))
postDelayed(longPressRunnable, 500); //同时延长500启动长按后处理的逻辑Runnable
break;
case MotionEvent.ACTION_MOVE:
Log.e("TAg", event.getPointerCount() + "");
if (captureLisenter != null
&& state == STATE_RECORDERING
&& (button_state == BUTTON_STATE_ONLY_RECORDER || button_state == BUTTON_STATE_BOTH)) {
//记录当前Y值与按下时候Y值的差值,调用缩放回调接口
captureLisenter.recordZoom(event_Y - event.getY());
if (event.getPointerCount() == 3) {
//第一个点
float point_2_X = event.getX(1);
float point_2_Y = event.getY(1);
//第二个点
float point_3_X = event.getX(2);
float point_3_Y = event.getY(2);
float result = (float) Math.sqrt(Math.pow(point_2_X - point_3_X, 2) + Math.pow(point_2_Y -
point_3_Y, 2));

if (lastLeght == -1) {
lastLeght = result;
return true;
}
if (Math.abs(lastLeght - result) > 3) {
if (lastLeght > result) {
//缩小
captureLisenter.recordZoom(-1);
} else {
//放大
captureLisenter.recordZoom(1);
}
lastLeght = result;
}
Log.e("TAg", result + "");
} else {
lastLeght = -1;
}
}

break;
case MotionEvent.ACTION_UP:
//根据当前按钮的状态进行相应的处理
handlerUnpressByState();
lastLeght = -1;
break;
}
return true;
Expand Down
34 changes: 19 additions & 15 deletions camera/src/main/java/com/cjt2325/cameralibrary/JCameraView.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ public class JCameraView extends FrameLayout implements CameraInterface.CameraOp
//缩放梯度
private int zoomGradient = 0;

private boolean firstTouch = true;
private float firstTouchLength = 0;
private float lastTouchLength = -1;

public JCameraView(Context context) {
this(context, null);
Expand Down Expand Up @@ -279,7 +278,7 @@ public void surfaceDestroyed(SurfaceHolder holder) {

@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
if (event.getPointerCount() == 1) {
//显示对焦指示器
Expand All @@ -289,10 +288,9 @@ public boolean onTouchEvent(MotionEvent event) {
Log.i("CJT", "ACTION_DOWN = " + 2);
}
break;

case MotionEvent.ACTION_MOVE:
if (event.getPointerCount() == 1) {
firstTouch = true;
}

if (event.getPointerCount() == 2) {
//第一个点
float point_1_X = event.getX(0);
Expand All @@ -303,20 +301,26 @@ public boolean onTouchEvent(MotionEvent event) {

float result = (float) Math.sqrt(Math.pow(point_1_X - point_2_X, 2) + Math.pow(point_1_Y -
point_2_Y, 2));

if (firstTouch) {
firstTouchLength = result;
firstTouch = false;
if (lastTouchLength == -1) {
lastTouchLength = result;
return true;
}
if ((int) (result - firstTouchLength) / zoomGradient != 0) {
firstTouch = true;
machine.zoom(result - firstTouchLength, CameraInterface.TYPE_CAPTURE);
if (Math.abs(lastTouchLength - result) > 3) {
if (lastTouchLength > result) {
//缩小
machine.zoom(-1, CameraInterface.TYPE_CAPTURE);
} else {
//放大
machine.zoom(1, CameraInterface.TYPE_CAPTURE);
}
lastTouchLength = result;
}
// Log.i("CJT", "result = " + (result - firstTouchLength));
} else {
lastTouchLength = -1;
}
break;
case MotionEvent.ACTION_UP:
firstTouch = true;
lastTouchLength = -1;
break;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import android.widget.Toast;

import com.cjt2325.cameralibrary.JCameraView;
import com.cjt2325.cameralibrary.lisenter.ErrorLisenter;
import com.cjt2325.cameralibrary.lisenter.JCameraLisenter;
import com.cjt2325.cameralibrary.listener.ErrorListener;
import com.cjt2325.cameralibrary.listener.JCameraListener;
import com.cjt2325.cameralibrary.util.DeviceUtil;
import com.cjt2325.cameralibrary.util.FileUtil;

Expand All @@ -35,7 +35,7 @@ protected void onCreate(Bundle savedInstanceState) {
jCameraView.setFeatures(JCameraView.BUTTON_STATE_BOTH);
jCameraView.setTip("JCameraView Tip");
jCameraView.setMediaQuality(JCameraView.MEDIA_QUALITY_MIDDLE);
jCameraView.setErrorLisenter(new ErrorLisenter() {
jCameraView.setErrorLisenter(new ErrorListener() {
@Override
public void onError() {
//错误监听
Expand All @@ -51,7 +51,7 @@ public void AudioPermissionError() {
}
});
//JCameraView监听
jCameraView.setJCameraLisenter(new JCameraLisenter() {
jCameraView.setJCameraLisenter(new JCameraListener() {
@Override
public void captureSuccess(Bitmap bitmap) {
//获取图片bitmap
Expand Down
19 changes: 9 additions & 10 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
# Project-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.

## Project-wide Gradle settings.
#
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

#
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# Default value: -Xmx1024m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

#
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# org.gradle.parallel=true
#Thu Sep 21 22:50:28 CST 2017
systemProp.http.proxyHost=127.0.0.1
systemProp.http.proxyPort=1080