欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Camera镜头缩放:双指滑动、seekBar拖动

程序员文章站 2022-05-08 22:35:33
...

Camera镜头缩放主要有两种:监听手势滑动放下控制镜头缩放、seekBar拖动进度条控制镜头缩放

项目地址:https://github.com/LeeVanie/CameraDemo

1、双指滑动实现镜头缩放:
(备注:项目中双指滑动镜头缩放单独成类做了封装,不过原理和下面列出代码一样)

 /**
     * 向外放缩标志
     */
    private static final int ZOOM_OUT = 0;
    /**
     * 向内放缩标志
     */
    private static final int ZOOM_IN = 1;
  
    //重写onTouchEvent方法 获取手势
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //识别手势
        gestureDetector.onTouchEvent(event);
        final int action = event.getAction();
        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN: {
                break;
            }
            case MotionEvent.ACTION_UP: {
                break;
            }
            case MotionEvent.ACTION_POINTER_DOWN: {
                break;
            }
            case MotionEvent.ACTION_CANCEL: {
                break;
            }
        }
        return true;
    }
    //操作类
    class ScaleGestureListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        int mScaleFactor;
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            mScaleFactor = (int) detector.getScaleFactor();
            Camera.Parameters params = camera.getParameters();
            int zoom = params.getZoom();
            if (mScaleFactor == ZOOM_IN) {
                if (zoom < params.getMaxZoom()) 
                    zoom += 1;
            } else if (mScaleFactor == ZOOM_OUT) {
                if (zoom > 0) 
                    zoom -= 1;
            }
            params.setZoom(zoom);
            camera.setParameters(params);
            return false;
        }

    }

2、seekBar进度条拖动控制镜头缩放

seekBar = ((SeekBar) findViewById(R.id.seekbar));

//初始化seekBar
int maxZoom = parameters.getMaxZoom();
seekBar.setProgress(0);
seekBar.setMax(maxZoom*100);

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Camera.Parameters parameters = camera.getParameters();
                parameters.setZoom((int) (((progress * (1.0f / (maxZoom * 100))) * maxZoom)));
                camera.setParameters(parameters);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
               
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                
            }
        });
相关标签: Android camera