Android自定義相機聚焦和顯示框

本文實例為大傢分享瞭Android自定義相機聚焦和顯示框的具體代碼,供大傢參考,具體內容如下

先看使用效果,白色圓框,放大後縮寫並變淡隱藏

下面是代碼

public class CameraFocusView extends AppCompatImageView {
 
 String TAG = getClass().getName();
 //顯示的圓寬顯示的位置
 public float currnetX = 40;
 public float currentY = 50;
 
 Paint paint;
 //半徑大小
 float radis = 100;
 //透明度
 int alpha = 255;
 boolean isAlpha = false;
 private ValueAnimator scaleAnimator,colorAnimator;
 
 public CameraFocusView(Context context) {
 super(context);
 init();
 }
 
 public CameraFocusView(Context context, @Nullable AttributeSet attrs) {
 super(context, attrs);
 init();
 }
 
 public CameraFocusView(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 init();
 }
 
 public void init() {
 scaleAnimator = new ValueAnimator();
 scaleAnimator.setFloatValues(80, 70, 90);
 scaleAnimator.setDuration(500);
 scaleAnimator.setInterpolator(new LinearInterpolator());
 //伸縮動畫效果
 scaleAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  @Override
  public void onAnimationUpdate(ValueAnimator animation) {
  radis = (float) animation.getAnimatedValue();
  invalidate();
  if (radis == 90){
   isAlpha = true;
   colorAnimator.start();
  }
  }
 });
 
 colorAnimator = new ValueAnimator();
 colorAnimator.setIntValues(255,0);
 colorAnimator.setDuration(3000);
 colorAnimator.setInterpolator(new LinearInterpolator());
 //顯示透明度動畫效果
 colorAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  @Override
  public void onAnimationUpdate(ValueAnimator animation) {
  alpha = (int) animation.getAnimatedValue();
  invalidate();
  }
 });
 
 //初始化不顯示
 setVisibility(GONE);
 }
 
 //開始現在圓框
 public void startAnimator() {
 isAlpha = false;
 if (scaleAnimator != null) {
  Log.e(TAG, "startAnimator: scaleAnimator != null");
  setVisibility(VISIBLE);
  scaleAnimator.start();
 } else {
  Log.e(TAG, "startAnimator: scaleAnimator == null");
 }
 }
 
 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 //畫筆設置
 paint = new Paint();
 paint.setStyle(Paint.Style.STROKE);//畫筆屬性是空心圓
 paint.setStrokeWidth(2);//設置畫筆粗細
 // 抗鋸齒
 paint.setAntiAlias(true);
 paint.setColor(Color.WHITE);
 paint.setAlpha(255);
 if (isAlpha){
  if (alpha > 0 && alpha < 155){
  paint.setAlpha(155);
  }else if (alpha == 0){
  setVisibility(GONE);
  }
 }
 canvas.drawCircle(currnetX, currentY, radis, paint);
 
 }
}

主界面圖

