在单个Viewgroup安卓中缩放两个视图

时间:2011-12-06 12:47:16

标签: android views pinch

我正在做一个项目,我需要在单一布局中使用缩放选项进行两次图像视图。 AFAIK android允许一个视图轻松进行缩放。但我的问题不是那样的。我有两个图像视图。我试着在Frame Layout中做到这一点。但结果并不那么好。当图像超出布局范围并且缩放不是那么平滑时,图像会缩小。我已尝试使用已弃用的Absolute布局。但它不起作用 欢迎所有建议。

我的代码在这里

  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);


       }
   }

2 个答案:

答案 0 :(得分:0)

我是通过在Frame Layout上方放置两个图像视图并为Frame Layout写入触摸事件并放置两个按钮来选择要缩放的视图并为该图像设置矩阵来实现的。

答案 1 :(得分:0)

您不应该使用par.width =(int)width,因为默认情况下小数部分会被错误截断, 而是使用par.width = Math.round(Width)并考虑一种机制来恢复默认值。