2011-12-06 43 views
-1

我正在做一个项目,其中需要使用缩放选项的单个布局中的两个图像视图。 AFAIK android允许一个视图轻松进行缩放。但我的问题不是那样的。我有两个图像浏览。我试图在框架布局中做到这一点。但结果并不好。当图像超出布局范围并且缩放不太平滑时,图像会缩小。我试图用弃绝的绝对布局来做。但它不起作用 欢迎您提供所有建议。在单个视图组中缩放两个视图android

我的代码是在这里

int x = (int)event.getRawX(); 
    int y = (int)event.getRawY(); 
     ImageView view = (ImageView) v; 

     switch (event.getAction() & MotionEvent.ACTION_MASK) { 
      case MotionEvent.ACTION_DOWN: 
      savedMatrix.set(matrix); 
      start.set(event.getX(), event.getY()); 

      mode = DRAG; 
      hoffset = x - v.getLeft(); 
     voffset = y - v.getTop(); 
      break; 
      case MotionEvent.ACTION_POINTER_DOWN: 
      oldDist = spacing(event); 
      Log.d(TAG, "oldDist=" + oldDist); 
      if (oldDist > 10f) { 
       savedMatrix.set(matrix); 
       midPoint(mid, event); 
       mode = ZOOM; 
       Log.d(TAG, "mode=ZOOM"); 
      } 
      break; 
      case MotionEvent.ACTION_UP: 
      // moveView(v, x - hoffset, y - voffset); 
      case MotionEvent.ACTION_POINTER_UP: 
      mode = NONE; 

      Log.d(TAG, "mode=NONE"); 
      break; 
      case MotionEvent.ACTION_MOVE: 
      if (mode == DRAG) { 
       moveView(v, x - hoffset, y - voffset); 
       hrecent = x; 
       vrecent = y; 
      } 
      else if (mode == ZOOM) { 


       newDist = spacing(event); 
       if (newDist > 10f) { 

        matrix.set(savedMatrix); 

        float scale = newDist/oldDist; 
     matrix.postScale(ScaleFactorForZoom, ScaleFactorForZoom, mid.x, mid.y); 



       float delta =10.0f+ (newDist -oldDist)/oldDist; 
      // view.setImageMatrix(matrix);  
       float newHeight; 
       float newWidth; 


     newHeight=(float) ((delta)+view.getHeight()); 
     newWidth=(float) ((delta)+view.getWidth()); 

        oldDist = newDist; 



       zoomView(view, newWidth, newHeight); 

       } 
      } 
      break; 
      } 



      return true; 
    } 

的MoveView功能是

 public void moveView(View v, int x, int y) 
     { 
     ViewGroup.LayoutParams params = v.getLayoutParams(); 
     if (params instanceof FrameLayout.LayoutParams) 
     { 
      FrameLayout.LayoutParams par = (FrameLayout.LayoutParams)params; 
      View parent = (View)(v.getParent()); 
      x = Math.min(x, parent.getWidth()); 
      x = Math.max(x, 0); 
      y = Math.min(y, parent.getHeight()); 
      y = Math.max(y, 0); 


      par.leftMargin=x; 
      par.topMargin=y; 
      v.setLayoutParams(par); 
     } 
    } 

变焦功能在这里

public void zoomView(View v, float width, float height) 
    { 
     ViewGroup.LayoutParams params = v.getLayoutParams(); 
     if (params instanceof FrameLayout.LayoutParams) 
     { 
      FrameLayout.LayoutParams par = (FrameLayout.LayoutParams)params; 
      par.width = (int)width; 
      par.height = (int)height; 

      v.setLayoutParams(par); 


     } 
    } 

回答

0

我做到了,通过将上述框架布局的两个图像视图和为Frame Layout编写触摸事件,并将两个按钮用于选择视图以缩放和设置该ima的矩阵ge

+3

如果发现请发布完整的解决方案。这对某人会有所帮助。 –

0

您不应该使用par.width =(int)宽度,因为小数部分总是默认被故障截断, 改为使用par.width = Math.round(Width)并考虑要恢复的机制有时默认值。