9
0
mirror of https://gitee.com/shuto/customCamera.git synced 2024-10-06 18:32:07 +08:00
Nouvelle implementation du zoom
This commit is contained in:
ChristopheBoucaut 2014-12-04 11:15:28 +01:00
commit ccc9aca185
2 changed files with 119 additions and 56 deletions

View File

@ -95,5 +95,13 @@
android:layout_weight="1"
android:text="@string/declinePicture" />
</LinearLayout>
</FrameLayout>
<SeekBar
android:id="@+id/niveauZoom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible" />
</RelativeLayout>

View File

@ -49,6 +49,11 @@ public class CameraActivity extends Activity {
*/
private Camera mCamera = null;
/**
* Distance between fingers for the zoom
*/
private static float distanceBetweenFingers;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -130,6 +135,35 @@ public class CameraActivity extends Activity {
CameraPreview mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
// The zoom bar progress
final Camera.Parameters params = mCamera.getParameters();
final SeekBar niveauZoom = (SeekBar) findViewById(R.id.niveauZoom);
int maxZoom = params.getMaxZoom();
final int zoom = params.getZoom();
niveauZoom.setMax(maxZoom);
niveauZoom.setProgress(zoom);
niveauZoom.setVisibility(View.VISIBLE);
// Event on change zoom with the bar.
niveauZoom.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
int progress = 0;
@Override
public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
progress = progresValue;
int newZoom = (int)(zoom+progress);
niveauZoom.setProgress(newZoom);
params.setZoom(newZoom);
mCamera.setParameters(params);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
}
/**
@ -145,13 +179,13 @@ public class CameraActivity extends Activity {
// Event on touch screen to call the manager of the zoom & the auto focus.
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!photoTaken) {
Camera.Parameters params = mCamera.getParameters();
int action = event.getAction();
if (event.getPointerCount() > 1) {
// If we touch with more than one finger
float distanceBetweenFingers = 0;
if (action == MotionEvent.ACTION_POINTER_UP) {
if (action == MotionEvent.ACTION_POINTER_2_DOWN) {
distanceBetweenFingers = getFingerSpacing(event);
} else if (action == MotionEvent.ACTION_MOVE && params.isZoomSupported()) {
mCamera.cancelAutoFocus();
@ -163,9 +197,23 @@ public class CameraActivity extends Activity {
handleFocus(event, params);
}
}
}
return true;
}
/**
* Determine the space between the first two fingers.
*
* @param MotionEvent event Current event which start this calculation.
*
* @return float
*/
private float getFingerSpacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return (float) Math.sqrt(x * x + y * y);
}
/**
* Manage the zoom.
*
@ -178,18 +226,19 @@ public class CameraActivity extends Activity {
int maxZoom = params.getMaxZoom();
// current value for the zoom.
int zoom = params.getZoom();
setZoomProgress(maxZoom, zoom);
// new distance between fingers.
float newDist = getFingerSpacing(event);
if (newDist > distanceBetweenFingers) {
//zoom in
if (zoom < maxZoom/2) {
zoom+=2;
if (zoom < maxZoom) {
zoom++;
}
} else if (newDist < distanceBetweenFingers) {
//zoom out
if (zoom > 0) {
zoom-=2;
zoom--;
}
}
distanceBetweenFingers = newDist;
@ -197,6 +246,19 @@ public class CameraActivity extends Activity {
mCamera.setParameters(params);
}
/**
* To set the seekBar zoom with the pinchZoom
*
* @param maxZoom int the max zoom of the device
* @param zoom int the current zoom
*/
private void setZoomProgress(int maxZoom, int zoom){
SeekBar niveauZoom = (SeekBar) findViewById(R.id.niveauZoom);
niveauZoom.setMax(maxZoom);
niveauZoom.setProgress(zoom*2);
niveauZoom.setVisibility(View.VISIBLE);
}
/**
* Manage the focus.
*
@ -215,19 +277,6 @@ public class CameraActivity extends Activity {
}
}
/**
* Determine the space between the first two fingers.
*
* @param MotionEvent event Current event which start this calculation.
*
* @return float
*/
private float getFingerSpacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return (float) Math.sqrt(x * x + y * y);
}
/**
* Display the miniature.
* @param View view Current view.
@ -260,6 +309,7 @@ public class CameraActivity extends Activity {
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
// resize miniature.
LayoutParams paramsReagrandissement = (LayoutParams) imageView.getLayoutParams();
imageView.setClickable(false);
paramsReagrandissement.width = -1;
paramsReagrandissement.height = -1;
imageView.setLayoutParams(paramsReagrandissement);
@ -269,6 +319,27 @@ public class CameraActivity extends Activity {
}
}
/**
* Set the size and the gravity of the miniature function of photo is taken or not.
*
* @param ImageView imageView Reference to the background image.
* @param Boolean Resize Should we resize or not ? Only when click on "miniature"
*/
public void setParamsMiniature(ImageView imageView, boolean resize){
FrameLayout.LayoutParams paramsMiniature = new FrameLayout.LayoutParams(imageView.getWidth(), imageView.getHeight());
if (resize == true){
paramsMiniature.width = imageView.getWidth()/4;
paramsMiniature.height = imageView.getHeight()/4;
}
if (!photoTaken){
paramsMiniature.gravity = Gravity.BOTTOM;
}
else {
paramsMiniature.gravity = Gravity.TOP;
}
imageView.setLayoutParams(paramsMiniature);
}
/**
* Method to get the device default orientation.
*
@ -335,6 +406,10 @@ public class CameraActivity extends Activity {
final Button photo = (Button)findViewById(R.id.capture);
photo.setVisibility(View.INVISIBLE);
// Hide the zoom progressBar
final SeekBar niveauZoom = (SeekBar) findViewById(R.id.niveauZoom);
niveauZoom.setVisibility(View.INVISIBLE);
// Put button miniature at the top of the page
final Button miniature = (Button)findViewById(R.id.miniature);
final LayoutParams params = (LinearLayout.LayoutParams)miniature.getLayoutParams();
@ -394,6 +469,7 @@ public class CameraActivity extends Activity {
keepPhoto.setVisibility(View.INVISIBLE);
photo.setVisibility(View.VISIBLE);
niveauZoom.setVisibility(View.VISIBLE);
mCamera.startPreview();
}
});
@ -430,25 +506,4 @@ public class CameraActivity extends Activity {
this.setResult(3);
this.finish();
}
/**
* Set the size and the gravity of the miniature function of photo is taken or not.
*
* @param ImageView imageView Reference to the background image.
* @param Boolean Resize Should we resize or not ? Only when click on "miniature"
*/
public void setParamsMiniature(ImageView imageView, boolean resize){
FrameLayout.LayoutParams paramsMiniature = new FrameLayout.LayoutParams(imageView.getWidth(), imageView.getHeight());
if (resize == true){
paramsMiniature.width = imageView.getWidth()/4;
paramsMiniature.height = imageView.getHeight()/4;
}
if (!photoTaken){
paramsMiniature.gravity = Gravity.BOTTOM;
}
else {
paramsMiniature.gravity = Gravity.TOP;
}
imageView.setLayoutParams(paramsMiniature);
}
}