調用方式,點擊屏幕就能顯示

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, Camera.PreviewCallback{
 String TAG = getClass().getName();
 SurfaceView surfaceview;
 SurfaceHolder surfaceHolder;
 Camera camera;
 AutoFocusCallBackClass autoFocusCallBackClass;
 CameraFocusView cameraFocusView;
 
 SensorControler sensorControler;
 
 int width = 480;
 int height = 800;
 int surfaceViewWidth = 480;
 int surfaceViewHeight = 800;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 ConstraintLayout root = findViewById(R.id.root);
 
 surfaceview = findViewById(R.id.surfaceview);
 surfaceHolder = surfaceview.getHolder();
 
 Display display = getWindowManager().getDefaultDisplay();
 width = display.getWidth();
 height = display.getHeight();
 cameraFocusView = new CameraFocusView(this);
 cameraFocusView.setMinimumWidth(width);
 cameraFocusView.setMinimumHeight(height);
 root.addView(cameraFocusView);
 
 
 
 surfaceview.setOnTouchListener(new View.OnTouchListener() {
  @Override
  public boolean onTouch(View view, MotionEvent motionEvent) {
  float reax = motionEvent.getX();
  float reay = motionEvent.getY();
  focusTouch(reax,reay);
  return false;
  }
 });
 
 surfaceHolder.addCallback(this);
 }
 
 
 @Override
 protected void onStart() {
 super.onStart();
 if (sensorControler != null){
  sensorControler.start();
 }
 }
 
 @Override
 protected void onStop() {
 super.onStop();
 if (sensorControler != null){
  sensorControler.stop();
 }
 }
 
 public void focusTouch(float reac_x, float reac_y){
 int areaX = (int)(reac_x / surfaceview.getWidth() * 2000) - 1000;
 int areaY = (int)(reac_y / surfaceview.getHeight() * 2000) - 1000;
 
 if (camera == null){
  return;
 }
 Camera.Parameters parameters = camera.getParameters();
 if (parameters == null){
  return;
 }
 //創建Rect區域
 Rect focusArea = new Rect();
 focusArea.left = Math.max(areaX -100,-1000);
 focusArea.top = Math.max(areaY - 100,-1000);
 focusArea.right = Math.min(areaX + 100,1000);
 focusArea.bottom = Math.min(areaY + 100,1000);
 
//  Log.e(TAG, "onTouch: left = " + focusArea.left + " top = " + focusArea.top + " right = " + focusArea.right + " bottom = " + focusArea.bottom);
 //創建Camera.Area
 Camera.Area cameraArea = new Camera.Area(focusArea,100);
 List<Camera.Area> mereringAreas = new ArrayList<>();
 List<Camera.Area> focusAreas = new ArrayList<>();
 
 if (parameters.getMaxNumMeteringAreas() > 0){
  mereringAreas.add(cameraArea);
  focusAreas.add(cameraArea);
 }
 parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
 parameters.setFocusAreas(focusAreas);
 parameters.setMeteringAreas(mereringAreas);
 try {
  camera.cancelAutoFocus();
  camera.setParameters(parameters);
 }catch (Exception e){
  e.printStackTrace();
 }
 
 if (cameraFocusView != null) {
  cameraFocusView.currnetX = reac_x;
  cameraFocusView.currentY = reac_y;
  cameraFocusView.startAnimator();
 }
 }
 
 @Override
 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
 super.onRequestPermissionsResult(requestCode, permissions, grantResults);
 if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
  Toast.makeText(this,"ok",Toast.LENGTH_SHORT).show();
  initCamera();
 }else {
  Toast.makeText(this,"no",Toast.LENGTH_SHORT).show();
 }
 }
 
 private void initCamera(){
 camera = Camera.open(0);
 surfaceViewWidth = surfaceview.getWidth();
 surfaceViewHeight = surfaceview.getHeight();
 Log.e(TAG, "surfaceCreated: " + surfaceview.getWidth() + " " + surfaceview.getHeight() );
 
 if (camera != null) {
  try {
  camera.setPreviewCallback(this);
  Camera.Parameters parameters = camera.getParameters();
  //需要設置大小,否則在聚焦後就才色調出現縮小
  parameters.setPreviewSize(height,width);
  camera.setParameters(parameters);
  camera.setDisplayOrientation(90);
  camera.setPreviewDisplay(surfaceHolder);
  camera.startPreview();
  } catch (IOException e) {
  e.printStackTrace();
  }
 }
 }
 
 @Override
 public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
 if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED){
  ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA},1);
 }else {
  initCamera();
 }
 }
 
 @Override
 public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
 
 }
 
 @Override
 public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
 if (camera != null){
  camera.setPreviewCallback(null);
  camera.stopPreview();
  camera.release();
  camera = null;
 }
 }
 
 
 @Override
 public void onPreviewFrame(byte[] bytes, Camera camera) {
 
 }
 
 class AutoFocusCallBackClass implements Camera.AutoFocusCallback{
 
 @Override
 public void onAutoFocus(boolean b, Camera camera) {
  Log.e(TAG, "onAutoFocus: " + b);
 }
 }
 
 @Override
 protected void onDestroy() {
 super.onDestroy();
 if (camera != null){
  camera.setPreviewCallback(null);
  camera.stopPreview();
  camera.release();
  camera = null;
 }
}

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: