diff options
author | Wu-cheng Li <wuchengli@google.com> | 2009-09-27 20:45:42 -0700 |
---|---|---|
committer | Wu-cheng Li <wuchengli@google.com> | 2009-09-28 12:34:36 -0700 |
commit | 35bda2dc8ab95a7baf68b544cf97826aabff6210 (patch) | |
tree | 769aa46699b7d08407bce1cdcfe8b5bacd5b56c1 /src | |
parent | 2a79ecbf5e5a76cfead677262deb089bd3e2e277 (diff) | |
download | LegacyCamera-35bda2dc8ab95a7baf68b544cf97826aabff6210.zip LegacyCamera-35bda2dc8ab95a7baf68b544cf97826aabff6210.tar.gz LegacyCamera-35bda2dc8ab95a7baf68b544cf97826aabff6210.tar.bz2 |
Add zoom UI back.
Diffstat (limited to 'src')
-rw-r--r-- | src/com/android/camera/Camera.java | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/src/com/android/camera/Camera.java b/src/com/android/camera/Camera.java index 4b06211..2ef70d7 100644 --- a/src/com/android/camera/Camera.java +++ b/src/com/android/camera/Camera.java @@ -64,6 +64,7 @@ import android.view.Window; import android.view.WindowManager; import android.view.MenuItem.OnMenuItemClickListener; import android.widget.ImageView; +import android.widget.ZoomButtonsController; import com.android.camera.gallery.IImage; import com.android.camera.gallery.IImageList; @@ -98,8 +99,26 @@ public class Camera extends Activity implements View.OnClickListener, private static final int SCREEN_DELAY = 2 * 60 * 1000; private static final int FOCUS_BEEP_VOLUME = 100; + private double mZoomValue; // The current zoom value. + private double mZoomStep; + private double mZoomMax; + public static final double ZOOM_STEP_MIN = 0.25; + public static final String ZOOM_STOP = "stop"; + public static final String ZOOM_IMMEDIATE = "zoom-immediate"; + public static final String ZOOM_CONTINUOUS = "zoom-continuous"; + public static final double ZOOM_MIN = 1.0; + public static final String ZOOM_SPEED = "99"; + private Parameters mParameters; + // The non-standard parameter strings to communicate with camera driver. + // This will be removed in the future. + public static final String PARM_ZOOM_STATE = "mot-zoom-state"; + public static final String PARM_ZOOM_STEP = "mot-zoom-step"; + public static final String PARM_ZOOM_TO_LEVEL = "mot-zoom-to-level"; + public static final String PARM_ZOOM_SPEED = "mot-zoom-speed"; + public static final String PARM_ZOOM_MAX = "mot-max-picture-continuous-zoom"; + private OrientationEventListener mOrientationListener; private int mLastOrientation = OrientationEventListener.ORIENTATION_UNKNOWN; private SharedPreferences mPreferences; @@ -121,6 +140,7 @@ public class Camera extends Activity implements View.OnClickListener, private IconIndicator mGpsIndicator; private IconIndicator mFlashIndicator; private ToneGenerator mFocusToneGenerator; + private ZoomButtonsController mZoomButtons; private GestureDetector mGestureDetector; private Switcher mSwitcher; private boolean mStartPreviewFail = false; @@ -165,6 +185,7 @@ public class Camera extends Activity implements View.OnClickListener, new RawPictureCallback(); private final AutoFocusCallback mAutoFocusCallback = new AutoFocusCallback(); + private final ZoomCallback mZoomCallback = new ZoomCallback(); // Use the ErrorCallback to capture the crash count // on the mediaserver private final ErrorCallback mErrorCallback = new ErrorCallback(); @@ -280,6 +301,8 @@ public class Camera extends Activity implements View.OnClickListener, initializeFocusTone(); + initializeZoom(); + mFirstTimeInitialized = true; } @@ -308,11 +331,57 @@ public class Camera extends Activity implements View.OnClickListener, checkStorage(); + if (mZoomButtons != null) { + mZoomValue = Double.parseDouble( + mParameters.get(PARM_ZOOM_TO_LEVEL)); + mCameraDevice.setZoomCallback(mZoomCallback); + } + if (!mIsImageCaptureIntent) { updateThumbnailButton(); } } + private void initializeZoom() { + // Check if the phone has zoom capability. + String zoomState = mParameters.get(PARM_ZOOM_STATE); + if (zoomState == null) return; + + mZoomValue = Double.parseDouble(mParameters.get(PARM_ZOOM_TO_LEVEL)); + mZoomMax = Double.parseDouble(mParameters.get(PARM_ZOOM_MAX)); + mZoomStep = Double.parseDouble(mParameters.get(PARM_ZOOM_STEP)); + mParameters.set(PARM_ZOOM_SPEED, ZOOM_SPEED); + mCameraDevice.setParameters(mParameters); + + mGestureDetector = new GestureDetector(this, new ZoomGestureListener()); + mCameraDevice.setZoomCallback(mZoomCallback); + mZoomButtons = new ZoomButtonsController(mSurfaceView); + mZoomButtons.setAutoDismissed(true); + mZoomButtons.setZoomSpeed(100); + mZoomButtons.setOnZoomListener( + new ZoomButtonsController.OnZoomListener() { + public void onVisibilityChanged(boolean visible) { + if (visible) { + updateZoomButtonsEnabled(); + } + } + + public void onZoom(boolean zoomIn) { + if (isZooming()) return; + + if (zoomIn) { + if (mZoomValue < mZoomMax) { + zoomToLevel(ZOOM_CONTINUOUS, mZoomValue + mZoomStep); + } + } else { + if (mZoomValue > ZOOM_MIN) { + zoomToLevel(ZOOM_CONTINUOUS, mZoomValue - mZoomStep); + } + } + } + }); + } + public void onVisibilityChanged(boolean visible) { // When the on-screen setting is not displayed, we show the gripper. // When the on-screen setting is displayed, we hide the gripper. @@ -327,6 +396,82 @@ public class Camera extends Activity implements View.OnClickListener, } } + private boolean isZooming() { + mParameters = mCameraDevice.getParameters(); + return "continuous".equals(mParameters.get(PARM_ZOOM_STATE)); + } + + private void zoomToLevel(String type, double zoomValue) { + if (zoomValue > mZoomMax) zoomValue = mZoomMax; + if (zoomValue < ZOOM_MIN) zoomValue = ZOOM_MIN; + + // If the application sets a unchanged zoom value, the driver will stuck + // at the zoom state. This is a work-around to ensure the state is at + // "stop". + mParameters.set(PARM_ZOOM_STATE, ZOOM_STOP); + mCameraDevice.setParameters(mParameters); + + mParameters.set(PARM_ZOOM_TO_LEVEL, Double.toString(zoomValue)); + mParameters.set(PARM_ZOOM_STATE, type); + mCameraDevice.setParameters(mParameters); + + if (ZOOM_IMMEDIATE.equals(type)) mZoomValue = zoomValue; + } + + private void updateZoomButtonsEnabled() { + mZoomButtons.setZoomInEnabled(mZoomValue < mZoomMax); + mZoomButtons.setZoomOutEnabled(mZoomValue > ZOOM_MIN); + } + + private class ZoomGestureListener extends + GestureDetector.SimpleOnGestureListener { + @Override + public boolean onDown(MotionEvent e) { + // Show zoom buttons only when preview is started and snapshot + // is not in progress. mZoomButtons may be null if it is not + // initialized. + if (!mPausing && isCameraIdle() && mPreviewing + && mZoomButtons != null) { + mZoomButtons.setVisible(true); + } + return true; + } + + @Override + public boolean onDoubleTap(MotionEvent e) { + // Perform zoom only when preview is started and snapshot is not in + // progress. + if (mPausing || !isCameraIdle() || !mPreviewing + || mZoomButtons == null || isZooming()) { + return false; + } + + if (mZoomValue < mZoomMax) { + // Zoom in to the maximum. + while (mZoomValue < mZoomMax) { + zoomToLevel(ZOOM_IMMEDIATE, mZoomValue + ZOOM_STEP_MIN); + // Wait for a while so we are not changing zoom too fast. + try { + Thread.sleep(5); + } catch (InterruptedException ex) { + } + } + } else { + // Zoom out to the minimum. + while (mZoomValue > ZOOM_MIN) { + zoomToLevel(ZOOM_IMMEDIATE, mZoomValue - ZOOM_STEP_MIN); + // Wait for a while so we are not changing zoom too fast. + try { + Thread.sleep(5); + } catch (InterruptedException ex) { + } + } + } + updateZoomButtonsEnabled(); + return true; + } + } + @Override public boolean dispatchTouchEvent(MotionEvent m) { if (!super.dispatchTouchEvent(m) && mGestureDetector != null) { @@ -525,6 +670,16 @@ public class Camera extends Activity implements View.OnClickListener, } } + private final class ZoomCallback + implements android.hardware.Camera.ZoomCallback { + public void onZoomUpdate(int zoomLevel, + android.hardware.Camera camera) { + mZoomValue = (double) zoomLevel / 1000; + Log.v(TAG, "ZoomCallback: zoom level=" + zoomLevel); + updateZoomButtonsEnabled(); + } + } + private class ImageCapture { private boolean mCancel = false; @@ -1095,6 +1250,12 @@ public class Camera extends Activity implements View.OnClickListener, mImageCapture.clearLastData(); mImageCapture = null; + // This is necessary to make the ZoomButtonsController unregister + // its configuration change receiver. + if (mZoomButtons != null) { + mZoomButtons.setVisible(false); + } + // Remove the messages in the event queue. mHandler.removeMessages(RESTART_PREVIEW); mHandler.removeMessages(FIRST_TIME_INIT); @@ -1134,6 +1295,7 @@ public class Camera extends Activity implements View.OnClickListener, // in progress. if (canTakePicture()) { Log.v(TAG, "Start autofocus."); + if (mZoomButtons != null) mZoomButtons.setVisible(false); mFocusStartTime = System.currentTimeMillis(); mFocusState = FOCUSING; updateFocusIndicator(); @@ -1240,6 +1402,7 @@ public class Camera extends Activity implements View.OnClickListener, if (mFocusMode.equals(Parameters.FOCUS_MODE_INFINITY) || (mFocusState == FOCUS_SUCCESS || mFocusState == FOCUS_FAIL)) { + if (mZoomButtons != null) mZoomButtons.setVisible(false); mImageCapture.onSnap(); } else if (mFocusState == FOCUSING) { // Half pressing the shutter (i.e. the focus button event) will |