使用Android中的Surface View放大和缩小

时间:2013-01-06 08:01:29

标签: android zoom surfaceview

我正在制作一个检查界面,当用户在底部签名时,我想放大到屏幕的那一部分。我使用相机尝试了这段代码。程序出错 - 意外停止。你能帮帮我吗?

    public class MainActivity extends Activity 
    {
        MySurfaceView mySurfaceView;
        private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

         Parameters params;
         Camera camera;

          int currentZoomLevel = 0, maxZoomLevel = 0;
          int MAX_ZOOM=200;

          ZoomControls zoomControls = (ZoomControls) findViewById(R.id.zoomControls1);

              @Override
             public void onCreate(Bundle savedInstanceState)  
             {                 
                   super.onCreate(savedInstanceState);
                   mySurfaceView = new MySurfaceView(this);
                   setContentView(mySurfaceView);
              } 

              class MySurfaceView extends SurfaceView
              {
                  Path path;
                  Thread thread = null;
                   SurfaceHolder surfaceHolder;
                volatile boolean running = false;
                   Random random;

                    public MySurfaceView(Context context)
                    {
                            super(context);

                       surfaceHolder = getHolder();
                  paint.setStyle(Paint.Style.STROKE);
                  paint.setStrokeWidth(3);
                    paint.setColor(Color.BLUE);
                  }  
             @Override
                public boolean onTouchEvent(MotionEvent event) 
             {
              if(event.getAction() == MotionEvent.ACTION_DOWN)
               {
                    path = new Path();
                      path.moveTo(event.getX(), event.getY());
                }
               else if(event.getAction() == MotionEvent.ACTION_MOVE)
               {
                     path.lineTo(event.getX(), event.getY());
               }
               else if(event.getAction() == MotionEvent.ACTION_UP)
              {
 path.lineTo(event.getX(), event.getY());
}
if(params.isZoomSupported())
{    
   maxZoomLevel = params.getMaxZoom();

   zoomControls.setIsZoomInEnabled(true);
       zoomControls.setIsZoomOutEnabled(true);

       zoomControls.setOnZoomInClickListener(new OnClickListener()
       {
        public void onClick(View v)
        {
            if(currentZoomLevel < MAX_ZOOM){
                 currentZoomLevel++;
                 camera.startSmoothZoom(currentZoomLevel); 
             }

        }
    });

   zoomControls.setOnZoomOutClickListener(new OnClickListener()
   {
           public void onClick(View v)
           {
                   if(currentZoomLevel > 0)
                   {
                       currentZoomLevel--;
                       camera.startSmoothZoom(currentZoomLevel);
                   }
           }
       });    
  }
  else
  {
    zoomControls.setVisibility(View.GONE);
  }


if(path != null)
{
 Canvas canvas = surfaceHolder.lockCanvas();
 canvas.drawPath(path, paint);
 //canvas.scale(20,20,canvas.getHeight()-20,canvas.getWidth()-20);


 surfaceHolder.unlockCanvasAndPost(canvas);
}
return running;
  }
    }
    }

1 个答案:

答案 0 :(得分:0)

ZoomControls未添加到您希望在此行的布局中显示的布局

setContentView(mySurfaceView);