缩放 ViewGroup 中的 ImageView

发布于 2024-11-26 06:56:26 字数 2179 浏览 2 评论 0原文

我在 ViewGroup 中有不同的 ImageView 。每个 ImageView 都有这样的 onTouch 事件:

      public boolean onTouch(View v,MotionEvent event){
      ImageView view = (ImageView)v;

      // Dump touch event to log
      dumpEvent(event);

      // Handle touch events here...
      switch (event.getAction() & MotionEvent.ACTION_MASK )
      {
          case MotionEvent.ACTION_DOWN:

              savedMatrix.set(matrix);
              start.set(event.getX(), event.getY());
              Log.d(TAG, "mode=DRAG");
              mode = DRAG;

              break;
          case MotionEvent.ACTION_UP:
          case MotionEvent.ACTION_POINTER_UP:
              mode = NONE;
              Log.d(TAG, "mode=NONE");
              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_MOVE:
              if (mode == DRAG)
              {
                  matrix.set(savedMatrix);
                  matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
              }
              else if (mode == ZOOM)
              {
                  float newDist = spacing(event);
                  Log.d(TAG, "newDist=" + newDist);
                  if (newDist > 10f)
                  {
                      matrix.set(savedMatrix);
                      float scale = newDist / oldDist;
                      matrix.postScale(scale, scale, mid.x, mid.y);
                  }
              }
              break;
      }
      Log.d(TAG, "setting matrix " + matrix);
      view.setImageMatrix(matrix);

      return true; // Indicate event was handled
  }

在 logcat 中,我看到 onTouch 事件已被处理,但没有任何反应。 在 ViewGroup 中,我使用 InterceptTouchEvent 来拖动 ImageView 并像 Android 主屏幕一样移动滑动视图。

我认为 ViewGroup 和 ImageView 在缩放触摸事件上存在问题,或者 ViewGroup 有特定设置。有人可以告诉我是否可以在 ViewGroup 的子级内部进行缩放以及如何实现它?

I have different ImageView in a ViewGroup. Every ImageView has onTouch event like this:

      public boolean onTouch(View v,MotionEvent event){
      ImageView view = (ImageView)v;

      // Dump touch event to log
      dumpEvent(event);

      // Handle touch events here...
      switch (event.getAction() & MotionEvent.ACTION_MASK )
      {
          case MotionEvent.ACTION_DOWN:

              savedMatrix.set(matrix);
              start.set(event.getX(), event.getY());
              Log.d(TAG, "mode=DRAG");
              mode = DRAG;

              break;
          case MotionEvent.ACTION_UP:
          case MotionEvent.ACTION_POINTER_UP:
              mode = NONE;
              Log.d(TAG, "mode=NONE");
              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_MOVE:
              if (mode == DRAG)
              {
                  matrix.set(savedMatrix);
                  matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
              }
              else if (mode == ZOOM)
              {
                  float newDist = spacing(event);
                  Log.d(TAG, "newDist=" + newDist);
                  if (newDist > 10f)
                  {
                      matrix.set(savedMatrix);
                      float scale = newDist / oldDist;
                      matrix.postScale(scale, scale, mid.x, mid.y);
                  }
              }
              break;
      }
      Log.d(TAG, "setting matrix " + matrix);
      view.setImageMatrix(matrix);

      return true; // Indicate event was handled
  }

In logcat I see that onTouch event was handled but nothing happen.
In ViewGroup I use InterceptTouchEvent for drag ImageView and move sliding view like android homescreen.

I think that there's a problem between ViewGroup and ImageView on dispacth touch event for zoom or there's a particular setting for ViewGroup. Can someone tell me if is possible zoom inside a child of a ViewGroup and how implement it?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文