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