How can I make a SurfaceView larger than the screen?

前端 未结 4 931
清酒与你
清酒与你 2021-01-02 00:25

I would like to effectively make a simple digital zoom for the camera preview, so I thought I would simply resize my SurfaceView to be larger than the screen. Other questio

4条回答
  •  借酒劲吻你
    2021-01-02 01:16

    You can't make your surfaceView bigger than the screen. That being said there are ways around it.

    I found you can adjust the size of the canvas in the SurfaceView, which will allow zooming.

    public class DrawingThread extends Thread {
    private MagnificationView mainPanel;
    private SurfaceHolder surfaceHolder;
    private boolean run;
    
    public DrawingThread(SurfaceHolder surface, MagnificationView panel){
        surfaceHolder = surface;
        mainPanel = panel;
    }
    
    public SurfaceHolder getSurfaceHolder(){
        return surfaceHolder;
    }
    
    public void setRunning (boolean run){
        this.run = run;
    }
    
    public void run(){
        Canvas c;
        while (run){
            c = null;
            try {
                c = surfaceHolder.lockCanvas(null);
                synchronized (surfaceHolder){
                    mainPanel.OnDraw(c);
                }
            } finally {
                if (c != null){
                    surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
    

    }

    In the MagnificationView class add a method:

    public void OnDraw(Canvas canvas){
        if (canvas!=null){
            canvas.save();
            canvas.scale(scaleX,scaleY);      
    
            canvas.restore();
        }
    }
    

    DrawingThread would be a thread you start in in your Activity. Also in your MagnificationView class override the OnTouchEvent to handle your own pinch-zoom (which will modify scaleX & scaleY.

    Hope This solves your issue

提交回复
热议问题