两个imageview与单独touchevent

时间:2012-06-18 06:34:29

标签: android imageview android-imageview

在我的xml文件中,我已声明了两个imageview,之后我使用setImageBitmap()方法在其中添加了图片,并且还应用了单独的触摸事件。

但问题是,当第二张图像添加到setImageBitmap()时,我无法移动第一张图像,现在只有第二张图像可以移动。
提前致谢并对不起英语。

working_bitmap = BitmapFactory.decodeFile(file_location+"cropped_image.jpg");  
outBitmap=Bitmap.createBitmap(
working_bitmap.getWidth(),
working_bitmap.getHeight(),Bitmap.Config.ARGB_8888);    
Canvas canvas1 = new Canvas(outBitmap);
canvas1.drawBitmap(working_bitmap, new Matrix(), null);
if(downloaded_shirt_image != null)
{
    int width = downloaded_shirt_image.getWidth();
    int height = downloaded_shirt_image.getHeight();
    int halfWidth = width/3;
    int halfHeight = height/3;
    //Half Scaled
    Bitmap bmHalf = Bitmap.createScaledBitmap(downloaded_shirt_image,
        halfWidth, halfHeight, false);
    proimg =(ImageView)findViewById(R.id.pro_img);
    proimg.setDrawingCacheEnabled(true);
    proimg.setImageBitmap(bmHalf);
    proimg.setVisibility(View.VISIBLE);
    proimg.setOnTouchListener(new View.OnTouchListener()
    {
      public boolean onTouch(View v, MotionEvent event) {
      ImageView view = (ImageView) v;
      int rotation = 25;
      // 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());
        Log.d(TAG, "mode=DRAG");
        mode = DRAG;
        proimg.invalidate();
        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) {
         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);
        }
     }
    proimg.invalidate();
    break;
 }
      view.setImageMatrix(matrix);
    return true; // indicate event was handled
  }
 });
  canvas1.drawBitmap(downloaded_shirt_image, 50, 192, null);
 }

 if(downloaded_pant_image != null)
 {
  int width = downloaded_pant_image.getWidth();
  int height = downloaded_pant_image.getHeight();
  int halfWidth = width/3;
  int halfHeight = height/3;
  //Half Scaled
  Bitmap bmHalf = Bitmap.createScaledBitmap(downloaded_pant_image,
                  halfWidth, halfHeight, false);
  pantimg =(ImageView)findViewById(R.id.pro_img_down);
  pantimg.setDrawingCacheEnabled(true);
  pantimg.setImageBitmap(bmHalf);
  pantimg.setVisibility(View.VISIBLE);
  pantimg.setOnTouchListener(new View.OnTouchListener()
  {
   public boolean onTouch(View v, MotionEvent event) {
   ImageView view = (ImageView) v;
   int rotation = 25;
   // 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());
  Log.d(TAG, "mode=DRAG");
  mode = DRAG;
  pantimg.invalidate();
  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) {
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);
}
}
pantimg.invalidate();
break;
}
view.setImageMatrix(matrix);
return true; // indicate event was handled
}
}                            
);
canvas1.drawBitmap(downloaded_pant_image, 110, 565, null);
}

1 个答案:

答案 0 :(得分:1)

请参阅以下链接的答案和评论,它可能对您有帮助。

Drag & Drop Two Images