draw a rectangle on the focused area for a moment

This commit is contained in:
tibbi 2016-07-12 13:17:02 +02:00
parent d9879aed69
commit fc9702ca06
4 changed files with 88 additions and 2 deletions

View File

@ -0,0 +1,66 @@
package com.simplemobiletools.camera;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.view.ViewGroup;
public class FocusRectView extends ViewGroup {
private static final int RECT_SIZE = 50;
private static final int RECT_DURATION = 500;
private static Paint mPaint;
private static Rect mRect;
private static Handler mHandler;
private static boolean mDrawRect;
private static int mPrimaryColor;
public FocusRectView(Context context) {
super(context);
setWillNotDraw(false);
mHandler = new Handler();
mPrimaryColor = getResources().getColor(R.color.colorPrimary);
setupPaint();
}
private void setupPaint() {
mPaint = new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(mPrimaryColor);
mPaint.setStrokeWidth(2);
}
public void drawFocusRect(int x, int y) {
mRect = new Rect(x - RECT_SIZE, y - RECT_SIZE, x + RECT_SIZE, y + RECT_SIZE);
toggleRect(true);
mHandler.removeCallbacksAndMessages(null);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
toggleRect(false);
}
}, RECT_DURATION);
}
private void toggleRect(boolean show) {
mDrawRect = show;
invalidate();
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mDrawRect) {
canvas.drawRect(mRect, mPaint);
}
}
}

View File

@ -29,7 +29,7 @@ import java.util.List;
public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.OnTouchListener, OnLongClickListener, View.OnClickListener,
MediaScannerConnection.OnScanCompletedListener {
private static final String TAG = Preview.class.getSimpleName();
private static final int FOCUS_AREA_SIZE = 200;
private static final int FOCUS_AREA_SIZE = 100;
private static final int PHOTO_PREVIEW_LENGTH = 1000;
private static final float RATIO_TOLERANCE = 0.1f;
@ -335,6 +335,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
final List<Camera.Area> focusAreas = new ArrayList<>(1);
focusAreas.add(new Camera.Area(focusRect, 1000));
mParameters.setFocusAreas(focusAreas);
mCallback.drawFocusRect(mLastClickX, mLastClickY);
}
mCamera.setParameters(mParameters);
@ -664,5 +665,7 @@ public class Preview extends ViewGroup implements SurfaceHolder.Callback, View.O
int getCurrentOrientation();
void videoSaved(Uri uri);
void drawFocusRect(int x, int y);
}
}

View File

@ -34,6 +34,7 @@ import android.widget.TextView;
import com.simplemobiletools.camera.Config;
import com.simplemobiletools.camera.Constants;
import com.simplemobiletools.camera.FocusRectView;
import com.simplemobiletools.camera.PhotoProcessor;
import com.simplemobiletools.camera.Preview;
import com.simplemobiletools.camera.Preview.PreviewListener;
@ -58,12 +59,12 @@ public class MainActivity extends AppCompatActivity
@BindView(R.id.about) View mAboutBtn;
@BindView(R.id.last_photo_video_preview) ImageView mLastPhotoVideoPreview;
private static final String TAG = MainActivity.class.getSimpleName();
private static final int CAMERA_STORAGE_PERMISSION = 1;
private static final int AUDIO_PERMISSION = 2;
private static SensorManager mSensorManager;
private static Preview mPreview;
private static FocusRectView mFocusRectView;
private static Handler mTimerHandler;
private static Uri mPreviewUri;
@ -160,6 +161,10 @@ public class MainActivity extends AppCompatActivity
mPreview = new Preview(this, (SurfaceView) findViewById(R.id.camera_view), this);
mPreview.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
mViewHolder.addView(mPreview);
mFocusRectView = new FocusRectView(getApplicationContext());
mViewHolder.addView(mFocusRectView);
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mIsInPhotoMode = true;
mTimerHandler = new Handler();
@ -579,6 +584,13 @@ public class MainActivity extends AppCompatActivity
}
}
@Override
public void drawFocusRect(int x, int y) {
if (mFocusRectView != null) {
mFocusRectView.drawFocusRect(x, y);
}
}
@Override
public void mediaSaved(String path) {
final String[] paths = {path};

View File

@ -11,6 +11,11 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<SurfaceView
android:id="@+id/focus_rect_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/about"
android:layout_width="@dimen/icon_size"