Android多图像移动调整大小问题

时间:2012-10-03 06:29:04

标签: android android-layout android-imageview

我想在我的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
}

1 个答案:

答案 0 :(得分:0)

需要将onTouch事件的自定义类用于其中一个imageview

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

   }
}