1

我有一个扩展 ImageView 类的 CustomImageView 类。现在我有另一个片段类,我在其中显示这个 customimageview。基本上我想添加一个 CustomImageView 并在其上实现拖放操作。

我可以添加自定义图像,但图像没有得到控制。

以下是我的 onTouch 代码

@Override public boolean onTouch(View v, MotionEvent event) { CustomImageView view = (CustomImageView) v; 转储事件(事件);

// Handle touch events here...
switch (event.getAction() & MotionEvent.ACTION_MASK) 
{
case MotionEvent.ACTION_DOWN:
    System.out.println("ACTION_DOWN");
    savedMatrix.set(matrix);
    start.set(event.getX(), event.getY());
    mode = DRAG;
    break;
case MotionEvent.ACTION_UP:
    System.out.println("ACTION_UP");

case MotionEvent.ACTION_POINTER_UP:
    mode = NONE;
    break;

case MotionEvent.ACTION_POINTER_DOWN:
    System.out.println("ACTION_POINTER_DOWN");

    oldDist = spacing(event);
    if (oldDist > 10f) {
        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_MOVE:
    if (mode == DRAG)
    {
        System.out.println("ACTION_MOVe inside DRAG");

        matrix.set(savedMatrix);
        matrix.postTranslate(event.getX() - start.x,
        event.getY() - start.y);
    }
    else if (mode == ZOOM) {
        System.out.println("ACTION_ZOOM");

        float newDist = spacing(event);
        if (newDist > 10f) {
            matrix.set(savedMatrix);
            float scale = newDist / oldDist;
            matrix.postScale(scale, scale, mid.x, mid.y);
        }
        if (lastEvent != null && event.getPointerCount() == 3)
        {
            newRot = rotation(event);
            float r = newRot - d;
            float[] values = new float[9];
            matrix.getValues(values);
            float tx = values[2];
            float ty = values[5];
            float sx = values[0];
            float xc = (view.getWidth() / 2) * sx;
            float yc = (view.getHeight() / 2) * sx;
            matrix.postRotate(r, tx + xc, ty + yc);
        }
    }
    break;
}
    view.setImageMatrix(matrix);
    return true; // indicate event was handled
}

}

4

0 回答 0