在单个视图组中的两个视图上捏缩放 android

发布于 2024-12-19 13:26:36 字数 3258 浏览 3 评论 0原文

我正在做一个项目,其中我需要单个布局中的两个带有缩放选项的图像视图。据我所知,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);
       }
   }

Zoom 功能在这里

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


       }
   }

I am doing a project in which i need two imageviews in single layout with zooming options. AFAIK android allows one view to make zoom easily. But my problem is not like that. I have two imageviews one over another. I've try to do it in Frame Layout. but the results are not so good. The images are shrink when it goes out of bounds of layout and the zooming is not so smooth. I have tried to do it with the deprecated Absolute layout.but it doesn't work
All suggestions are welcomed.

My Code is here

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

The MoveView Function is

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

The Zoom Function is here

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


       }
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀里藏娇 2024-12-26 13:26:36

我通过在框架布局上方放置两个图像视图并为框架布局编写触摸事件并放置两个按钮来选择要缩放的视图并为该图像设置矩阵来做到这一点

I did it by placing two image views above Frame Layout and write touch event for Frame Layout and put two buttons for choosing the view to zoom and set matrix for that image

酒废 2024-12-26 13:26:36

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

You should not use par.width = (int) width because the fractional part is truncated by a fault always by default,
instead uses par.width = Math.round (Width) and think of a mechanism to restore the default values ​​sometimes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文