2012-10-03 60 views
0

我想在我的android應用程序中移動旋轉和調整圖像大小,我發現這個代碼從stackoverflow和它的工作完全符合我的要求....但現在我需要移動兩個圖像當我切換到第二個圖像時,此代碼將重複第一個圖像移動。 1)我怎麼做那個... 2)我試圖爲這兩個圖像添加單獨的觸摸監聽器,但沒有一個圖像沒有「T動彈Android多圖像移動調整大小問題

 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 
} 

回答

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

    } 
}