0

我想在我的 android 应用程序中移动旋转和调整图像大小,我从 stackoverflow 中找到了这段代码,它可以完美地满足我的要求....但是现在我需要移动两个图像,所以这个代码会在我复制第一个图像移动切换到第二个图像.. 1)我该怎么做... 2)我尝试为这两个图像添加单独的触摸监听器,但没有一个图像根本没有移动

        iv_choose_hats = (ImageView)findViewById(R.id.iv_chooseHats);
        iv_choose_hats.setScaleType(ScaleType.MATRIX); // set the image scale type for compatible for onTouch movements
        iv_choose_hats.setOnTouchListener(this);

            selected_hatImage = (ImageView)findViewById(R.id.iv_selectedHat);
            selected_hatImage.setOnTouchListener(this);
            selected_hatImage.setScaleType(ScaleType.MATRIX);

@Override
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());
             mode = DRAG;
             lastEvent = null;

             break;

        case MotionEvent.ACTION_POINTER_DOWN:
            oldDist = spacing(event);         
            savedMatrix.set(matrix);
            midPoint(mid, event);
            mode = ZOOM;

            lastEvent = new float[4];
            lastEvent[0] = event.getX(0);
            lastEvent[1] = event.getX(1);
            lastEvent[2] = event.getY(0);
            lastEvent[3] = event.getY(1);
            d = rotation(event);

            break;

        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:
            mode = NONE;
            lastEvent = null;

            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 && event.getPointerCount()==2) {
            float newDist = spacing(event);

            matrix.set(savedMatrix);
            if (newDist > 10f) {              
               float scale = newDist / oldDist;
               matrix.postScale(scale, scale, mid.x, mid.y);
            } 
            if (lastEvent!=null){
                newRot = rotation(event); 

                float r = newRot-d;
                matrix.postRotate(r, view.getMeasuredWidth()/2, view.getMeasuredHeight()/2);  
            }
         }
         break;
  }

      view.setImageMatrix(matrix);
      return true; // indicate event was handled
}
4

1 回答 1

0

需要为其中一个图像视图使用带有 onTouch 事件的自定义类

iv_choose_hats = (ImageView)findViewById(R.id.iv_chooseHats);
iv_choose_hats.setScaleType(ScaleType.MATRIX);
iv_choose_hats.setOnTouchListener(new Image_OnTouch());

public class Image_OnTouch implements OnTouchListener { 
   @Override
   public boolean onTouch(View v, MotionEvent event) {

   }
}
于 2012-10-04T08:10:49.747 回答