作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
经过对 SO 的一些研究,我找不到使用 android.hardware.camera 实现缩放的方法。我正在使用 SeekBar 但这不是我需要的。有谁知道我在哪里可以找到适用于 Android SKD >= 15 的示例?
在我的 cameraFragment 中,我正在使用 OnSeekBarChangeListener在 onProgressChanged 方法中,我设置了相机的缩放。
seekbar.setOnSeekBarChangeListener(this);
.....
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
try {
Camera.Parameters params = camera.getParameters();
params.setZoom(progress);
currentZoomLevel = progress;
params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
camera.setParameters(params);
//set zoom level value
try {
float ratio = ((float) params.getZoomRatios().get(progress)) / 100;
tvZoomLevel.setText(String.format("%.1fX", ratio));
} catch (Exception ex) {
Log.e(TAG, ex.getMessage());
}
} catch (Exception ex) {
Log.e(TAG, ex.getMessage());
}
}
最佳答案
1.覆盖onTouchEvent(MotionEvent事件)
2.获取相机最大缩放级别param
3.Adjust your pinch zoom into a camera param
@Override
public boolean onTouchEvent(MotionEvent event) {
// Get the pointer ID
Camera.Parameters params = mCamera.getParameters();
int action = event.getAction();
if (event.getPointerCount() > 1) {
// handle multi-touch events
if (action == MotionEvent.ACTION_POINTER_DOWN) {
mDist = getFingerSpacing(event);
} else if (action == MotionEvent.ACTION_MOVE && params.isZoomSupported()) {
mCamera.cancelAutoFocus();
handleZoom(event, params);
}
} else {
// handle single touch events
if (action == MotionEvent.ACTION_UP) {
handleFocus(event, params);
}
}
return true;
}
private void handleZoom(MotionEvent event, Camera.Parameters params) {
int maxZoom = params.getMaxZoom();
int zoom = params.getZoom();
float newDist = getFingerSpacing(event);
if (newDist > mDist) {
//zoom in
if (zoom < maxZoom)
zoom++;
} else if (newDist < mDist) {
//zoom out
if (zoom > 0)
zoom--;
}
mDist = newDist;
params.setZoom(zoom);
mCamera.setParameters(params);
}
public void handleFocus(MotionEvent event, Camera.Parameters params) {
int pointerId = event.getPointerId(0);
int pointerIndex = event.findPointerIndex(pointerId);
// Get the pointer's current position
float x = event.getX(pointerIndex);
float y = event.getY(pointerIndex);
List<String> supportedFocusModes = params.getSupportedFocusModes();
if (supportedFocusModes != null && supportedFocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
mCamera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean b, Camera camera) {
// currently set to auto-focus on single touch
}
});
}
}
/** Determine the space between the first two fingers */
private float getFingerSpacing(MotionEvent event) {
// ...
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
This certain step help you a basic implemention of your requirement.
关于java - 如何在Android中实现双指缩放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40999674/
我是一名优秀的程序员,十分优秀!