我正在制作一个应用程序,其中有一个小玩具,左侧有一些连衣裙(ListView),您可以选择连衣裙。从该列表视图中,您只需选择一件衣服,这件衣服就会自动添加到视图中,现在我可以轻松拖动它,但我无法放大/缩小它。
下面是我的代码:
mKittyBGLayer.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
for (ImageView img1 : mArrayList) {
if (kittyStatus == img1.getId()) {
mKittyDress = img1;
break;
}
}
if (mKittyDress != null) {
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, (int) event.getX()
- mKittyDress.getWidth() / 2, (int) event.getY() - mKittyDress.getHeight() / 2);
mKittyDress.setLayoutParams(lp);
// 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;
break;
case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
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);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;
}
mKittyDress.setImageMatrix(matrix);
return true; // indicate event was handled
}
return true;
}
});
/** Determine the space between the first two fingers */
private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}
/** Calculate the mid point of the first two fingers */
private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}