本文实例为大家分享了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; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小牛知识库。
本文向大家介绍Android自定义相机Camera实现手动对焦的方法示例,包括了Android自定义相机Camera实现手动对焦的方法示例的使用技巧和注意事项,需要的朋友参考一下 前言 我采用的是Camera来实现自定义相机的,如果你使用的是Camera2,那本文将不适用你。为了减少篇幅,本文主要讲解手动对焦的实现方式,前提是你已经能实现自定义相机的预览等基础功能。 目录 手动对焦的方法解析 实现
本文向大家介绍Android 用 camera2 API 自定义相机,包括了Android 用 camera2 API 自定义相机的使用技巧和注意事项,需要的朋友参考一下 前言 笔者因为项目需要自定义相机,所以了解了一下 Android 关于 camera 这块的 API。Android SDK 21(LOLLIPOP) 开始已经弃用了之前的 Camera 类,提供了 camera2 相关 API
我正在尝试弹出一个自定义对话框,当我点击一个按钮,但它不会弹出在所有。我的应用程序基本上是一个日历,我将使用sqlite在日历中添加/保留约会和其他内容,使用对话框,这是指定约会细节的地方。 我为此使用的代码如下: 我做错了什么?
uniapp 自定义相机进行连续拍照 1.使用live-pusher 在进行拍照的时候 snapshot提示未定义 2.video 和 canvas 但是视频流没有显示出来 video显示的圈 结果 自定义相机可以单拍也可以连续拍多张
本文向大家介绍Android 自定义相机及分析源码,包括了Android 自定义相机及分析源码的使用技巧和注意事项,需要的朋友参考一下 Android 自定义相机及分析源码 使用Android 系统相机的方法: 要想让应用有相机的action,咱们就必须在清单文件中做一些声明,好让系统知道,如下 action的作用就是声明action的类型,便于Intent的使用,category的作用就是注册,
问题内容: 在创建我自己的SimpleAdapter对象之前,因为我想更改行的颜色,所以我只是使用new SimpleAdapter(…)。现在,我正在使用自己的自定义SimpleAdapter,行颜色正在更改,但是我的文本没有得到更新。我已经调用了adapter.notifyDataSetChanged(),但它仍只显示示例文本“ TextView”。正如我所说,当我没有创建自己的适配器时,一切