diff --git a/app/src/main/java/com/cjt2325/cameraview/MainActivity.java b/app/src/main/java/com/cjt2325/cameraview/MainActivity.java index 83b281f..60fdc60 100644 --- a/app/src/main/java/com/cjt2325/cameraview/MainActivity.java +++ b/app/src/main/java/com/cjt2325/cameraview/MainActivity.java @@ -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; @@ -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 @@ -48,11 +48,11 @@ public void recordSuccess(String url, Bitmap firstFrame) { } - @Override - public void quit() { - //退出按钮 - MainActivity.this.finish(); - } +// @Override +// public void quit() { +// //退出按钮 +// MainActivity.this.finish(); +// } }); //6.0动态权限获取 getPermissions(); diff --git a/camera/src/main/java/com/cjt2325/cameralibrary/JCameraView.java b/camera/src/main/java/com/cjt2325/cameralibrary/JCameraView.java index 74ea5ac..4efc1ef 100644 --- a/camera/src/main/java/com/cjt2325/cameralibrary/JCameraView.java +++ b/camera/src/main/java/com/cjt2325/cameralibrary/JCameraView.java @@ -34,6 +34,10 @@ import com.cjt2325.cameralibrary.view.CameraView; import java.io.IOException; +import java.lang.ref.WeakReference; +import java.util.concurrent.LinkedBlockingDeque; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; /** @@ -113,6 +117,7 @@ public class JCameraView extends FrameLayout implements CameraInterface.CameraOp private boolean firstTouch = true; private float firstTouchLength = 0; + private ThreadPoolExecutor mPoolExecutor; public JCameraView(Context context) { this(context, null); @@ -124,7 +129,12 @@ public JCameraView(Context context, AttributeSet attrs) { public JCameraView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); - mContext = context; + /** + * 使用弱引用的方式,避免造成内存泄漏 + */ + WeakReference contextWeakReference = new WeakReference<>(context); + mContext = contextWeakReference.get(); + //get AttributeSet TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.JCameraView, defStyleAttr, 0); iconSize = a.getDimensionPixelSize(R.styleable.JCameraView_iconSize, (int) TypedValue.applyDimension( @@ -136,6 +146,12 @@ public JCameraView(Context context, AttributeSet attrs, int defStyleAttr) { iconRight = a.getResourceId(R.styleable.JCameraView_iconRight, 0); duration = a.getInteger(R.styleable.JCameraView_duration_max, 10 * 1000); //没设置默认为10s a.recycle(); + + /** + * 使用线程池,避免内存泄漏 + */ + mPoolExecutor = new ThreadPoolExecutor(3, 5, 1, TimeUnit.SECONDS, new LinkedBlockingDeque(128)); + initData(); initView(); } @@ -301,12 +317,13 @@ public void onPause() { @Override public void surfaceCreated(SurfaceHolder holder) { LogUtil.i("JCameraView SurfaceCreated"); - new Thread() { + Runnable runnable = new Runnable() { @Override public void run() { CameraInterface.getInstance().doOpenCamera(JCameraView.this); } - }.start(); + }; + mPoolExecutor.execute(runnable); } @Override @@ -361,6 +378,8 @@ public boolean onTouchEvent(MotionEvent event) { case MotionEvent.ACTION_UP: firstTouch = true; break; + default: + break; } return true; } @@ -435,6 +454,8 @@ public void resetState(int type) { case TYPE_DEFAULT: mVideoView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); break; + default: + break; } mSwitchCamera.setVisibility(VISIBLE); mFlashLamp.setVisibility(VISIBLE); @@ -462,6 +483,8 @@ public void confirmState(int type) { break; case TYPE_DEFAULT: break; + default: + break; } mCaptureLayout.resetCaptureLayout(); } @@ -484,7 +507,7 @@ public void showPicture(Bitmap bitmap, boolean isVertical) { public void playVideo(Bitmap firstFrame, final String url) { videoUrl = url; JCameraView.this.firstFrame = firstFrame; - new Thread(new Runnable() { + Runnable runnable = new Runnable() { @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN) @Override public void run() { @@ -519,7 +542,8 @@ public void onPrepared(MediaPlayer mp) { e.printStackTrace(); } } - }).start(); + }; + mPoolExecutor.execute(runnable); } @Override @@ -594,6 +618,8 @@ private void setFlashRes() { mFlashLamp.setImageResource(R.drawable.ic_flash_off); machine.flash(Camera.Parameters.FLASH_MODE_OFF); break; + default: + break; } } }