0

//画像を上下にのみドラッグしたい、画像を左右にドラッグしたくない、これを制限する方法と、画像をドラッグしているときに画面の境界を越える、これを制限する方法.助けてください?

public class MultiTouch extends Activity implement OnTouchListener {

// these matrices will be used to move and zoom image
private Matrix matrix = new Matrix();
private Matrix savedMatrix = new Matrix();
// we can be in one of these 3 states
private static final int NONE = 0;
private static final int DRAG = 1;
private static final int ZOOM = 2;
private int mode = NONE;
// remember some things for zooming
private PointF start = new PointF();
private PointF mid = new PointF();
private float oldDist = 1f;
private float d = 0f;
private float newRot = 0f;
private float[] lastEvent = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ImageView view = (ImageView) findViewById(R.id.imageView);

    view.setOnTouchListener(this);
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public boolean onTouch(View v, MotionEvent event) {
    // handle touch events here

    ImageView view = (ImageView) v;

    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        System.out.println("start.x" + start.x + "," + "start.y" + start.y);

        savedMatrix.set(matrix);

        start.set(event.getX(), event.getY());

        mode = DRAG;

        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);


            float dx = event.getX() - start.x;
            float dy = event.getY() - start.y;
            int width32 = view.getWidth();
            int height32 = view.getHeight();
            System.out.println("dx" + dx + "," + "dy" + dy);
            matrix.postTranslate(dx, dy);

        }
        break;
    }

    view.setImageMatrix(matrix);

    return true;
}

}

4

0 に答える 0