Android根据另一个图像的尺度翻译图像

时间:2013-01-15 20:07:33

标签: android android-layout scale

我有这个应用程序,其中有2个ImageViews,一个像背景一样,另一个是位于背景图像上的小图像,在地图上就像一个说标记,需要保持相关的位置背景图片。

所以在拖拽上实现这一点很容易,但我需要的是在用户放大背景图像时保持小图像与背景图像的相对位置,这是迄今为止的代码(lampMatrix和lampStart是矩阵和用于较小图像的PointF):

    public boolean onTouch(View v, MotionEvent rawEvent) {
      WrapMotionEvent event = WrapMotionEvent.wrap(rawEvent);
      // ...
      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());

         savedLampMatrix.set(lampMatrix);
         lampStart.set(event.getX(), event.getY());

         Log.d(TAG, "mode=DRAG");
         mode = DRAG;
         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:
      case MotionEvent.ACTION_POINTER_UP:
         mode = NONE;
         Log.d(TAG, "mode=NONE");
         break;
      case MotionEvent.ACTION_MOVE:
         if (mode == DRAG) {
            // ...
            matrix.set(savedMatrix);
            matrix.postTranslate(event.getX() - start.x,
                  event.getY() - start.y);

            lampMatrix.set(savedLampMatrix);
            lampMatrix.postTranslate(event.getX() - lampStart.x, event.getY() - lampStart.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;
      }

      view.setImageMatrix(matrix);
      ((ImageView)findViewById(R.id.imageView1)).setImageMatrix(lampMatrix);
      return true; // indicate event was handled
   }

0 个答案:

没有答案