android在翻译后跟踪位置并缩放图像

时间:2015-10-06 17:33:17

标签: android imageview position scaling

我是android的新手。 我希望在图像翻译和缩放后跟踪位置 在下面的代码中,我可以在翻译图像后跟踪位置,还可以找到比例因子。 但是,如何在缩放后获得左侧和顶部位置

imageView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            view = (ImageView) v;
            view.setScaleType(ImageView.ScaleType.MATRIX);

            switch (event.getAction() & MotionEvent.ACTION_MASK) 
            {
                case MotionEvent.ACTION_DOWN:   // first finger down only
                     matrix.set(view.getImageMatrix());
                     savedMatrix.set(matrix);

                     start.set(event.getX(), event.getY());
                     mode = DRAG;
                     break;
                case MotionEvent.ACTION_UP: // first finger lifted
                    move_x = event.getX()-start.x;
                    move_y = event.getY()-start.y;
                    laststop.x = laststop.x + move_x;
                    laststop.y = laststop.y + move_y;
                        tv_touch.setText("Up!! x = "+Float.valueOf(laststop.x)+
                                            ", y = "+Float.valueOf(laststop.y)+
                                            ", scale = "+ Float.valueOf(scale_new));


                case MotionEvent.ACTION_POINTER_UP: // second finger lifted
                            if (mode == ZOOM)
                            {
                                scale_new = scale * scale_new;

                            }
                            mode = NONE;
                            break;

                case MotionEvent.ACTION_POINTER_DOWN: // first and second finger down

                          oldDist = spacing(event);
                          if (oldDist > 5f) {
                              savedMatrix.set(matrix);
                              midPoint(mid, event);
                              mode = ZOOM;
                          }
                          break;

                case MotionEvent.ACTION_MOVE:

                          if (mode == DRAG) 
                          { 
                              matrix.set(savedMatrix);
                                tv_touch.setText("Move!! x = "+Float.valueOf(event.getX() - start.x)+", y = "+Float.valueOf(event.getY() - start.y));
                              matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); // create the transformation in the matrix  of points
                          } 
                          else if (mode == ZOOM) { 
                              // pinch zooming
                              float newDist = spacing(event);
                              if (newDist > 5f) {
                                  matrix.set(savedMatrix);
                                  scale = newDist / oldDist; 
                                  matrix.postScale(scale, scale, mid.x, mid.y);
                                  tv_touch.setText("BgScale: "+Float.valueOf(scale));
                              }
                          }
                          break;
            }

            view.setImageMatrix(matrix);            
            return true;
        }
    });

0 个答案:

没有答案