时间:2021-05-20
本文实例为大家分享了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; }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Android编程实现AlertDialog自定义弹出对话框的方法。分享给大家供大家参考,具体如下:弹出对话框,显示自定义的布局文件弹出对话框提示
这篇博客为大家介绍Android自定义相机,并且实现倒计时拍照功能。首先自定义拍照会用到SurfaceView控件显示照片的预览区域,以下是布局文件:activ
前言:前面几篇讲了自定义控件绘制原理Android自定义控件基本原理详解(一),Android自定义控件之自定义属性(二),Android自定义控件之自定义组合
本文实例为大家分享了Android自定义view实现输入框的具体代码,供大家参考,具体内容如下自定义输入框的Viewpackagecom.fenghongzha
前言在Android开发中,我们有时需要实现类似IOS的对话框。今天我就来总结下,如何通过自定义的开发来实现类似的功能。自定义Dialog我们知道Android