拖动自定义imageview并实现触摸侦听器

时间:2013-07-22 09:09:02

标签: android

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

我可以添加自定义图像,但图像无法控制。

以下是我的onTouch代码

@覆盖 public boolean onTouch(View v,MotionEvent事件) {     CustomImageView view =(CustomImageView)v;     dumpEvent(事件);

// 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
}

}

0 个答案:

没有答案