Question

I have made my own code for my 'implementation' of a pinch zoom feature and it works, but it is not user-friendly at all. Often very sporadic and in general not pretty at all. I was wondering if any of you could help me out, maybe a easier way or different approach. Perhaps using the GestureListener I'm not sure how to use it though. Any help would be appreciative.

Below is my current code:

    public boolean onTouchEvent(MotionEvent event) {
        float cr, cr2 = 0.0f;
        cx = new float[2];
        cy = new float[2];
        float zx;
        final int action = event.getAction();
        switch (action) {
        case MotionEvent.ACTION_DOWN: {
            break;
        }
        case MotionEvent.ACTION_POINTER_DOWN: {
            break;
            }
        case MotionEvent.ACTION_MOVE: {
            for (int i = 0; i<event.getPointerCount(); i++) {
            if (run == true) { //see if it is the first time running program and set orig to the scale / screen res 
            Orig = scale * 800; //800 = screen res
            }
            cx[i] = event.getX(i); //get x and y for each finger
            cy[i] = event.getY(i);

            cr = (cx[0] - cx[1])*(cx[0] - cx[1]) + (cy[0] - cy[1])*(cy[0] - cy[1]); //get the distance between pointers
            cr2 = FloatMath.sqrt(cr);

            if (run == true) {
            scale = cr2/800;
            run = false;
            }
            else {
                if (scale >=  550.0f) {
                    scale = 1.0f;
                }
                if (scale <= 0.0f) {
                    scale = 1.0f;
                }
                nScale = Orig/cr2;
                scale = nScale * 0.5f;

            }
            }}  

This is probably not the best way to do this at all. Would taking the average over several events give better responses?

Thanks

Was it helpful?

Solution

See the zoom support tutorials on Sony Ericsson Developer World, hope they can give you some clue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top