本文介绍了Android的多图像缩放移动问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想移动旋转和调整在我的Andr​​oid应用程序的图像中,我发现从这个计算器code和它完美对我要求工作....但现在我需要移动两个图像所以这个code将复制第一个图像的动作时,我切换到第二图像..
1),我该怎么办?这就是
2)我尝试添加单独的触摸监听器这两个图像,但没有图像根本没有移动

  iv_choose_hats =(ImageView的)findViewById(R.id.iv_chooseHats);
        iv_choose_hats.setScaleType(ScaleType.MATRIX); //设置图像缩放类型为onTouch运动兼容
        iv_choose_hats.setOnTouchListener(本);            selected_hatImage =(ImageView的)findViewById(R.id.iv_selectedHat);
            selected_hatImage.setOnTouchListener(本);
            selected_hatImage.setScaleType(ScaleType.MATRIX);@覆盖
公共布尔onTouch(视图V,MotionEvent事件){    ImageView的观点=(ImageView的)V;    //转储触摸事件日志
    dumpEvent(事件);    //这里处理触摸事件...
    开关(event.getAction()及MotionEvent.ACTION_MASK){        案例MotionEvent.ACTION_DOWN:
             savedMatrix.set(矩阵);
             start.set(event.getX(),event.getY());
             模式= DRAG;
             lastEvent = NULL;             打破;        案例MotionEvent.ACTION_POINTER_DOWN:
            oldDist =间距(事件);
            savedMatrix.set(矩阵);
            中点(中,事件);
            模式= ZOOM;            lastEvent =新的浮动[4];
            lastEvent [0] = event.getX(0);
            lastEvent [1] = event.getX(1);
            lastEvent [2] = event.getY(0);
            lastEvent [3] = event.getY(1);
            D =旋转(事件);            打破;        案例MotionEvent.ACTION_UP:
        案例MotionEvent.ACTION_POINTER_UP:
            模式=无;
            lastEvent = NULL;            打破;        案例MotionEvent.ACTION_MOVE:
         如果(==模式拖动){
            matrix.set(savedMatrix);
            matrix.postTranslate(event.getX() - start.x,event.getY() - start.y);
         }
         否则,如果(==模式缩放和放大器;&安培; event.getPointerCount()== 2){
            浮newDist =间距(事件);            matrix.set(savedMatrix);
            如果(newDist> 10F){
               浮规模= newDist / oldDist;
               matrix.postScale(规模化,规模化,mid.x,mid.y);
            }
            如果(lastEvent!= NULL){
                newRot =旋转(事件);                浮动R = newRot-D;
                matrix.postRotate(R,view.getMeasuredWidth()/ 2,view.getMeasuredHeight()/ 2);
            }
         }
         打破;
  }      view.setImageMatrix(矩阵);
      返回true; //表示事件已处理
}


解决方案

需要使用具有onTouch事件自定义类的ImageView的一个

  iv_choose_hats =(ImageView的)findViewById(R.id.iv_chooseHats);
iv_choose_hats.setScaleType(ScaleType.MATRIX);
iv_choose_hats.setOnTouchListener(新Image_OnTouch());公共类Image_OnTouch实现OnTouchListener {
   @覆盖
   公共布尔onTouch(视图V,MotionEvent事件){   }
}

I want to move rotate and resize images in my android application, i've found this code from stackoverflow and its working perfectly for my requirement.... but now i need to move two images so this code will duplicate first image movements when i switched to the second image..1)how can i do thats...2)i tried to add separate on touch listeners for these two images, but none of the images didn't move at all

        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
}
解决方案

Need to use a custom class with onTouch event for one of the 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) {

   }
}

这篇关于Android的多图像缩放移动问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:40